Skip to content

Instantly share code, notes, and snippets.

@yclian
Last active June 2, 2016 06:28
Show Gist options
  • Save yclian/9720bcd40e9beffdb9c25130f8d7ad28 to your computer and use it in GitHub Desktop.
Save yclian/9720bcd40e9beffdb9c25130f8d7ad28 to your computer and use it in GitHub Desktop.
Terraform on Jenkins
node {
def ak = "<AWS_ACCESS_KEY>"
def id = "${env.JOB_NAME}-${env.BUILD_ID}"
def wd = pwd()
def run = "docker run --rm -u `id -u jenkins` -v ${wd}:/tmp/${id} -w=/tmp/${id} -e AWS_ACCESS_KEY_ID=${ak} -e AWS_SECRET_ACCESS_KEY=${env.ask} --entrypoint=/go/bin/terraform hashicorp/terraform:full"
git credentialsId: '<CREDS_ID>', url: '<GIT_URL>'
withCredentials([[$class: 'StringBinding', credentialsId: '<CREDS_ID_2>', variable: 'ask']]) {
sh "(head -n20 ${wd}/.terraform/terraform.tfstate 2>/dev/null | grep -q remote) || ${run} remote config -backend=s3 -backend-config=bucket=<S3_BUCKET> -backend-config=key=<S3_PATH> -backend-config=region=<S3_REGION>"
stage 'Plan'
sh "${run} plan -var aws_access_key=${ak} -var aws_secret_key=${env.ask}"
input 'Apply the plan?'
stage 'Apply'
sh "${run} apply -var aws_access_key=${ak} -var aws_secret_key=${env.ask}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment