Skip to content

Commit

Permalink
Merge pull request #32 from kelvinshammai/feat/unifica-pipelines-terr…
Browse files Browse the repository at this point in the history
…aform

Unifica pipelines terraform
  • Loading branch information
lgfa29 authored Aug 31, 2022
2 parents 5d12164 + 1257a9b commit 6f3918f
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 108 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/terraform.yaml
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
66 changes: 0 additions & 66 deletions .github/workflows/terraform_executa_modulo_aws.yaml

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/test_terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ jobs:
uses: "./.github/workflows/terraform_valida_modulo.yaml"
with:
working_directory: tests/terraform/test-required-version/test-004
test-terraform:
uses: "./.github/workflows/terraform.yaml"
with:
working_directory: tests/terraform/workspace-plan-apply
test-terraform-plan:
uses: "./.github/workflows/terraform.yaml"
with:
working_directory: tests/terraform/workspace-plan-apply
plan: true
test-terraform-plan-apply:
uses: "./.github/workflows/terraform.yaml"
with:
working_directory: tests/terraform/workspace-plan-apply
plan: true
apply: true
test-terraform-plan-apply-workspace:
uses: "./.github/workflows/terraform.yaml"
with:
working_directory: tests/terraform/workspace-plan-apply
plan: true
apply: true
workspace: "dev"
40 changes: 40 additions & 0 deletions docs/terraform.md
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
```
42 changes: 0 additions & 42 deletions docs/terraform_executa_modulo_aws.md

This file was deleted.

3 changes: 3 additions & 0 deletions tests/terraform/workspace-plan-apply/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.terraform.lock.hcl
.terraform/providers/
**/terraform.tfstate.backup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default
5 changes: 5 additions & 0 deletions tests/terraform/workspace-plan-apply/main.tf
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
}
8 changes: 8 additions & 0 deletions tests/terraform/workspace-plan-apply/terraform.tfstate
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": []
}
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": []
}

0 comments on commit 6f3918f

Please sign in to comment.