Skip to content

Commit ca0d899

Browse files
authored
docs: Add documentation to README (#3)
1 parent 4af5517 commit ca0d899

File tree

13 files changed

+167
-24
lines changed

13 files changed

+167
-24
lines changed

.github/workflows/e2e.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: End-to-End
2+
on:
3+
pull_request:
4+
5+
permissions:
6+
contents: read
7+
pull-requests: write
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
simple-test:
14+
name: Simple Test
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
- name: Setup terraform
20+
uses: hashicorp/setup-terraform@v3
21+
- name: Setup - initialize
22+
run: terraform init
23+
working-directory: tests/ci/0-setup
24+
- name: Setup - apply
25+
run: terraform apply -auto-approve -state ../.tfstate
26+
working-directory: tests/ci/0-setup
27+
- name: Change - initialize
28+
run: terraform init
29+
working-directory: tests/ci/1-change
30+
- name: Change - plan
31+
run: terraform plan -state ../.tfstate -out .planfile
32+
working-directory: tests/ci/1-change
33+
- name: Post PR comment
34+
uses: ./
35+
with:
36+
token: ${{ github.token }}
37+
planfile: .planfile
38+
working-directory: tests/ci/1-change

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,73 @@
11
# terraform-plan-comment
22

33
GitHub Action to post the output of `terraform plan` to a PR comment.
4+
5+
## Features
6+
7+
- Generate a "markdown-native" representation of the plan including foldable sections and coloring
8+
- Post the plan to pull requests as a "sticky comment"
9+
- Use with or without the Terraform wrapper script provided by
10+
[hashicorp/setup-terraform](https://github.com/hashicorp/setup-terraform)
11+
12+
## Usage
13+
14+
```yaml
15+
- name: Setup terraform
16+
uses: hashicorp/setup-terraform@v3
17+
- name: Initialize
18+
run: terraform init
19+
- name: Plan
20+
run: terraform plan -out .planfile
21+
- name: Post PR comment
22+
uses: borchero/terraform-plan-comment@v1
23+
with:
24+
token: ${{ github.token }}
25+
planfile: .planfile
26+
```
27+
28+
### Example Comments
29+
30+
<details><summary><b>Collapsed</b></summary>
31+
32+
<img width="916" alt="Screenshot 2024-04-30 at 00 07 36" src="https://github.com/borchero/terraform-plan-comment/assets/22455425/b6d0e64c-1c9c-42b8-8060-c096922baa0a">
33+
34+
</details>
35+
36+
<details><summary><b>Expanded</b></summary>
37+
38+
<img width="699" alt="Screenshot 2024-04-30 at 00 08 22" src="https://github.com/borchero/terraform-plan-comment/assets/22455425/c91c319a-276d-4d2d-98a7-52bd24b64d4c">
39+
40+
</details>
41+
42+
## Parameters
43+
44+
This action provides a few input parameters that allow for customization:
45+
46+
### `token` (required)
47+
48+
Required input parameter to access the GitHub API for posting a pull request comment. Can be provided as
49+
`${{ github.token }}`, `${{ env.GITHUB_TOKEN }}` or some personal access token with appropriate permissions.
50+
51+
If using the workflow-provided token, make sure that your workflow/job has write-permissions to pull requests.
52+
53+
### `planfile` (required)
54+
55+
The path to the planfile generated by `terraform plan` which holds the information about which changes ought to be
56+
applied.
57+
58+
### `terraform-cmd`
59+
60+
The command to execute to call the Terraform binary. Defaults to `terraform`. You likely don't need to augment this
61+
unless `terraform` cannot be found in the `PATH`.
62+
63+
### `working-directory`
64+
65+
The directory where the Terraform binary ought to be called. Defaults to `$GITHUB_WORKSPACE` and _must_ be specified if
66+
`terraform init` has been run in a different directory. Should be specified relative to `$GITHUB_WORKSPACE`.
67+
68+
> [!IMPORTANT] > `planfile` must be specified relative to the working directory.
69+
70+
### `id`
71+
72+
A custom identifier for the Terraform execution. This allows to distinguish multiple Terraform runs: each sticky pull
73+
request comment is tied to an ID.

dist/index.js

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ function renderBody(plan: RenderedPlan): string {
3030
`${Object.keys(plan.deletedResources ?? {}).length} to delete.**`
3131

3232
if (plan.createdResources) {
33-
body += '\n\n### Create'
33+
body += '\n\n### Create'
3434
body += renderResources(plan.createdResources)
3535
}
3636
if (plan.updatedResources) {
37-
body += '\n\n### Update'
37+
body += '\n\n### ♻️ Update'
3838
body += renderResources(plan.updatedResources)
3939
}
4040
if (plan.recreatedResources) {
41-
body += '\n\n### Re-Create'
41+
body += '\n\n### ⚙️ Re-Create'
4242
body += renderResources(plan.recreatedResources)
4343
}
4444
if (plan.deletedResources) {
45-
body += '\n\n### Delete'
45+
body += '\n\n### 🗑️ Delete'
4646
body += renderResources(plan.deletedResources)
4747
}
4848

@@ -71,7 +71,7 @@ export function renderComment({
7171
let footer = ''
7272
if (includeFooter === undefined || includeFooter === true) {
7373
footer =
74-
`\n\n_Triggered by @${github.context.actor},` +
74+
`\n\n---\n\n_Triggered by @${github.context.actor},` +
7575
` Commit: \`${(github.context.payload as PullRequestEvent).pull_request.head.sha}\`_`
7676
}
7777

tests/ci/0-setup/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_providers {
3+
local = {
4+
source = "hashicorp/local"
5+
}
6+
}
7+
}
8+
9+
variable "test" {
10+
default = 42
11+
sensitive = true
12+
}
13+
14+
resource "local_file" "test" {
15+
count = 2
16+
filename = "../test.txt"
17+
content = "test-${var.test}"
18+
}

tests/ci/1-change/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
terraform {
2+
required_providers {
3+
local = {
4+
source = "hashicorp/local"
5+
}
6+
}
7+
}
8+
9+
variable "test" {
10+
default = 40
11+
sensitive = true
12+
}
13+
14+
resource "local_file" "test" {
15+
count = 1
16+
filename = "../test.txt"
17+
content = "test-${var.test}"
18+
}
19+
20+
resource "local_file" "another" {
21+
filename = "../another.txt"
22+
content = "Hello terraform-plan-comment!"
23+
}

tests/fixtures/basic/0-create/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
3-
null = {
4-
source = "hashicorp/null"
3+
local = {
4+
source = "hashicorp/local"
55
}
66
}
77
}

tests/fixtures/basic/0-create/plan.json

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/basic/0-create/rendered.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**→ Resource Changes: 1 to create, 0 to update, 0 to re-create, 0 to delete.**
44

5-
### Create
5+
### Create
66

77
<details><summary><code>local_file.test</code></summary>
88

tests/fixtures/basic/1-modify/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
3-
null = {
4-
source = "hashicorp/null"
3+
local = {
4+
source = "hashicorp/local"
55
}
66
}
77
}

0 commit comments

Comments
 (0)