Skip to content

Instantly share code, notes, and snippets.

@freuds
Created August 11, 2020 15:39
Show Gist options
  • Save freuds/65c62795c1a83bf0f63bc9228ac3b557 to your computer and use it in GitHub Desktop.
Save freuds/65c62795c1a83bf0f63bc9228ac3b557 to your computer and use it in GitHub Desktop.
# Define environment
dist: xenial # Ubuntu 16.04
language: bash
# Only build pushes to master branch
branches:
only:
- master
# Export variables
env:
- tf_version=0.12.19 tf_init_cli_options="-input=false" tf_validation_cli_options="" tf_plan_cli_options="-lock=false -input=false" tf_apply_cli_options="-auto-approve -input=false"
# Install dependencies
before_install:
- wget https://releases.hashicorp.com/terraform/"$tf_version"/terraform_"$tf_version"_linux_amd64.zip
- unzip terraform_"$tf_version"_linux_amd64.zip
- sudo mv terraform /usr/local/bin/
- rm terraform_"$tf_version"_linux_amd64.zip
# Terraform Plan and Apply
# Plan only on pull requests, Apply on merged code
jobs:
include:
- stage: terraform plan
# Only run terraform validate and plan state if within a pull request
if: type IN (pull_request, api)
script:
- echo "Validating Terraform fmt"
- terraform fmt -recursive -check
- echo "Pull request detected, creating change plan"
- terraform init $tf_init_cli_options
- terraform validate $tf_validation_cli_options
- terraform plan $tf_plan_cli_options
- stage: terraform apply
# Only run terraform apply stage if outside of a pull request
if: type IN (push) and branch = master
script:
- echo "Merge detected, executing changes"
- terraform init $tf_init_cli_options
- terraform apply $tf_apply_cli_options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment