Skip to content

Commit ccb6f53

Browse files
ci: add terraform action workflow
1 parent ceb0a62 commit ccb6f53

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

.github/workflows/terraform.yml

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
name: 'Terraform GitHub Actions'
2+
on:
3+
workflow_call:
4+
inputs:
5+
working_directory:
6+
required: true
7+
type: string
8+
default: _example
9+
description: 'Root directory of the terraform where all resources exist.'
10+
provider:
11+
required: true
12+
type: string
13+
default: azurerm
14+
description: 'Cloud provider to run the workflow. e.g. azurerm, aws or Digitalocean'
15+
aws_region:
16+
required: false
17+
type: string
18+
default: us-east-1
19+
description: 'AWS region of terraform deployment.'
20+
var_file:
21+
required: false
22+
default: ""
23+
type: string
24+
description: 'Terraform var file directory. e.g. vars/dev.tfvars'
25+
secrets:
26+
AZURE_CREDENTIALS:
27+
required: false
28+
description: 'Azure Credentials to install Azure in github runner.'
29+
AWS_ACCESS_KEY_ID:
30+
required: false
31+
description: 'AWS Access Key ID to install AWS CLI.'
32+
AWS_SECRET_ACCESS_KEY:
33+
required: false
34+
description: 'AWS Secret access key to install AWS CLI'
35+
AWS_SESSION_TOKEN:
36+
required: false
37+
description: 'AWS Session Token to install AWS CLI'
38+
DIGITALOCEAN_ACCESS_TOKEN:
39+
required: false
40+
description: 'Digitalocean access Token to install Digitalocean CLI'
41+
GITHUB:
42+
required: true
43+
description: 'PAT of the user to run the jobs.'
44+
TF_API_TOKEN:
45+
required: false
46+
description: 'Terraform cloud token if your backend is terraform cloud.'
47+
48+
jobs:
49+
terraform-plan:
50+
name: 'Terraform Validate, Init and Plan'
51+
runs-on: ubuntu-latest
52+
env:
53+
#this is needed since we are running terraform with read-only permissions
54+
ARM_SKIP_PROVIDER_REGISTRATION: true
55+
outputs:
56+
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}
57+
58+
steps:
59+
# Checkout the repository to the GitHub Actions runner
60+
- name: Checkout
61+
uses: actions/checkout@v3
62+
63+
# install AWS-cli
64+
- name: Install AWS CLI
65+
if: ${{ inputs.provider == 'aws' }}
66+
uses: aws-actions/configure-aws-credentials@v2
67+
with:
68+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
69+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
70+
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
71+
aws-region: ${{ inputs.aws_region }}
72+
73+
# Install azure-cli
74+
- name: Install Azure CLI
75+
if: ${{ inputs.provider == 'azurerm' }}
76+
uses: azure/login@v1
77+
with:
78+
creds: ${{ secrets.AZURE_CREDENTIALS }}
79+
80+
# Install digitalocean-cli
81+
- name: Install doctl
82+
if: ${{ inputs.provider == 'digitalocean' }}
83+
uses: digitalocean/action-doctl@v2
84+
with:
85+
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
86+
87+
# Install the latest version of the Terraform CLI
88+
- name: Setup Terraform
89+
uses: hashicorp/setup-terraform@v2
90+
with:
91+
terraform_wrapper: false
92+
93+
- name: 'Terraform Format'
94+
uses: 'dflook/terraform-fmt-check@v1'
95+
with:
96+
actions_subcommand: 'fmt'
97+
98+
# Run some scripts
99+
- name: Run shell commands
100+
run: ls -la
101+
102+
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
103+
- name: "Terraform Init"
104+
uses: hashicorp/terraform-github-actions@master
105+
with:
106+
tf_actions_subcommand: "init"
107+
tf_actions_version: 1.3.6
108+
tf_actions_working_dir: ${{ inputs.working_directory }}
109+
env:
110+
GITHUB_TOKEN: '${{ secrets.GITHUB }}'
111+
TF_CLI_ARGS: "-backend-config=token=${{ secrets.TF_API_TOKEN }}"
112+
113+
- name: 'Terraform validate'
114+
uses: dflook/terraform-validate@v1
115+
with:
116+
tf_actions_working_dir: ${{ inputs.working_directory }}
117+
118+
# Generates an execution plan for Terraform
119+
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes.
120+
- name: Terraform Plan
121+
id: tf-plan
122+
run: |
123+
export exitcode=0
124+
cd ${{ inputs.working_directory }}
125+
if [ -n "${{ inputs.var_file }}" ]; then
126+
terraform plan -detailed-exitcode -no-color -out tfplan --var-file=${{ inputs.var_file }} || export exitcode=$?
127+
else
128+
terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$?
129+
fi
130+
131+
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
132+
133+
if [ $exitcode -eq 1 ]; then
134+
echo Terraform Plan Failed!
135+
exit 1
136+
else
137+
exit 0
138+
fi

0 commit comments

Comments
 (0)