Skip to content

Commit 3f950d1

Browse files
committed
feat: add reusable OpenTofu workflow and CI
1 parent 74eab5d commit 3f950d1

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

.github/workflows/_ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
lint:
13+
name: Lint
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Install actionlint
20+
run: |
21+
curl -sSL https://github.com/rhysd/actionlint/releases/download/v1.7.7/actionlint_1.7.7_linux_amd64.tar.gz | \
22+
tar xz -C /usr/local/bin actionlint
23+
24+
- name: Run pre-commit
25+
uses: pre-commit/action@v3.0.1

.github/workflows/opentofu.yml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: OpenTofu
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
environment:
7+
description: Environment for apply job
8+
type: string
9+
default: production
10+
secrets:
11+
SOPS_AGE_KEY:
12+
required: true
13+
14+
permissions:
15+
contents: read
16+
pull-requests: write
17+
18+
jobs:
19+
test:
20+
name: Pre-commit Tests
21+
runs-on: ubuntu-latest
22+
container:
23+
image: ghcr.io/makeitworkcloud/runner:latest
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0
29+
30+
- name: Initialize OpenTofu
31+
run: tofu init -backend=false
32+
33+
- name: Run tests
34+
run: make test
35+
36+
plan:
37+
name: OpenTofu Plan
38+
runs-on: ubuntu-latest
39+
container:
40+
image: ghcr.io/makeitworkcloud/runner:latest
41+
if: github.event_name == 'pull_request'
42+
needs: [test]
43+
env:
44+
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
45+
steps:
46+
- name: Checkout
47+
uses: actions/checkout@v4
48+
49+
- name: OpenTofu Plan
50+
id: plan
51+
run: |
52+
make plan || true
53+
54+
sed -n '/OpenTofu will perform the following actions:/,$p' plan-output.txt > plan-filtered.txt
55+
56+
if [ ! -s plan-filtered.txt ]; then
57+
grep -A 2 "No changes" plan-output.txt > plan-filtered.txt || echo "No plan output found" > plan-filtered.txt
58+
fi
59+
60+
tail -n 1000 plan-filtered.txt > plan-filtered-truncated.txt
61+
mv plan-filtered-truncated.txt plan-filtered.txt
62+
63+
- name: Comment PR with Plan
64+
uses: actions/github-script@v7
65+
with:
66+
github-token: ${{ secrets.GITHUB_TOKEN }}
67+
script: |
68+
const fs = require('fs');
69+
const planOutput = fs.readFileSync('plan-filtered.txt', 'utf8');
70+
71+
const output = `#### OpenTofu Plan
72+
\`\`\`
73+
${planOutput}
74+
\`\`\`
75+
`;
76+
github.rest.issues.createComment({
77+
issue_number: context.issue.number,
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
body: output
81+
});
82+
83+
apply:
84+
name: OpenTofu Apply
85+
runs-on: ubuntu-latest
86+
container:
87+
image: ghcr.io/makeitworkcloud/runner:latest
88+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
89+
needs: [test]
90+
environment: ${{ inputs.environment }}
91+
env:
92+
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
93+
steps:
94+
- name: Checkout
95+
uses: actions/checkout@v4
96+
97+
- name: OpenTofu Apply
98+
run: make apply

.pre-commit-config.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v5.0.0
4+
hooks:
5+
- id: check-case-conflict
6+
- id: check-merge-conflict
7+
- id: check-symlinks
8+
- id: check-vcs-permalinks
9+
- id: destroyed-symlinks
10+
- id: detect-private-key
11+
- id: mixed-line-ending
12+
- id: trailing-whitespace
13+
14+
- repo: https://github.com/rhysd/actionlint
15+
rev: v1.7.7
16+
hooks:
17+
- id: actionlint

0 commit comments

Comments
 (0)