Skip to content

Commit

Permalink
Merge pull request #3 from byu-oit/json_file
Browse files Browse the repository at this point in the history
trying out a file pattern instead of passing in the json string directly
  • Loading branch information
yoshutch authored Jun 26, 2020
2 parents 4c14696 + 87828a7 commit 86a0e84
Show file tree
Hide file tree
Showing 10 changed files with 1,498 additions and 171 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: "build"
on:
pull_request:
push:
branches:
- master
- 'v*'

jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: |
yarn install
yarn run all
111 changes: 76 additions & 35 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,86 @@
name: "build-test"
on: # rebuild any PRs and main branch changes
name: "test"
on:
pull_request:
push:
branches:
- master
- 'releases/*'
- 'v*'

jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: |
yarn install
yarn run all
test: # make sure the action works on a clean machine without building only on PRs
if: ${{ github.event_name == 'pull_request' }}
test-action: # make sure the action works on a clean machine without building
name: Test normal PR use
runs-on: ubuntu-latest
env:
tf_version: 0.12.27
steps:
- uses: actions/checkout@v1
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.tf_version }}
- run: terraform init
working-directory: __tests__
- run: terraform plan -out test-plan.tfplan
working-directory: __tests__
- uses: ./
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
terraform_plan_json: |
{
"resource_changes": [
{
"address": "module.app.module.database.aws_db_instance.database",
"type": "aws_db_instance",
"name": "database",
"change": {
"actions": ["delete"]
}
},
{
"address": "module.app.aws_security_group_rule.db_access",
"type": "aws_security_group_rule",
"name": "db_access",
"change": {
"actions": ["delete", "create"]
}
}
]
}
github-token: ${{ secrets.GITHUB_TOKEN }}
working-directory: __tests__
terraform-plan-file: test-plan.tfplan

test-action-plan-in-diff-dir: # make sure the action works with plan in different directory from working dir
name: Test TF plan in different dir
runs-on: ubuntu-latest
env:
tf_version: 0.12.27
steps:
- uses: actions/checkout@v1
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.tf_version }}
- run: terraform init
working-directory: __tests__
- run: terraform plan -out ../test-plan.tfplan
working-directory: __tests__
- uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
working-directory: __tests__
terraform-plan-file: ../test-plan.tfplan

test-action-without-tf-wrapper: # make sure the action works without the terraform wrapper
name: Test without TF wrapper
runs-on: ubuntu-latest
env:
tf_version: 0.12.27
steps:
- uses: actions/checkout@v1
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.tf_version }}
terraform_wrapper: false
- run: terraform init
working-directory: __tests__
- run: terraform plan -out test-plan.tfplan
working-directory: __tests__
- uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
working-directory: __tests__
terraform-plan-file: test-plan.tfplan

test-action-at-root: # make sure the action works with terraform dir as root dir
name: Test from Root
runs-on: ubuntu-latest
env:
tf_version: 0.12.27
steps:
- uses: actions/checkout@v1
- run: mv __tests__/* .
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: ${{ env.tf_version }}
- run: terraform init
- run: terraform plan -out test-plan.tfplan
- uses: ./
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
terraform-plan-file: test-plan.tfplan
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ __tests__/runner/*
lib/**/*

.idea
*.iml
*.iml

!__tests__/main.test.ts
!__tests__/test.tf
__tests__/
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<p align="center">
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
</p>
![build](https://github.com/byu-oit/github-action-tf-plan-comment/workflows/build/badge.svg)
![test](https://github.com/byu-oit/github-action-tf-plan-comment/workflows/test/badge.svg)

# ![BYU logo](https://www.hscripts.com/freeimages/logos/university-logos/byu/byu-logo-clipart-128.gif) github-action-tf-plan-comment

GitHub Action to make a comment on a pull request with the proposed updated terraform plan

This action takes in a JSON representation of your terraform plan and creates a comment on the Pull Request (PR) with basic info about what the plan will create, update, replace, or delete.
This action takes in the terraform plan and creates a comment on the Pull Request (PR) with basic info about what the plan will create, update, replace, or delete.

**Note:** this action does not run terraform plan for you, you must pass in the plan as an input.
**Note:** this action does not run `terraform plan` for you, you must pass in the plan as an input as well as the directory of the terraform configuration (where the plan and .terraform dir are located after `terraform init`).

## Usage
```yaml
Expand All @@ -19,34 +18,33 @@ jobs:
runs-on: ubuntu-latest
steps:
# ...
- name: Terraform Plan JSON
id: json_plan
run: terraform show -json plan
# terraform init
# terraform plan
- name: Comment Terraform Plan
uses: byu-oit/github-action-tf-plan-comment@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
terraform_plan_json: ${{ steps.json_plan.outputs.stdout }}
github-token: ${{ secrets.GITHUB_TOKEN }}
working-directory: terraform-iac/dev/app # where your terraform files are
terraform-plan-file: plan.tfplan # relative to working directory
```
**Note:** make sure you run your `terraform show-json plan` in the same working directory as the `terraform plan` step, and make sure you
## Inputs
* `github-token` - (**required**) pass in the GitHub token to make comments on the PR
* `working-directory` - (_optional_) the directory of the terraform configuration files (defaults to `.`)
* `terraform-plan-file` - (**required**) Filename of the terraform plan (relative to `working-directory`)

## Output
This action will create a comment on your PR like:

> ## Terraform Plan:
> will replace (delete then create) 1 resources:
> will **replace (delete then create)** 1 resources:
> - aws_security_group_rule - db_access
>
> will delete 1 resources:
> will **delete** 1 resources:
> - aws_db_instance - database
>
>[see details](link to the github action workflow)


## Inputs
* `github_token` - (**required**) pass in the GitHub token to make comments on the PR
* `terraform_plan_json` - (**required**) JSON representation of the terraform plan to be executed

## Contributing
Hopefully this is useful to others at BYU.
Feel free to ask me some questions about it, but I make no promises about being able to commit time to support it.
Expand Down
10 changes: 10 additions & 0 deletions __tests__/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "local_file" "fake_file" {
filename = "test.txt"
content = "Hello ${random_pet.name.id}"
}

resource "random_pet" "name"{
keepers = {
uuid = uuid()
}
}
10 changes: 7 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ name: 'Terraform Plan Comment'
description: 'Creates a comment on a pull request with the terraform plan'
author: 'Brigham Young University'
inputs:
github_token:
github-token:
required: true
description: 'github token'
terraform_plan_json:
terraform-plan-file:
required: true
description: JSON of the terraform plan
description: File name of the terraform plan
working-directory:
required: false
description: Directory of the terraform configuration
default: .
runs:
using: 'node12'
main: 'dist/index.js'
Loading

0 comments on commit 86a0e84

Please sign in to comment.