Skip to content

Instantly share code, notes, and snippets.

@YannMjl
Created August 16, 2021 21:02
Show Gist options
  • Save YannMjl/bd42ec21302a2c21c12253faf1687503 to your computer and use it in GitHub Desktop.
Save YannMjl/bd42ec21302a2c21c12253faf1687503 to your computer and use it in GitHub Desktop.
Github action job to automate provisioning on GCP with Terraform
# this workflow job is used to automate provision of servers and resources
# for Google Cloud Project with terraform
# author: Yann Mulonda
name: 'Terraform'
on:
push:
branches:
- terraform
pull_request:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
terraform:
name: 'Provision GCP'
runs-on: ubuntu-latest
environment: production
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
defaults:
run:
shell: bash
# On push to terraform branch, build or change infrastructure according to Terraform configuration files
if: github.ref == 'refs/heads/terraform' && github.event_name == 'push'
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v2
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform init
# # Checks that all Terraform configuration files adhere to a canonical format
# - name: Terraform Format
# run: terraform fmt -check
# Setup gcloud CLI
- name: setup gcloud CLI
uses: google-github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GCP_PROJECT }}
# this is the json key of the service account created on the project
service_account_key: ${{ secrets.GCP_CREDENTIALS }}
export_default_credentials: true
# Generates an execution plan for Terraform
# - name: Terraform Plan
# run: terraform plan
# Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud".
# See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
- name: Terraform Apply
# If any commit message in your push or the HEAD commit of your PR
# contains the strings <construire> -- then terraform Apply step will run
if: "contains(github.event.commits[0].message, '[construire]')"
# terraform plan
run: |
terraform apply -auto-approve
- name: Terraform Destroy
# If any commit message in your push or the HEAD commit of your PR
# contains the strings [detruire] -- then terraform Destry step will run
if: "contains(github.event.commits[0].message, '[detruire]')"
# terraform plan -destroy
run: |
terraform apply -destroy -auto-approve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment