Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Use third-party actions for terraform workflow #3

Merged
merged 3 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/apply.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: apply

on:
push:
branches:
- main

env:
AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}"
AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}"
AWS_DEFAULT_REGION: us-east-1


jobs:
apply:
name: apply
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: checkout
uses: actions/checkout@v2

- name: terraform apply
uses: dflook/terraform-apply@v1
with:
path: .
76 changes: 10 additions & 66 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
@@ -1,79 +1,23 @@
name: Terraform
---
name: test

on:
push:
branches:
- main
pull_request:

env:
AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}"
AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}"
AWS_DEFAULT_REGION: us-east-1


jobs:
terraform:
name: terraform
test:
name: test
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2

- name: setup terraform
uses: hashicorp/setup-terraform@v1
- name: terraform fmt
uses: dflook/terraform-fmt-check@v1
with:
terraform_version: ~0.12.0

- name: init
run: terraform init
path: .

- name: fmt
run: terraform fmt -check

- name: plan
id: plan
run: |
set -o pipefail
tfplan_file=$(mktemp)
echo "::set-output name=tfplan::$tfplan_file"
terraform plan -out=$tfplan_file -no-color
continue-on-error: true # Make sure to post the error to the PR

- name: publish plan to PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v3
env:
PLAN: "${{ steps.plan.outputs.stdout }}"
- name: terraform validate
uses: dflook/terraform-validate@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`terraform
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});
- name: check plan status
if: steps.plan.outcome != 'success'
run: |
echo "Check plan output."
exit 1
# On push to main, build or change infrastructure according to
# Terraform configuration files
- name: apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply ${{ steps.plan.outputs.tfplan }}
path: .
21 changes: 21 additions & 0 deletions .github/workflows/drift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: check

on:
schedule:
- cron: "0 8 * * *"

jobs:
check_drift:
runs-on: ubuntu-latest
name: Check for drift
steps:
- name: checkout
uses: actions/checkout@v2

- name: check
uses: dflook/terraform-check@v1
with:
path: .

# TODO open an issue on failure
26 changes: 26 additions & 0 deletions .github/workflows/plan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
name: plan

on:
pull_request:

env:
AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}"
AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}"
AWS_DEFAULT_REGION: us-east-1


jobs:
plan:
name: plan
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: checkout
uses: actions/checkout@v2

- name: terraform plan
uses: dflook/terraform-plan@v1
with:
path: .
5 changes: 4 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
provider "aws" {
region = "us-east-1"
region = "us-east-1"
version = "~> 2.70.0"
}

terraform {
Expand All @@ -10,6 +11,8 @@ terraform {
encrypt = true
dynamodb_table = "datagov-lock-table"
}

required_version = "~> 0.12.0"
}

# https://github.com/18F/aws-admin/blob/035c2dc740fe80cf0e6d5a9cb800cf39bd18d34b/terraform/iam/base.tf#L121-L131
Expand Down
4 changes: 2 additions & 2 deletions user/main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
resource "aws_iam_user" "default" {
name = var.name
name = var.name

# Ensure we can delete the user if non-terraform MFA or login profiles are
# created.
force_destroy = true
}

resource "aws_iam_user_group_membership" "default" {
user = aws_iam_user.default.name
user = aws_iam_user.default.name
groups = var.groups
}
2 changes: 1 addition & 1 deletion user/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ variable "name" {
}

variable "groups" {
default = []
default = []
description = "List of IAM groups to assign the user to."
}