Skip to content

Commit

Permalink
Merge pull request #9 from aigisuk/develop
Browse files Browse the repository at this point in the history
🤖 Update terraform github actions
  • Loading branch information
colinwilson authored Apr 27, 2021
2 parents 497efa6 + e61f126 commit 562cb81
Show file tree
Hide file tree
Showing 6 changed files with 465 additions and 13 deletions.
156 changes: 144 additions & 12 deletions .github/workflows/terraform.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,155 @@
name: 'Terraform GitHub Actions'

on:
push:
branches:
- release
pull_request:

jobs:
terraform:
name: 'Terraform Format'
runs-on: ubuntu-20.04
terraform-fmt:
name: Terraform Format
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1

- name: Terraform Format
id: fmt
run: terraform fmt -diff -check -no-color -recursive
continue-on-error: true

defaults:
run:
shell: bash
- uses: actions/github-script@v4
if: github.event_name == 'pull_request' && steps.fmt.outputs.exitcode != 0
env:
TF_FMT_STDOUT: "${{ steps.fmt.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style 🖌 - \`${{ steps.fmt.outcome }}\`
\`\`\`diff
${process.env.TF_FMT_STDOUT}
\`\`\`
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
throw "failed to run `terraform fmt -check -recursive -diff`"
terraform-plan:
name: Terraform Plan
needs: terraform-fmt
runs-on: ubuntu-latest
env:
WORKING_DIR: "examples/default_deployment"
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/[email protected]

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: Terraform Init
id: init
run: terraform init -no-color
working-directory: ${{ env.WORKING_DIR }}
continue-on-error: true

- uses: actions/github-script@v4
if: github.event_name == 'pull_request' && steps.init.outputs.exitcode != 0
env:
TF_INIT_STDERR: "${{ steps.init.outputs.stderr }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Initialization ⚙️ - \`${{ steps.init.outcome }}\`
\`\`\`${ process.env.TF_INIT_STDERR }\`\`\`
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workdir: \`${{ env.WORKING_DIR }}\`, Workflow: \`${{ github.workflow }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
throw "failed to run `terraform init`"
- name: Terraform Validate
id: validate
run: terraform validate -no-color
working-directory: ${{ env.WORKING_DIR }}
continue-on-error: true

- uses: actions/github-script@v4
if: github.event_name == 'pull_request' && steps.validate.outputs.exitcode != 0
env:
TF_VAL_STDERR: "${{ steps.validate.outputs.stderr }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Initialization ⚙️ - \`${{ steps.init.outcome }}\`
#### Terraform Validate 📃 - \`${{ steps.validate.outcome }}\`
\`\`\`${ process.env.TF_VAL_STDERR }\`\`\`
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workdir: \`${{ env.WORKING_DIR }}\`, Workflow: \`${{ github.workflow }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
throw "failed to run `terraform validate`"
- name: Terraform Plan
id: plan
run: terraform plan -no-color
working-directory: ${{ env.WORKING_DIR }}
continue-on-error: true

- uses: actions/github-script@v4
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Initialization ⚙️ - \`${{ steps.init.outcome }}\`
#### Terraform Validate 📃 - \`${{ steps.validate.outcome }}\`
#### Terraform Plan 📖 - \`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`${process.env.PLAN}\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workdir: \`${{ env.WORKING_DIR }}\`, Workflow: \`${{ github.workflow }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

- name: Terraform Format
run: terraform fmt -check
# - name: Terraform Apply
# id: apply
# if: github.ref == 'refs/heads/release' && github.event_name == 'push'
# run: terraform apply -auto-approve
# working-directory: ${{ env.WORKING_DIR }}
9 changes: 9 additions & 0 deletions examples/default_deployment/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
terraform {
# Reconfigure the backend block to suit your needs
backend "remote" {
hostname = "app.terraform.io"
organization = "AIGISUK"

workspaces {
name = "gh-actions-terraform-digitalocean-ha-k3s"
}
}
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
Expand Down
3 changes: 3 additions & 0 deletions examples/default_deployment/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "cluster_summary" {
value = module.ha-k3s.cluster_summary
}
Loading

0 comments on commit 562cb81

Please sign in to comment.