Created
February 7, 2020 14:13
-
-
Save adiii717/8271a65d0c8469d4bbc9405f2850e8f7 to your computer and use it in GitHub Desktop.
Terraform simple pipeline with Jenkins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent any | |
options { timestamps () | |
disableConcurrentBuilds() } | |
environment { | |
TERRAFORM_CMD='terraform' | |
} | |
stages { | |
stage('Build') { | |
steps { | |
sh ''' | |
# prepare environment for pipeline | |
''' | |
} | |
} | |
stage('init/plan/apply/destroy') { | |
steps { | |
sh ''' | |
cd myrepo | |
# TFACTION can be `init`, `plan`, `apply` and `destory`, | |
# some environment varialbe like creating resouces with that env prefix, `ENV_PREFIX` | |
# Also the pipeline assuem you storing stats on remote | |
case $TFACTION in | |
init) | |
# create workspace if not exist, or you can use single workspace | |
$TERRAFORM_CMD init | |
$TERRAFORM_CMD workspace list | |
$TERRAFORM_CMD workspace select $ENV_PREFIX || $TERRAFORM_CMD workspace new $ENV_PREFIX | |
$TERRAFORM_CMD validate | |
;; | |
apply) | |
$TERRAFORM_CMD init | |
$TERRAFORM_CMD workspace list | |
$TERRAFORM_CMD workspace select $ENV_PREFIX || $TERRAFORM_CMD workspace new $ENV_PREFIX | |
$TERRAFORM_CMD init | |
#$TERRAFORM_CMD validate && $TERRAFORM_CMD apply --auto-approve | |
$TERRAFORM_CMD validate && ($TERRAFORM_CMD apply || $TERRAFORM_CMD apply --auto-approve) | |
;; | |
destroy) | |
$TERRAFORM_CMD init | |
$TERRAFORM_CMD workspace list | |
$TERRAFORM_CMD workspace select $ENV_PREFIX || $TERRAFORM_CMD workspace new $ENV_PREFIX | |
$TERRAFORM_CMD init | |
$TERRAFORM_CMD validate && ($TERRAFORM_CMD destroy || ($TERRAFORM_CMD destroy --auto-approve || (sh -x ./clear_script.sh && $TERRAFORM_CMD validate && $TERRAFORM_CMD destroy --auto-approve))) | |
;; | |
*) | |
echo "wrong choice" | |
;; | |
esac | |
''' | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment