Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. pipeline {
  2. agent {
  3. kubernetes {
  4. label UUID.randomUUID().toString()
  5. containerTemplate {
  6. name 'this'
  7. image 'staywell/terraform:1.1.0'
  8. ttyEnabled true
  9. command 'cat'
  10. }
  11. }
  12. }
  13.  
  14. options {
  15. buildDiscarder(logRotator(numToKeepStr: '30'))
  16. }
  17.  
  18. environment {
  19. AWS_ACCESS_KEY_ID = credentials('aws.id')
  20. AWS_SECRET_ACCESS_KEY = credentials('aws.key')
  21. }
  22.  
  23. stages {
  24. stage('Initialize') {
  25. steps {
  26. container('this') {
  27. configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBECONFIG')]) {
  28. sh '''
  29. # calculate env name based on git repository name
  30. export TF_VAR_env=$(git remote get-url origin | sed -e "s/.*env-//" | sed -e "s/.git//")
  31.  
  32. # This config would be better put inside config.tf, but due to a Terraform limitation you
  33. # cannot use variables in a backend configuration. Therefore this is the only way to
  34. # dynamically set the "key" value so that it is unique for each environment.
  35. #
  36. # Note that the "region" value below is NOT the region that your Terraform infrastructure
  37. # is being deployed to. It is only the region of the state storage bucket and will be the
  38. # same across all Staywell environments.
  39. terraform init \\
  40. -backend-config "bucket=staywell-terraform-state" \\
  41. -backend-config "region=us-west-2" \\
  42. -backend-config "key=${TF_VAR_env}.tfstate" \\
  43. -backend-config "encrypt=true"
  44. '''
  45. }
  46. }
  47. }
  48. }
  49. stage('Plan') {
  50. steps {
  51. container('this') {
  52. configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBECONFIG')]) {
  53. sh '''
  54. # calculate env name based on git repository name
  55. export TF_VAR_env=$(git remote get-url origin | sed -e "s/.*env-//" | sed -e "s/.git//")
  56.  
  57. # uncomment for a single run any time the EKS cluster is destroyed and needs to be rebuilt
  58. # terraform state rm module.worker_config_map
  59. # terraform state rm module.jenkins
  60. terraform plan -input=false -out .tfplan
  61. '''
  62. }
  63. }
  64. }
  65. }
  66. /*
  67. stage('Apply') {
  68. when {
  69. expression { env.BRANCH_NAME == 'master' }
  70. }
  71. steps {
  72. container('this') {
  73. timeout(time: 5, unit: 'MINUTES') {
  74. input message: 'Continuing may change or destroy existing infrastructure, be sure to review the plan stage before continuing'
  75. }
  76. configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBECONFIG')]) {
  77. sh 'terraform apply -input=false -auto-approve .tfplan'
  78. sh 'terraform output worker_config_map > worker-config-map.yml'
  79. sh 'terraform output kubeconfig > kubeconfig'
  80. }
  81. }
  82. }
  83. }
  84. */
  85. }
  86.  
  87. post {
  88. always {
  89. archiveArtifacts artifacts: 'worker-config-map.yml,kubeconfig', fingerprint: true, allowEmptyArchive: true
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement