Skip to content

Commit a17eac0

Browse files
author
Edvin N
authored
How to setup terraform CI in github (#60)
* How to setup terraform CI in github
1 parent 08ab73b commit a17eac0

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

docs/xks/operator-guide/github.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
id: github
3+
title: XKF on Github
4+
---
5+
6+
As a XKF user you can use both Azure DevOps and GitHub to store
7+
your terraform and gitops repositories.
8+
9+
In this document we will go through how to use XKF on GitHub focusing
10+
on Infrastructure As Code (IAC) using Terraform.
11+
12+
## Terraform
13+
14+
How to run Terraform plan and apply through a GitHub action workflow.
15+
16+
### Workflow
17+
18+
Just like in azure devops case we have created a
19+
[basic pipeline](https://github.com/XenitAB/azure-devops-templates/terraform-docker-github/README.md)
20+
for easy use.
21+
22+
Below you can find a example pipeline that uses the github action workflow.
23+
Read further down to see how to create the secrets needed to run the pipeline.
24+
25+
You should store this GitHub action in your Terraform repository under `.github/workflows/name.yaml`
26+
27+
```.github/workflows/core.yaml
28+
name: terraform_core
29+
30+
on:
31+
push:
32+
branches:
33+
- main
34+
paths:
35+
- core/**
36+
pull_request:
37+
paths:
38+
- core/**
39+
workflow_dispatch:
40+
inputs:
41+
OPA_BLAST_RADIUS:
42+
description: OPA Blast Radius
43+
required: true
44+
default: "50"
45+
46+
jobs:
47+
terraform:
48+
uses: xenitab/azure-devops-templates/.github/workflows/terraform-docker.yaml@2021.10.1
49+
with:
50+
DIR: core
51+
runs-on: '["self-hosted", "linux"]' # If you don't want to use the default ubuntu-latest
52+
ENVIRONMENTS: |
53+
{
54+
"environments":[
55+
{"name":"dev"},
56+
{"name":"qa"},
57+
{"name":"prod"}
58+
]
59+
}
60+
secrets:
61+
AZURE_CREDENTIALS_DEV: ${{ secrets.AZURE_CREDENTIALS_DEV }}
62+
AZURE_CREDENTIALS_QA: ${{ secrets.AZURE_CREDENTIALS_QA }}
63+
AZURE_CREDENTIALS_PROD: ${{ secrets.AZURE_CREDENTIALS_PROD }}
64+
```
65+
66+
### Azure Service Principal
67+
68+
Create a Service Principal(SP) with the access that terraform requires to perform all the tasks you want.
69+
You can read more about SP creation in our [operator guide](operator-guide.md)
70+
71+
The workflow is using [Azure Login GitHub Action](https://github.com/marketplace/actions/azure-login#configure-deployment-credentials)
72+
to login to Azure. When uploading your SP to GitHub make sure to follow the formatting in the examples.
73+
74+
This is to prevent unnecessary masking of { } in your logs which are in dictionary form.
75+
76+
**For example, do**:
77+
78+
```.json
79+
{"clientId": "00000000-0000-0000-0000-000000000000",
80+
"clientSecret": "super-duper-secret-value",
81+
"subscriptionId": "00000000-0000-0000-0000-000000000000",
82+
"tenantId": "00000000-0000-0000-0000-000000000000"}
83+
```
84+
85+
**instead of**:
86+
87+
```.json
88+
{
89+
"clientId": "00000000-0000-0000-0000-000000000000",
90+
"clientSecret": "super-duper-secret-value",
91+
"subscriptionId": "00000000-0000-0000-0000-000000000000",
92+
"tenantId": "00000000-0000-0000-0000-000000000000"
93+
}
94+
```
95+
96+
Upload the entire json as your github secret.
97+
The workflow uses one secret per environment and we recommend that you follow our namestandard.
98+
The secret name the workflow use is **AZURE_CREDENTIALS_\<ENV\>**, for example **AZURE_CREDENTIALS_DEV**
99+
100+
To upload the secret to github you can use the github UI or you can use the [gh cli](https://github.com/cli/cli) to upload secrets to GitHub.
101+
102+
Assuming that you are storing the SP json data in a file you could do:
103+
104+
```shell
105+
gh secret -R ORG/xks-terraform set AZURE_CREDENTIALS_DEV < dev-secrets.json
106+
gh secret -R ORG/xks-terraform set AZURE_CREDENTIALS_QA < qa-secrets.json
107+
gh secret -R ORG/xks-terraform set AZURE_CREDENTIALS_PROD < prod-secrets.json
108+
```

sidebars.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ module.exports = {
3838
"xks/operator-guide/eks",
3939
"xks/operator-guide/blast-radius",
4040
"xks/operator-guide/operator-guide",
41-
"xks/operator-guide/azure-devops-agents"
41+
"xks/operator-guide/azure-devops-agents",
42+
"xks/operator-guide/github",
4243
]
4344
}
4445
]

0 commit comments

Comments
 (0)