-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from kelvinshammai/feat/unifica-pipelines-terr…
…aform Unifica pipelines terraform
- Loading branch information
Showing
10 changed files
with
176 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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,89 @@ | ||
name: "Terraform Executa Modulo" | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
os_version: | ||
description: "Versão do sistema operacional" | ||
required: false | ||
default: "ubuntu-20.04" | ||
type: string | ||
workspace: | ||
description: "Define o workspace em que o modulo será executado" | ||
required: false | ||
default: "" | ||
type: string | ||
plan: | ||
description: "Define se será executado ou não o step Terraform Plan" | ||
required: false | ||
default: false | ||
type: boolean | ||
apply: | ||
description: "Define se será executado ou não o step Terraform Apply" | ||
required: false | ||
default: false | ||
type: boolean | ||
working_directory: | ||
description: "Diretório onde a pipeline irá atuar" | ||
required: false | ||
default: "." | ||
type: string | ||
|
||
jobs: | ||
terraform: | ||
name: Terraform workflow | ||
runs-on: ${{ inputs.os_version }} | ||
defaults: | ||
run: | ||
working-directory: ${{ inputs.working_directory }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
# É esperado que secrets sejam herdados do repositório que está chamando | ||
# a pipeline e que esses secrets serão usados para configurar providers | ||
# to Terraform usando variáveis de ambiente. | ||
# Esse step irá export todos os secrets disponíveis como variáveis de | ||
# ambiente para os passos seguintes. | ||
- uses: mentoriaiac/secrets-to-env-action@v1 | ||
with: | ||
secrets: ${{ toJSON(secrets) }} | ||
|
||
- name: Lê versão do Terraform | ||
id: tf-version | ||
run: | | ||
TF_VERSION=$(sed -nr 's/\s*required_version\s+=\s+"(.*)"/\1/p' *.tf) | ||
echo "::set-output name=version::$TF_VERSION" | ||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v1 | ||
with: | ||
terraform_version: ${{ steps.tf-version.outputs.version }} | ||
|
||
- name: Terraform Version | ||
run: terraform version | ||
|
||
- name: Terraform Format | ||
run: terraform fmt -check -diff | ||
|
||
- name: Terraform Workspace | ||
if: ${{ inputs.workspace != '' }} | ||
run: terraform workspace select ${{ inputs.workspace }} | ||
|
||
- name: Terraform Init | ||
run: terraform init | ||
|
||
- name: Terraform Validate | ||
run: terraform validate | ||
|
||
- name: Validação do tfsec | ||
run: | | ||
docker run --rm -v $PWD:/app -w /app tfsec/tfsec . | ||
- name: Terraform Plan | ||
if: ${{ inputs.plan }} | ||
run: terraform plan -out tfplan | ||
|
||
- name: Terraform Apply | ||
if: ${{ inputs.plan && inputs.apply }} | ||
run: terraform apply tfplan |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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,40 @@ | ||
# Terraform Executa Modulo | ||
Github Actions para ser reutilizado nos projetos que utilizam Terraform, com a finalidade de validar a sintaxe do código e/ou criar uma infra baseada em um modulo. | ||
|
||
## Inputs | ||
| Nome | Descrição | Requirida | Default | | ||
|------|-----------|-----------|---------| | ||
| `apply` | Define se o step terraform apply será executado | não | `false` | | ||
| `os_version` | Versão do sistema operacional | não | `"ubuntu-20.04"` | | ||
| `plan` | Define se o step terraform plan será executado | não | `false` | | ||
| `working_directory` | Define o diretório onde a pipeline irá atuar | não | `"."` | | ||
| `workspace` | Seleciona o Workspace | não | `""` | | ||
|
||
## Secrets | ||
|
||
Herda a secrets existentes no repositório que utiliza este workflow. A principal função é configurar as variáveis de ambientes necessárias para executar o modulo terraform. | ||
|
||
## Utilizando | ||
Criar a seguintes estrutura de diretórios: | ||
|
||
`.github/workflows/<proposito>.yml` | ||
|
||
Utilize o exemplo abaixo para seu pipeline de CI: | ||
|
||
```yaml | ||
name: "Terraform Valida e Executa Modulo" | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
terraform: | ||
uses: "mentoriaiac/cicd_centralizado/.github/workflows/terraform.yaml@v1" | ||
with: | ||
plan: true | ||
apply: true | ||
workspace: "prod" | ||
secrets: inherit | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains 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,3 @@ | ||
.terraform.lock.hcl | ||
.terraform/providers/ | ||
**/terraform.tfstate.backup |
This file contains 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 @@ | ||
default |
This file contains 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,5 @@ | ||
resource "null_resource" "global" {} | ||
|
||
resource "null_resource" "only_dev" { | ||
count = terraform.workspace == "dev" ? 1 : 0 | ||
} |
This file contains 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,8 @@ | ||
{ | ||
"version": 4, | ||
"terraform_version": "1.1.4", | ||
"serial": 3, | ||
"lineage": "bf85634d-9a64-50be-9be5-f3c9e6855883", | ||
"outputs": {}, | ||
"resources": [] | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/terraform/workspace-plan-apply/terraform.tfstate.d/dev/terraform.tfstate
This file contains 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,8 @@ | ||
{ | ||
"version": 4, | ||
"terraform_version": "1.1.4", | ||
"serial": 4, | ||
"lineage": "d56b2b0b-d036-d2e0-7814-daca9c040ad4", | ||
"outputs": {}, | ||
"resources": [] | ||
} |