Skip to content

Commit 9e0c309

Browse files
authored
Merge pull request #43 from clouddrove/internal-445
ci: add terraform action workflow
2 parents ceb0a62 + 0ae533e commit 9e0c309

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

.github/workflows/terraform.yml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: 'Terraform Checks'
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-checks:
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+
# The terraform fmt command is used to format your configuration files into a canonical format and style
94+
- name: 'Terraform Format'
95+
uses: 'dflook/terraform-fmt-check@v1'
96+
with:
97+
actions_subcommand: 'fmt'
98+
99+
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
100+
- name: "Terraform Init"
101+
uses: hashicorp/terraform-github-actions@master
102+
with:
103+
tf_actions_subcommand: "init"
104+
tf_actions_version: 1.3.6
105+
tf_actions_working_dir: ${{ inputs.working_directory }}
106+
env:
107+
GITHUB_TOKEN: '${{ secrets.GITHUB }}'
108+
TF_CLI_ARGS: "-backend-config=token=${{ secrets.TF_API_TOKEN }}"
109+
110+
# The terraform validate command validates the configuration files in a directory, referring only to the configuration
111+
- name: 'Terraform validate'
112+
uses: dflook/terraform-validate@v1
113+
with:
114+
tf_actions_working_dir: ${{ inputs.working_directory }}
115+
116+
# Generates an execution plan for Terraform
117+
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes.
118+
- name: Terraform Plan
119+
id: tf-plan
120+
run: |
121+
export exitcode=0
122+
cd ${{ inputs.working_directory }}
123+
if [ -n "${{ inputs.var_file }}" ]; then
124+
terraform plan -detailed-exitcode -no-color -out tfplan --var-file=${{ inputs.var_file }} || export exitcode=$?
125+
else
126+
terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$?
127+
fi
128+
129+
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
130+
131+
if [ $exitcode -eq 1 ]; then
132+
echo Terraform Plan Failed!
133+
exit 1
134+
else
135+
exit 0
136+
fi

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Above example is just a simple example to call workflow from github shared workf
5454
* [Example for scan and push docker image on Dockerhub](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/docker.md#example-for-scan-and-push-docker-image-on-dockerhub)
5555
* [Example for scan and push docker image on ECR](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/docker.md#example-for-scan-and-push-docker-image-on-ecr)
5656
4. [Auto Assign Assignee Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/auto-assignee.md)
57+
5. [Terraform Checks Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md)
58+
* [Example for terraform checks with azure cloud](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md#example-for-terraform-checks-with-azure-cloud)
59+
* [Example for terraform checks with aws cloud](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md#example-for-terraform-checks-with-aws-cloud)
60+
* [Example for terraform checks with digitalocean cloud](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md#example-for-terraform-checks-with-digitalocean-cloud)
5761
5862
## Feedback
5963
If you come accross a bug or have any feedback, please log it in our [issue tracker](https://github.com/clouddrove/github-shared-workflows/issues), or feel free to drop us an email at [[email protected]](mailto:[email protected]).

docs/terraform-checks.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## [Terraform Checks Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/terraform.yml)
2+
3+
This workflow is used to terraform checks. Workflows have been added in `.github/workflows/terraform.yml`
4+
5+
#### Usage
6+
This workflow is used to terraform checks. Workflows have been added in `.github/workflows/terraform.yml`
7+
8+
#### Example with azure cloud
9+
```yaml
10+
name: Terraform Checks
11+
12+
on:
13+
pull_request:
14+
15+
jobs:
16+
terraform:
17+
uses: clouddrove/github-shared-workflows/.github/workflows/terraform.yml@master
18+
secrets:
19+
GITHUB: ${{ secrets.GITHUB }}
20+
DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
21+
with:
22+
provider: 'azurerm'
23+
working_directory: './_example/'
24+
```
25+
#### Example with aws cloud
26+
```yaml
27+
name: Terraform Checks
28+
29+
on:
30+
pull_request:
31+
32+
jobs:
33+
terraform:
34+
uses: clouddrove/github-shared-workflows/.github/workflows/terraform.yml@master
35+
secrets:
36+
GITHUB: ${{ secrets.GITHUB }}
37+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
38+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
39+
AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }}
40+
with:
41+
provider: 'aws'
42+
working_directory: './_example/'
43+
```
44+
#### Example with digitalocean cloud
45+
```yaml
46+
name: Terraform Checks
47+
48+
on:
49+
pull_request:
50+
51+
jobs:
52+
terraform:
53+
uses: clouddrove/github-shared-workflows/.github/workflows/terraform.yml@master
54+
secrets:
55+
GITHUB: ${{ secrets.GITHUB }}
56+
DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
57+
with:
58+
provider: 'digitalocean'
59+
working_directory: './_example/'
60+
```

0 commit comments

Comments
 (0)