Skip to content

Instantly share code, notes, and snippets.

@B0yc3y
Created November 8, 2018 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save B0yc3y/1909c802a0e24c534692c4f6ca43b976 to your computer and use it in GitHub Desktop.
Save B0yc3y/1909c802a0e24c534692c4f6ca43b976 to your computer and use it in GitHub Desktop.
Example Terraform Gitlab CI/CD Pipeline
# NOTE: A REMOTE BACKEND IS NEEDED FOR TF CI/CD TO WORK
#This file is a template, and might need editing before it works on your project.
# Official image for Hashicorp's Terraform. It uses an image which is Alpine
# based as it is much lighter.
#
# Entrypoint is also needed as image by default set `terraform` binary as an
# entrypoint.
image:
name: hashicorp/terraform:light
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
# Default output file for Terraform plan
variables:
PLAN: plan.tfplan
before_script:
- mkdir credentials # Probably a better way to provide credentials, currently using Gitlab ci/cd variables
- echo "$TF_SVC_ACCOUNT" > credentials/open-mss-poc-terraform.json
- terraform --version
- terraform init -var -force-copy . ## Force copy is there to ensure your state is moved from local to remote.
stages:
- validate
- build
- deploy
- destroy
validate:
tags:
- consultancy
- shared
stage: validate
script:
- terraform validate
plan:
tags:
- consultancy
- shared
stage: build
script:
- terraform plan -out=$PLAN
artifacts:
name: plan
paths:
- $PLAN
apply:
tags:
- consultancy
- shared
stage: deploy
environment:
name: stage
script:
- terraform apply -input=false $PLAN
dependencies:
- plan
only: # ensure we only deploy from master
- master
destroy:
tags:
- consultancy
- shared
stage: destroy
environment:
name: stage
script:
- terraform destroy --auto-approve # tear everything down. (good for demo/poc envirionments)
dependencies:
- plan
when: manual # Create a button to use manually in the gitlab UI
only: # Only allow destroy if master
- master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment