Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pipeline {
- agent {
- kubernetes {
- label UUID.randomUUID().toString()
- containerTemplate {
- name 'this'
- image 'staywell/terraform:1.1.0'
- ttyEnabled true
- command 'cat'
- }
- }
- }
- options {
- buildDiscarder(logRotator(numToKeepStr: '30'))
- }
- environment {
- AWS_ACCESS_KEY_ID = credentials('aws.id')
- AWS_SECRET_ACCESS_KEY = credentials('aws.key')
- }
- stages {
- stage('Initialize') {
- steps {
- container('this') {
- configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBECONFIG')]) {
- sh '''
- # calculate env name based on git repository name
- export TF_VAR_env=$(git remote get-url origin | sed -e "s/.*env-//" | sed -e "s/.git//")
- # This config would be better put inside config.tf, but due to a Terraform limitation you
- # cannot use variables in a backend configuration. Therefore this is the only way to
- # dynamically set the "key" value so that it is unique for each environment.
- #
- # Note that the "region" value below is NOT the region that your Terraform infrastructure
- # is being deployed to. It is only the region of the state storage bucket and will be the
- # same across all Staywell environments.
- terraform init \\
- -backend-config "bucket=staywell-terraform-state" \\
- -backend-config "region=us-west-2" \\
- -backend-config "key=${TF_VAR_env}.tfstate" \\
- -backend-config "encrypt=true"
- '''
- }
- }
- }
- }
- stage('Plan') {
- steps {
- container('this') {
- configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBECONFIG')]) {
- sh '''
- # calculate env name based on git repository name
- export TF_VAR_env=$(git remote get-url origin | sed -e "s/.*env-//" | sed -e "s/.git//")
- # uncomment for a single run any time the EKS cluster is destroyed and needs to be rebuilt
- # terraform state rm module.worker_config_map
- # terraform state rm module.jenkins
- terraform plan -input=false -out .tfplan
- '''
- }
- }
- }
- }
- /*
- stage('Apply') {
- when {
- expression { env.BRANCH_NAME == 'master' }
- }
- steps {
- container('this') {
- timeout(time: 5, unit: 'MINUTES') {
- input message: 'Continuing may change or destroy existing infrastructure, be sure to review the plan stage before continuing'
- }
- configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBECONFIG')]) {
- sh 'terraform apply -input=false -auto-approve .tfplan'
- sh 'terraform output worker_config_map > worker-config-map.yml'
- sh 'terraform output kubeconfig > kubeconfig'
- }
- }
- }
- }
- */
- }
- post {
- always {
- archiveArtifacts artifacts: 'worker-config-map.yml,kubeconfig', fingerprint: true, allowEmptyArchive: true
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement