Skip to content

Commit b633c2d

Browse files
Merge pull request #1 from florenciacomuzzi/feature
add env
2 parents 27c2d45 + a5fa2fb commit b633c2d

File tree

13 files changed

+193
-154
lines changed

13 files changed

+193
-154
lines changed

.github/workflows/terraform-apply.yml

Lines changed: 0 additions & 40 deletions
This file was deleted.

.github/workflows/terraform-plan.yml

Lines changed: 0 additions & 106 deletions
This file was deleted.

.github/workflows/terraform.yml

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
name: "Terraform CICD"
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
- develop
9+
# paths:
10+
# - terraform/**
11+
pull_request:
12+
# branches:
13+
# - main
14+
# - develop
15+
# paths:
16+
# - terraform/**
17+
18+
env:
19+
# verbosity setting for Terraform logs
20+
TF_LOG: INFO
21+
# # Credentials for deployment to AWS
22+
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
23+
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
24+
# # S3 bucket for the Terraform state
25+
# BUCKET_TF_STATE: ${{ secrets.BUCKET_TF_STATE}}
26+
27+
jobs:
28+
terraform:
29+
name: "Terraform Infrastructure Change Management"
30+
runs-on: ubuntu-latest
31+
permissions:
32+
pull-requests: write # Required for creating or updating PRs
33+
contents: write # Required for modifying files (if applicable)
34+
defaults:
35+
run:
36+
shell: bash
37+
# We keep Terraform files in the terraform directory.
38+
# working-directory: ./terraform
39+
40+
steps:
41+
- name: Checkout the repository to the runner
42+
uses: actions/checkout@v2
43+
44+
- id: 'auth'
45+
name: 'Authenticate to Google Cloud'
46+
uses: 'google-github-actions/auth@v2'
47+
with:
48+
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
49+
50+
- name: 'Set up Cloud SDK'
51+
uses: 'google-github-actions/setup-gcloud@v2'
52+
53+
- name: Setup Terraform with specified version on the runner
54+
uses: hashicorp/setup-terraform@v2
55+
with:
56+
terraform_version: 1.3.0
57+
58+
- name: Terraform init [pull_request]
59+
id: init-pr
60+
if: github.event_name == 'pull_request'
61+
run: |
62+
if [ "${{ github.event.pull_request.base.ref }}" == "develop" ]; then
63+
export ENV="dev";
64+
elif [ "${{ github.event.pull_request.base.ref }}" == "main" ]; then
65+
export ENV="prod";
66+
else
67+
echo "unsupported environment";
68+
fi
69+
terraform init -upgrade -backend-config=backend/$ENV.tfvars --reconfigure
70+
71+
- name: Terraform init [push]
72+
id: init-push
73+
if: github.event_name == 'push'
74+
run: |
75+
if [ "${{ github.ref }}" == "develop" ]; then
76+
export ENV="dev";
77+
elif [ "${{ github.ref }}" == "main" ]; then
78+
export ENV="prod";
79+
else
80+
echo "unsupported environment";
81+
fi
82+
terraform init -upgrade -backend-config=backend/$ENV.tfvars --reconfigure
83+
84+
- name: Terraform format
85+
id: fmt
86+
run: terraform fmt -check
87+
88+
- name: Terraform validate
89+
id: validate
90+
run: terraform validate
91+
92+
- name: Terraform plan [pull_request]
93+
id: plan-pr
94+
if: github.event_name == 'pull_request'
95+
run: |
96+
if [ "${{ github.event.pull_request.base.ref }}" == "develop" ]; then
97+
export ENV="dev";
98+
elif [ "${{ github.event.pull_request.base.ref }}" == "main" ]; then
99+
export ENV="prod";
100+
else
101+
echo "unsupported environment";
102+
fi
103+
terraform plan -input=false -var-file=variables/$ENV.auto.tfvars -lock=false
104+
continue-on-error: true
105+
106+
- name: Terraform plan [push]
107+
id: plan-push
108+
if: github.event_name == 'push'
109+
run: |
110+
if [ "${{ github.ref }}" == "develop" ]; then
111+
export ENV="dev";
112+
elif [ "${{ github.ref }}" == "main" ]; then
113+
export ENV="prod";
114+
else
115+
echo "unsupported environment";
116+
fi
117+
terraform plan -input=false -var-file=variables/$ENV.auto.tfvars -lock=false
118+
continue-on-error: true
119+
120+
- uses: actions/github-script@v6
121+
if: github.event_name == 'pull_request'
122+
env:
123+
PLAN: "terraform\n${{ steps.plan-pr.outputs.stdout }}"
124+
with:
125+
script: |
126+
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
127+
#### Terraform Initialization ⚙️\`${{ steps.init-pr.outcome }}\`
128+
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
129+
#### Terraform Plan 📖\`${{ steps.plan-pr.outcome }}\`
130+
131+
<details><summary>Show Plan</summary>
132+
133+
\`\`\`\n
134+
${process.env.PLAN}
135+
\`\`\`
136+
137+
</details>
138+
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
139+
140+
github.rest.issues.createComment({
141+
issue_number: context.issue.number,
142+
owner: context.repo.owner,
143+
repo: context.repo.repo,
144+
body: output
145+
})
146+
147+
- name: Terraform Plan Status [pull_request]
148+
if: github.event_name == 'pull_request' && steps.plan-pr.outcome == 'failure'
149+
run: exit 1
150+
151+
- name: Terraform Plan Status
152+
if: github.event_name == 'push' && steps.plan-push.outcome == 'failure'
153+
run: exit 1
154+
155+
- name: Terraform Apply
156+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
157+
run: terraform apply -auto-approve -input=false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ terraform.rc
3939

4040

4141
secret.*
42+
43+
# TODO remove this file
44+
florenciacomuzzi-1cde3fb795c6.json

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
<!-- BEGIN_TF_DOCS -->
12
## Requirements
23

34
| Name | Version |
45
|------|---------|
56
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
6-
| <a name="requirement_linode"></a> [linode](#requirement\_linode) | 2.34.1 |
7+
| <a name="requirement_google"></a> [google](#requirement\_google) | 6.24.0 |
78

89
## Providers
910

1011
| Name | Version |
1112
|------|---------|
12-
| <a name="provider_linode"></a> [linode](#provider\_linode) | 2.34.1 |
13+
| <a name="provider_google"></a> [google](#provider\_google) | 6.24.0 |
1314

1415
## Modules
1516

@@ -19,14 +20,17 @@ No modules.
1920

2021
| Name | Type |
2122
|------|------|
22-
| [linode_instance.web](https://registry.terraform.io/providers/linode/linode/2.34.1/docs/resources/instance) | resource |
23+
| [google_compute_network.vpc_network](https://registry.terraform.io/providers/hashicorp/google/6.24.0/docs/resources/compute_network) | resource |
2324

2425
## Inputs
2526

2627
| Name | Description | Type | Default | Required |
2728
|------|-------------|------|---------|:--------:|
28-
| <a name="input_token"></a> [token](#input\_token) | Linode API Token | `string` | n/a | yes |
29+
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | GCP project id | `string` | `"florenciacomuzzi"` | no |
30+
| <a name="input_region"></a> [region](#input\_region) | GCP region | `string` | `"us-east1"` | no |
31+
| <a name="input_vpc_name"></a> [vpc\_name](#input\_vpc\_name) | VPC network name | `string` | n/a | yes |
2932

3033
## Outputs
3134

3235
No outputs.
36+
<!-- END_TF_DOCS -->

SETUP.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Deployments are triggered from GitHub Actions workflows.
55

66
## Setting up your own project
77

8+
### GCP
9+
* Login to GCP account.
10+
* Create a service account like `k8s-environment-terraform-cicd` to use for CICD.
11+
* Create a service account JSON file.
12+
* Add as a repository secret by going to Settings > Secrets and variables > Actions. Name it GCP_CREDENTIALS and paste in the credentials JSON.
13+
* Create the buckets for Terraform state like `prod-tf-state-bucket`. The bucket names are specified in the `backend/{env}.tfvars` file.
14+
* Go to the bucket > Permissions > Add Member > Service Account > k8s-environment-terraform-cicd@florenciacomuzzi.iam.gserviceaccount.com > Role > Storage Object Admin.
15+
816
### Linode
917
* Login to Linode account.
1018
* Create a personal access token. This secret is the value of "token" input variable of the Terraform module.

backend.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
backend "gcs" {}
3+
}

backend/dev.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bucket = "florenciacomuzzi-dev-tf-state-bucket"
2+
prefix = "k8s/environ"

backend/prod.tfvars

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bucket = "florenciacomuzzi-prod-tf-state-bucket"
2+
prefix = "k8s/environ"

0 commit comments

Comments
 (0)