项目作者: kenych

项目描述 :
jenkins config as code, poc
高级语言: Groovy
项目地址: git://github.com/kenych/jenkins_config_as_code.git
创建时间: 2018-03-17T23:37:06Z
项目社区:https://github.com/kenych/jenkins_config_as_code

开源协议:

下载


Advanced Jenkins config as code setup with 4 steps

Creating Jenkins configation as code and applying changes without downtime with Groovy, Java, Docker and Jenkins job.

POC:
1) Being able to update any Jenkins master or slave immediately - no new image, no redeploy, no downtime
2) No manual changes through UI - everything is kept as a code, and as a result:
3) Jenkins current state and state of image + config is kept in sync
4) Any change could be tested immediatelly without vicious cycle: create a new image, deploy, test, and if fails - repeat!
5) Creating a configuration that could be applied for specific environment only(prod vs test/dev Jenkins), with inheritence of common config and custom per jenkins config

Step 1: Write groovy to interact with Java API

  1. import hudson.model.*
  2. import jenkins.model.*
  3. import com.cloudbees.plugins.credentials.CredentialsScope
  4. import com.cloudbees.plugins.credentials.domains.Domain
  5. import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl
  6. def domain = Domain.global()
  7. def store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
  8. def instance = System.getenv("JENKINS_INSTANCE_NAME").replaceAll('-','_')
  9. ConfigObject conf = new ConfigSlurper().parse(new File(System.getenv("JENKINS_HOME")+'/jenkins_config/credentials.txt').text)
  10. conf.common_credentials.each { key, credentials ->
  11. println("Adding common credential ${key}")
  12. store.addCredentials(domain, new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, key, credentials.description, credentials.username, credentials.password))
  13. }
  14. conf."${instance}_credentials".each { key, credentials ->
  15. println("Adding ${instance} credential ${key}")
  16. store.addCredentials(domain, new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, key, credentials.description, credentials.username, credentials.password))
  17. }
  18. println("Successfully configured credentials")

Step 2: Create config for the script

  1. common_credentials {
  2. exclude{
  3. tyrion-jenkins
  4. }
  5. data{
  6. jenkins_service_user = [
  7. username: 'jenkins_service_user',
  8. password: '{{with $secret := secret "secret/jenkins/jenkins_service_user" }}{{ $secret.Data.value }}{{end}}',
  9. description :'for automated jenkins jobs'
  10. ]
  11. slack = [
  12. username: '{{with $secret := secret "secret/slack/user" }}{{ $secret.Data.value }}{{end}}',
  13. password: '{{with $secret := secret "secret/slack/pass" }}{{ $secret.Data.value }}{{end}}',
  14. description: 'slack credentials'
  15. ]
  16. }
  17. }
  18. custom_credentials {
  19. include{
  20. john-snow-jenkins
  21. arya-jenkins
  22. sansa-jenkins
  23. }
  24. data{
  25. artifactory = [
  26. username: 'arti',
  27. password: '{{with $secret := secret "secret/jenkins/artifactory" }}{{ $secret.Data.artifactory_password }}{{end}}',
  28. description: 'Artifactory credentials'
  29. ]
  30. }
  31. }
  32. tyrion-jenkins_credentials {
  33. data{
  34. nexus=[
  35. 'username':'deployment',
  36. 'password':'{{with $secret := secret "secret/jenkins/nexus" }}{{ $secret.Data.nexus_password }}{{end}}',
  37. 'description':'Nexus credentials'
  38. ]
  39. }
  40. }

Step 3: Checkout config and script and inject secrets and other variables with consul-template in container:

  1. #!/usr/bin/env bash
  2. git clone ssh://git@your_scm_here/jenkins_config_as_code.git ${JENKINS_HOME}/jenkins_config
  3. mv ${JENKINS_HOME}/jenkins_config/*.groovy ${JENKINS_HOME}/init.groovy.d/
  4. consul-template \
  5. -consul-addr "$CONSUL_ADDR" \
  6. -vault-addr "$VAULT_ADDR" \
  7. -config "jenkins_config.hcl" \
  8. -once

Step 4: Update continuously with Jenkins job without downtime

  1. node {
  2. stage('checkout') {
  3. sh '''
  4. git clone ssh://git@your_scm_here/jenkins_config_as_code.git ${JENKINS_HOME}/jenkins_config
  5. mv ${JENKINS_HOME}/jenkins_config/*.groovy ${JENKINS_HOME}/init.groovy.d/
  6. '''
  7. }
  8. stage('run consul template'){
  9. sh '''
  10. consul-template \
  11. -consul-addr "$CONSUL_ADDR" \
  12. -vault-addr "$VAULT_ADDR" \
  13. -config "jenkins_config.hcl" \
  14. -once
  15. '''
  16. }
  17. stage('update credentials') {
  18. load("/var/jenkins_home/init.groovy.d/credentials.groovy")
  19. }
  20. stage('update k8s') {
  21. load("/var/jenkins_home/init.groovy.d/kubernetes.groovy")
  22. }
  23. }