-
Notifications
You must be signed in to change notification settings - Fork 1
How to setup terraform CI in github #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--- | ||
id: github | ||
title: XKF on Github | ||
--- | ||
|
||
As a XKF user you can use both Azure DevOps and GitHub to store | ||
your terraform and gitops repositories. | ||
|
||
In this document we will go through how to use XKF on GitHub focusing | ||
on Infrastructure As Code (IAC) using Terraform. | ||
|
||
## Terraform | ||
|
||
How to run Terraform plan and apply through a GitHub action workflow. | ||
|
||
### Workflow | ||
|
||
Just like in azure devops case we have created a | ||
[basic pipeline](https://github.com/XenitAB/azure-devops-templates/terraform-docker-github/README.md) | ||
for easy use. | ||
|
||
Below you can find a example pipeline that uses the github action workflow. | ||
Read further down to see how to create the secrets needed to run the pipeline. | ||
|
||
You should store this GitHub action in your Terraform repository under `.github/workflows/name.yaml` | ||
|
||
```.github/workflows/core.yaml | ||
name: terraform_core | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- core/** | ||
pull_request: | ||
paths: | ||
- core/** | ||
workflow_dispatch: | ||
inputs: | ||
OPA_BLAST_RADIUS: | ||
description: OPA Blast Radius | ||
required: true | ||
default: "50" | ||
|
||
jobs: | ||
terraform: | ||
uses: xenitab/azure-devops-templates/.github/workflows/terraform-docker.yaml@2021.10.1 | ||
with: | ||
DIR: core | ||
runs-on: '["self-hosted", "linux"]' # If you don't want to use the default ubuntu-latest | ||
ENVIRONMENTS: | | ||
{ | ||
"environments":[ | ||
{"name":"dev"}, | ||
{"name":"qa"}, | ||
{"name":"prod"} | ||
] | ||
} | ||
secrets: | ||
AZURE_CREDENTIALS_DEV: ${{ secrets.AZURE_CREDENTIALS_DEV }} | ||
AZURE_CREDENTIALS_QA: ${{ secrets.AZURE_CREDENTIALS_QA }} | ||
AZURE_CREDENTIALS_PROD: ${{ secrets.AZURE_CREDENTIALS_PROD }} | ||
``` | ||
nissessenap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Azure Service Principal | ||
|
||
Create a Service Principal(SP) with the access that terraform requires to perform all the tasks you want. | ||
You can read more about SP creation in our [operator guide](operator-guide.md) | ||
|
||
The workflow is using [Azure Login GitHub Action](https://github.com/marketplace/actions/azure-login#configure-deployment-credentials) | ||
to login to Azure. When uploading your SP to GitHub make sure to follow the formatting in the examples. | ||
|
||
This is to prevent unnecessary masking of { } in your logs which are in dictionary form. | ||
|
||
**For example, do**: | ||
|
||
```.json | ||
{"clientId": "00000000-0000-0000-0000-000000000000", | ||
"clientSecret": "super-duper-secret-value", | ||
"subscriptionId": "00000000-0000-0000-0000-000000000000", | ||
"tenantId": "00000000-0000-0000-0000-000000000000"} | ||
``` | ||
|
||
**instead of**: | ||
|
||
```.json | ||
{ | ||
"clientId": "00000000-0000-0000-0000-000000000000", | ||
"clientSecret": "super-duper-secret-value", | ||
"subscriptionId": "00000000-0000-0000-0000-000000000000", | ||
"tenantId": "00000000-0000-0000-0000-000000000000" | ||
} | ||
``` | ||
|
||
Upload the entire json as your github secret. | ||
The workflow uses one secret per environment and we recommend that you follow our namestandard. | ||
The secret name the workflow use is **AZURE_CREDENTIALS_\<ENV\>**, for example **AZURE_CREDENTIALS_DEV** | ||
|
||
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. | ||
|
||
Assuming that you are storing the SP json data in a file you could do: | ||
|
||
```shell | ||
gh secret -R ORG/xks-terraform set AZURE_CREDENTIALS_DEV < dev-secrets.json | ||
gh secret -R ORG/xks-terraform set AZURE_CREDENTIALS_QA < qa-secrets.json | ||
gh secret -R ORG/xks-terraform set AZURE_CREDENTIALS_PROD < prod-secrets.json | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.