Skip to content

Commit f68c7d1

Browse files
authored
Merge pull request #133 from mallardduck/cleanup-auto-bumps
[ci] Improve auto-bump titles and cleanup
2 parents 09f1abb + a4721cf commit f68c7d1

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

.github/scripts/bump-kubectl-patch-versions

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ set -euo pipefail
55
# Otherwise, default to "kubectl-versions.txt".
66
VERSIONS_FILE=${1:-"kubectl-versions.txt"}
77
TEMP_FILE="${VERSIONS_FILE}.tmp"
8+
UPDATE_LOG_FILE="${VERSIONS_FILE}.log"
89

910
echo "Using versions file: $VERSIONS_FILE"
1011
echo "Temporary file will be: $TEMP_FILE"
12+
echo "Log file will be: $UPDATE_LOG_FILE"
1113

1214
# Check if the input file exists
1315
if [[ ! -f "$VERSIONS_FILE" ]]; then
@@ -30,6 +32,7 @@ while IFS= read -r VERSION; do
3032
fi
3133
echo "Found newer patch $NEWEST_OPTION to replace $VERSION"
3234
echo "$NEWEST_OPTION" >> "$TEMP_FILE"
35+
echo "$NEWEST_OPTION" >> "$UPDATE_LOG_FILE"
3336
done < "$VERSIONS_FILE"
3437

3538
# Check if the temporary file was created successfully
@@ -41,3 +44,5 @@ else
4144
echo "Error: Temporary file not created. No changes made."
4245
exit 1
4346
fi
47+
48+
echo "Remember the $UPDATE_LOG_FILE must be cleaned up after reading..."

.github/workflows/cleanup.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Clean PR Branch
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
cleanup:
9+
runs-on: ubuntu-latest
10+
if: contains(github.event.pull_request.labels.*.name, 'status/auto-created')
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
14+
- name: Check PR branch exists
15+
id: check-branch
16+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
17+
with:
18+
script: |
19+
try {
20+
let {status: status} = await github.rest.git.getRef({
21+
owner: context.repo.owner,
22+
repo: context.repo.repo,
23+
ref: "heads/${{ github.event.pull_request.head.ref }}"
24+
})
25+
if (status == 200) {
26+
await core.summary
27+
.addHeading("PR Branch Found", 2)
28+
.addRaw("The PR branch was found and will be cleaned up.")
29+
.write();
30+
} else {
31+
32+
core.setFailed("Branch not found, nothing to clean. (failure can be ignored)");
33+
}
34+
}
35+
catch (err) {
36+
if (err.response && err.response.status === 404) {
37+
await core.summary
38+
.addHeading("PR Branch Not Found", 2)
39+
.addRaw("The PR branch didn't exist; the action failure can be ignored.")
40+
.write();
41+
core.setFailed("Branch not found, nothing to clean. (failure can be ignored)");
42+
} else {
43+
core.setFailed(`Action failed with error ${err}`);
44+
}
45+
}
46+
- name: Delete the PR branch
47+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
48+
with:
49+
script: |
50+
try {
51+
let {status: status} = github.rest.git.deleteRef({
52+
owner: context.repo.owner,
53+
repo: context.repo.repo,
54+
ref: "heads/${{ github.event.pull_request.head.ref }}"
55+
})
56+
await core.summary
57+
.addHeading("PR Branch Deleted", 2)
58+
.addRaw("The PR's branch `${{ github.event.pull_request.head.ref }}` was deleted.")
59+
.write();
60+
}
61+
catch (err) {
62+
// setFailed logs the message and sets a failing exit code
63+
core.setFailed(`Action failed with error ${err}`);
64+
}

.github/workflows/kubectl-create-bump-pr.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
fi
4747
echo "is_supported=true" >> $GITHUB_ENV
4848
49-
- name: Pull script from main branch
49+
- name: "Pull script from `${{ inputs.script_ref || 'main' }}` branch"
5050
if: ${{ env.is_supported == 'true' }}
5151
run: |
5252
git fetch origin ${{ inputs.script_ref || 'main' }}
@@ -59,11 +59,28 @@ jobs:
5959
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6060

6161
- name: Check for changes
62+
id: changes_check
6263
if: ${{ env.is_supported == 'true' }}
6364
run: |
64-
rm -f .github/scripts/bump-kubectl-patch-versions
65+
rm -f .github/scripts/bump-kubectl-patch-versions || true
6566
git restore --staged --worktree .github/scripts/bump-kubectl-patch-versions || true
6667
68+
# Read log for PR title and clean up log file
69+
LOG_FILE="kubectl-versions.txt.log"
70+
if [[ -f "$LOG_FILE" ]]; then
71+
echo "Log file exists, parsing for title text..."
72+
# Check if the log file exists and is not empty
73+
if [[ -s "$LOG_FILE" ]]; then
74+
LOG_TITLE=$(cat "$LOG_FILE" | awk '{printf "%s%s", (NR==1?"":", "), $0} END{print ""}')
75+
echo "Log file content found, created title:"
76+
echo "$LOG_TITLE"
77+
echo "pr_title_versions=$LOG_TITLE" >> $GITHUB_OUTPUT
78+
fi
79+
80+
echo "Removing log file: $LOG_FILE"
81+
rm "$LOG_FILE"
82+
fi
83+
6784
if git diff --quiet; then
6885
echo "No changes detected."
6986
echo "changes_exist=false" >> $GITHUB_ENV
@@ -92,7 +109,9 @@ jobs:
92109
run: |
93110
{
94111
echo 'PR_BODY<<EOF'
95-
echo "Automated update using the script from \`main\` branch."
112+
echo "Bumping Kubectl versions to: ${{ steps.changes_check.outputs.pr_title_versions }}"
113+
echo ""
114+
echo "Automated update using the kubectl bump script from \`${{ inputs.script_ref || 'main' }}\` branch."
96115
echo ""
97116
echo "Triggered on: \`${{ inputs.target_branch }}\`"
98117
echo "Initiated by: @${GITHUB_ACTOR}"
@@ -108,7 +127,7 @@ jobs:
108127
if: ${{ env.changes_exist == 'true' }}
109128
env:
110129
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
111-
PR_TITLE: "Automated `kubectl` update for `${{ inputs.target_branch }}`"
130+
PR_TITLE: "[${{ inputs.target_branch }}] Automated `kubectl` updates: `${{ steps.changes_check.outputs.pr_title_versions }}`"
112131
run: |
113132
EXISTING_PR=$(gh pr list --limit 100 --json title,url \
114133
| jq --arg t "${PR_TITLE}" -r '.[] | select(.title==$t) | .url')

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
versions.txt
1111
new-versions.txt
1212
/image_arch_test
13-
*.oci
13+
*.oci
14+
kubectl-versions.txt.log

0 commit comments

Comments
 (0)