Skip to content

Commit c7ca07b

Browse files
authored
feat: Enhance changelog workflow with better error handling and logging (#12)
1 parent d2124fa commit c7ca07b

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

.github/workflows/update-changelog.yml

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
with:
2020
fetch-depth: 0
2121
ref: main
22-
token: ${{ secrets.GITHUB_TOKEN }}
22+
token: ${{ secrets.PAT_TOKEN }}
2323

2424
- name: Get PR title and body
2525
id: pr-info
@@ -31,21 +31,40 @@ jobs:
3131
3232
- name: Update Changelog
3333
run: |
34+
# Check if CHANGELOG.md exists
35+
if [ ! -f CHANGELOG.md ]; then
36+
echo "Error: CHANGELOG.md not found"
37+
echo "Creating initial CHANGELOG.md"
38+
echo "# Changelog\n\nAll notable changes to this project will be documented in this file.\n\n" > CHANGELOG.md
39+
CURRENT_VERSION="0.0.0"
40+
else
41+
echo "Reading current version from CHANGELOG.md"
42+
# Read current version from latest entry with error handling
43+
CURRENT_VERSION=$(grep -m 1 "## \[v" CHANGELOG.md | sed 's/## \[v\(.*\)\].*/\1/')
44+
if [ -z "$CURRENT_VERSION" ]; then
45+
echo "Warning: No version found in CHANGELOG.md, using 0.0.0"
46+
CURRENT_VERSION="0.0.0"
47+
else
48+
echo "Current version: $CURRENT_VERSION"
49+
fi
50+
fi
51+
3452
# Get today's date
3553
DATE=$(date +%Y-%m-%d)
36-
37-
# Read current version from latest entry
38-
CURRENT_VERSION=$(grep -m 1 "## \[v" CHANGELOG.md | sed 's/## \[v\(.*\)\].*/\1/')
54+
echo "Generating changelog entry for date: $DATE"
3955
4056
# Increment patch version
4157
IFS='.' read -r major minor patch <<< "$CURRENT_VERSION"
4258
NEW_VERSION="${major}.${minor}.$((patch + 1))"
59+
echo "New version will be: $NEW_VERSION"
4360
4461
# Create new changelog entry
4562
NEW_ENTRY="## [v${NEW_VERSION}] - ${DATE}\n\n"
4663
4764
# Determine change type from PR title
4865
PR_TITLE="${{ steps.pr-info.outputs.title }}"
66+
echo "Processing PR title: $PR_TITLE"
67+
4968
if [[ "$PR_TITLE" == *"feat:"* || "$PR_TITLE" == *"feature:"* ]]; then
5069
CHANGE_TYPE="Added"
5170
elif [[ "$PR_TITLE" == *"fix:"* ]]; then
@@ -60,11 +79,13 @@ jobs:
6079
CHANGE_TYPE="Changed"
6180
fi
6281
82+
echo "Change type determined as: $CHANGE_TYPE"
6383
NEW_ENTRY+="### ${CHANGE_TYPE}\n"
6484
NEW_ENTRY+="- ${PR_TITLE#*: }\n"
6585
6686
# Add PR body details if they exist
6787
if [ ! -z "${{ steps.pr-info.outputs.body }}" ]; then
88+
echo "Processing PR body for additional details"
6889
while IFS= read -r line; do
6990
if [[ "$line" =~ ^-[[:space:]].*$ ]]; then
7091
NEW_ENTRY+="$line\n"
@@ -75,29 +96,42 @@ jobs:
7596
# Add version link at the end
7697
NEW_ENTRY+="\n[v${NEW_VERSION}]: https://github.com/PeterVinter/Manage_linux_docker_containers/releases/tag/v${NEW_VERSION}\n"
7798
78-
# Create temporary file with new content
99+
echo "Creating temporary file with new changelog entry"
79100
echo -e "$NEW_ENTRY" > temp_entry
80-
sed -i '7r temp_entry' CHANGELOG.md
101+
102+
if [ -s CHANGELOG.md ]; then
103+
echo "Inserting new entry into existing CHANGELOG.md"
104+
sed -i '7r temp_entry' CHANGELOG.md
105+
else
106+
echo "Creating new CHANGELOG.md with initial entry"
107+
cat temp_entry >> CHANGELOG.md
108+
fi
81109
rm temp_entry
82110
83111
# Configure Git
112+
echo "Configuring Git"
84113
git config --local user.email "github-actions[bot]@users.noreply.github.com"
85114
git config --local user.name "github-actions[bot]"
86115
87116
# Create branch, commit and push
88117
BRANCH_NAME="bot/update-changelog-v${NEW_VERSION}"
118+
echo "Creating branch: $BRANCH_NAME"
89119
git checkout -b "$BRANCH_NAME"
120+
121+
echo "Committing changes"
90122
git add CHANGELOG.md
91123
git commit -m "docs: update changelog for v${NEW_VERSION}"
92124
93-
# Force push in case branch exists
125+
echo "Pushing changes"
94126
git push -f origin "$BRANCH_NAME"
95127
96-
# Create PR using GitHub CLI
128+
echo "Creating pull request"
97129
gh pr create \
98130
--title "docs: update changelog for v${NEW_VERSION}" \
99131
--body "Automated changelog update for version ${NEW_VERSION}" \
100132
--base main \
101-
--head "$BRANCH_NAME" || true
133+
--head "$BRANCH_NAME" || {
134+
echo "Warning: Failed to create PR. This might be because a PR already exists."
135+
}
102136
env:
103-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
137+
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}

0 commit comments

Comments
 (0)