Skip to content

Commit c7af35a

Browse files
committed
TT-17776: add reusable drift-check workflow for gromit-managed repos
1 parent 895743d commit c7af35a

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

.github/workflows/drift-check.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Gromit Drift Check
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
label_name:
7+
description: 'PR label that bypasses the drift check'
8+
default: 'drift-override'
9+
type: string
10+
11+
jobs:
12+
drift-check:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
steps:
17+
- name: Break-glass bypass
18+
if: ${{ contains(github.event.pull_request.labels.*.name, inputs.label_name) }}
19+
run: |
20+
echo "::warning title=Drift check bypassed::'${{ inputs.label_name }}' label present. This change bypasses gromit; backport it into gromit's templates or it will be overwritten by the next policy sync."
21+
{
22+
echo "## :rotating_light: Drift check bypassed"
23+
echo ""
24+
echo "The \`${{ inputs.label_name }}\` label is present on this PR."
25+
echo "Backport this change into gromit's templates, otherwise the next \`policy sync\` will overwrite it."
26+
} >> "$GITHUB_STEP_SUMMARY"
27+
28+
- name: Checkout
29+
if: ${{ !contains(github.event.pull_request.labels.*.name, inputs.label_name) }}
30+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
31+
32+
- name: Set up Go
33+
if: ${{ !contains(github.event.pull_request.labels.*.name, inputs.label_name) }}
34+
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
35+
with:
36+
go-version: "1.22"
37+
38+
- name: Build gromit from master
39+
if: ${{ !contains(github.event.pull_request.labels.*.name, inputs.label_name) }}
40+
run: go install github.com/TykTechnologies/gromit@master
41+
42+
- name: Check for drift from gromit
43+
if: ${{ !contains(github.event.pull_request.labels.*.name, inputs.label_name) }}
44+
env:
45+
BASE_REF: ${{ github.base_ref }}
46+
run: |
47+
set -o pipefail
48+
export PATH="$PATH:$(go env GOPATH)/bin"
49+
50+
REPO=$(basename "${{ github.repository }}")
51+
52+
if ! out=$(gromit policy gen --repo "$REPO" --branch "$BASE_REF" . 2>&1); then
53+
if echo "$out" | grep -q "unknown among"; then
54+
echo "::notice title=Drift check skipped::Branch '$BASE_REF' of $REPO is not managed by gromit."
55+
exit 0
56+
fi
57+
echo "$out"
58+
exit 1
59+
fi
60+
61+
git add -A
62+
if ! gromit policy diff .; then
63+
echo "::error title=Drift from gromit detected::Files in this PR differ from what gromit generates for $REPO/$BASE_REF. These files are managed by gromit; make this change in gromit's templates instead (https://github.com/TykTechnologies/gromit). For release emergencies, a repo admin can add the '${{ inputs.label_name }}' label to bypass this check."
64+
{
65+
echo "## :x: Drift from gromit detected"
66+
echo ""
67+
echo "Gromit-managed files in this PR differ from what gromit generates for \`$REPO\`/\`$BASE_REF\`."
68+
echo ""
69+
echo '```'
70+
git --no-pager diff --staged --stat
71+
echo '```'
72+
echo ""
73+
echo "Make this change in [gromit](https://github.com/TykTechnologies/gromit) instead. For release emergencies, a repo admin can add the \`${{ inputs.label_name }}\` label to bypass this check."
74+
} >> "$GITHUB_STEP_SUMMARY"
75+
exit 1
76+
fi
77+
echo "No drift detected: $REPO/$BASE_REF matches gromit."

0 commit comments

Comments
 (0)