Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor step into standalone action #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
60 changes: 5 additions & 55 deletions .github/workflows/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,62 +51,12 @@ jobs:
- run: terraform show -no-color tfplan > summary.txt

- name: Create status check with details
uses: actions/github-script@v7
uses: ./actions/checks
with:
github-token: ${{ github.token }}
script: |
const fs = require('fs').promises
const plan = JSON.parse(await fs.readFile('tfplan.json', 'utf-8'))
const humanSummary = await fs.readFile('summary.txt', 'utf-8')
let applyLog;
try {
applyLog = await fs.readFile('apply.log', 'utf-8')
} catch {}

function countActions(plan, type) {
return plan.resource_changes.filter(ch => ch.change.actions.includes(type)).length
}
const createCount = countActions(plan, 'create')
const updateCount = countActions(plan, 'update')
const deleteCount = countActions(plan, 'delete')

const noChanges = createCount == 0 && updateCount == 0 && deleteCount == 0
const title = noChanges
? "No changes"
: (context.eventName === 'push'
? `${createCount} added, ${updateCount} changed, ${deleteCount} destroyed`
: `${createCount} to add, ${updateCount} to change, ${deleteCount} to destroy`
)

const codefence = "```"
const summary = `
# Terraform Plan
${codefence}
${humanSummary.trim("\n")}
${codefence}
${!!applyLog ? `
# Terraform Apply
${codefence}
${applyLog.replace(/\u001b\[[^m]+m/g, '').trim()}
${codefence}
` : ""}
`

const sha = context.eventName === 'pull_request'
? context.payload.pull_request.head.sha
: context.sha
await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: sha,
status: 'completed',
conclusion: noChanges ? 'neutral' : 'success',
name: context.eventName === 'push' ? "Apply" : "Plan",
output: {
title,
summary,
},
});
token: ${{ github.token }}
plan-json: tfplan.json
plan-summary: summary.txt
apply-summary: apply.log

- name: Show plan on PR
uses: actions/github-script@v7
Expand Down
17 changes: 17 additions & 0 deletions actions/checks/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "Terraform Checks"
description: "Annotate a commit with details about the Terraform run"
inputs:
token:
description: "GitHub token"
required: true
plan-json:
description: "JSON file containing the plan"
required: true
plan-summary:
description: "Log file with the human readable plan summary"
required: true
apply-summary:
description: "Log file with the human readable apply summary"
runs:
using: "node20"
main: "index.js"
75 changes: 75 additions & 0 deletions actions/checks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs").promises;

const planJson = core.getInput("plan-json");
const planSummary = core.getInput("plan-summary");
const applySummary = core.getInput("apply-summary");
const ghToken = core.getInput("token");
const oktokit = github.getOctokit(ghToken);

async function createCheck() {
const { context } = github;

const plan = JSON.parse(await fs.readFile(planJson, "utf-8"));
const humanSummary = await fs.readFile(planSummary, "utf-8");
let applyLog;
if (applySummary) {
try {
applyLog = await fs.readFile(applySummary, "utf-8");
} catch {}
}

function countActions(plan, type) {
return plan.resource_changes.filter((ch) =>
ch.change.actions.includes(type)
).length;
}
const createCount = countActions(plan, "create");
const updateCount = countActions(plan, "update");
const deleteCount = countActions(plan, "delete");

const noChanges = createCount == 0 && updateCount == 0 && deleteCount == 0;
const title = noChanges
? "No changes"
: context.eventName === "push"
? `${createCount} added, ${updateCount} changed, ${deleteCount} destroyed`
: `${createCount} to add, ${updateCount} to change, ${deleteCount} to destroy`;

const codefence = "```";
const summary = `
# Terraform Plan
${codefence}
${humanSummary.trim()}
${codefence}
${
!!applyLog
? `
# Terraform Apply
${codefence}
${applyLog.replace(/\u001b\[[^m]+m/g, "").trim()}
${codefence}
`
: ""
}
`;

const sha =
context.eventName === "pull_request"
? context.payload.pull_request?.head.sha
: context.sha;
await oktokit.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
head_sha: sha,
status: "completed",
conclusion: noChanges ? "neutral" : "success",
name: context.eventName === "push" ? "Apply" : "Plan",
output: {
title,
summary,
},
});
}

createCheck();
Loading