Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from speee/output-resource-names
Browse files Browse the repository at this point in the history
Enhance outputs
  • Loading branch information
rakiyoshi authored Nov 26, 2021
2 parents 11811b8 + f07e454 commit b1a5cce
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
57 changes: 50 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,62 @@ The `speee/terraform-plan-action` action is a CI action that runs `terraform pla
```yaml
- uses: speee/terraform-plan-action@v1
with:
terraform_version: 0.12.29
working-directory: terraform/dir/
```
## Inputs
The action supports the following inputs:
This action supports the following inputs.
- `working-directory` - (optional) Relative path from repository root to your terraform root module (default `.`).
### `working-directory`

- `aws-access-key-id` - (optional) Used when performing `terraform plan` to your AWS environment.
**Required.**
Relative path from repository root to your terraform root module. Default is `.`.

- `aws-secret-access-key` - (optional)
### `enable-lock`

- `terraform-version` - (optional) The version of Terraform CLI to install. Defaults to `latest`.
Optional.
Flag to determine whether state lock will be enabled. Default is `true`.

- `github-token` - Used when writing comments on your pull request.
## Outputs

### `plan-stdout`

stdout of plan execution.

### `plan-stderr`

stderr of plan execution.

### `plan-result`

stdout of plan execution **without logs during plan**.

### `resources-to-be-replaced`

JSON strings of resources to be replaced.

An example output format is like below.

```
["aws_iam_user.foo","aws_iam_user.bar"]
```
### `resources-to-be-created`
JSON strings of resources to be created.
An example output format is same as `resources-to-be-created`.
### `resources-to-be-deleted`
JSON strings of resources to be deleted.
An example output format is same as `resources-to-be-created`.
### `resources-to-be-updated`
JSON strings of resources to be updated.
An example output format is same as `resources-to-be-created`.
### `plan-has-changes`
A flag that indicates whether the plan has changes.
45 changes: 39 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@ name: 'Terraform plan'
description: 'Terraform plan on Pull Request'
inputs:
working-directory:
description: ''
description: 'Directory path of a root module.'
required: true
default: '.'
enable-lock:
default: true
description: 'Boolean string whether state locks to be enabled.'
default: 'true'
required: false
outputs:
plan-stdout:
description: Plan stdout
description: Plan stdout.
value: ${{ steps.plan.outputs.stdout }}
plan-stderr:
description: Plan stderr
description: Plan stderr.
value: ${{ steps.plan.outputs.stderr }}
plan-result:
description: Plan result without logs during plan.
value: ${{ steps.plan_result.outputs.result }}
resources-to-be-replaced:
description: JSON strings of resources to be replaced.
value: ${{ steps.plan_result.outputs.resources-to-be-replaced }}
resources-to-be-created:
description: JSON strings of resources to be created.
value: ${{ steps.plan_result.outputs.resources-to-be-created }}
resources-to-be-deleted:
description: JSON strings of resources to be deleted.
value: ${{ steps.plan_result.outputs.resources-to-be-deleted }}
resources-to-be-updated:
description: JSON strings of resources to be updated.
value: ${{ steps.plan_result.outputs.resources-to-be-updated }}
plan-has-changes:
description: A flat indicates that the plan command has changes
description: A flag that indicates whether the plan has changes.
value: ${{ steps.plan-exitcode.outputs.plan-has-changes }}
runs:
using: composite
Expand All @@ -35,11 +51,28 @@ runs:
shell: bash
- run: |
set +e
terraform plan -detailed-exitcode -input=false -lock=${{ inputs.enable-lock }}
terraform plan -detailed-exitcode -input=false -lock=${{ inputs.enable-lock }} -out tfplan
set -e
id: plan
working-directory: "${{ inputs.working-directory }}"
shell: bash
- name: Read result of Terraform Plan
id: plan_result
if: steps.plan.outcome == 'success'
shell: bash
run: |
echo "::set-output name=result::$(terraform show tfplan)"
terraform show -json tfplan | jq -c '.resource_changes' > tfplan.resource_changes.json
# Ref: https://www.terraform.io/docs/internals/json-format.html#change-representation
RESOURCES_TO_BE_REPLACED=$(jq -c '. | map(select(.change.actions | length==2) ) | [.[].address]' < tfplan.resource_changes.json)
RESOURCES_TO_BE_CREATED=$(jq -c '. | map(select(.change.actions | length==1 and .[0] == "create") ) | [.[].address]' < tfplan.resource_changes.json)
RESOURCES_TO_BE_DELETED=$(jq -c '. | map(select(.change.actions | length==1 and .[0] == "delete") ) | [.[].address]' < tfplan.resource_changes.json)
RESOURCES_TO_BE_UPDATED=$(jq -c '. | map(select(.change.actions | length==1 and .[0] == "update") ) | [.[].address]' < tfplan.resource_changes.json)
echo "::set-output name=resources-to-be-replaced::${RESOURCES_TO_BE_REPLACED}"
echo "::set-output name=resources-to-be-created::${RESOURCES_TO_BE_CREATED}"
echo "::set-output name=resources-to-be-deleted::${RESOURCES_TO_BE_DELETED}"
echo "::set-output name=resources-to-be-updated::${RESOURCES_TO_BE_UPDATED}"
working-directory: "${{ inputs.working-directory }}"
- run: |
planExitcode=${{ steps.plan.outputs.exitcode }}
if [ ${planExitcode} -eq 0 ]; then
Expand Down

0 comments on commit b1a5cce

Please sign in to comment.