Skip to content

Commit a04ecd1

Browse files
committed
ci: refactor generate-template workflow to use dynamic branch name
1 parent d9c8513 commit a04ecd1

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

.github/workflows/generate-template.yaml

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,52 @@ permissions:
1010
jobs:
1111
generate-template:
1212
runs-on: ubuntu-latest
13-
13+
env:
14+
GENERATED_BRANCH_NAME: generated-template
1415
steps:
1516
- name: Checkout main
1617
uses: actions/checkout@v4
17-
with:
18-
ref: main
19-
fetch-depth: 0
20-
token: ${{ secrets.PAT_TOKEN }}
21-
22-
- name: Configure Git for push
23-
run: |
24-
git config user.name "github-actions[bot]"
25-
git config user.email "github-actions[bot]@users.noreply.github.com"
26-
git remote set-url origin "https://${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository }}.git"
18+
# with:
19+
# ref: main
20+
# fetch-depth: 0
21+
# token: ${{ secrets.PAT_TOKEN }}
2722

28-
# - name: Prepare or reuse generated-template branch
29-
# id: prepare_branch
23+
# - name: Configure Git for push
3024
# run: |
31-
# BRANCH="generated-template"
32-
# git fetch origin
33-
# if git show-ref --verify --quiet refs/remotes/origin/$BRANCH; then
34-
# echo "Branch exists — checking it out."
35-
# git checkout $BRANCH
36-
# git pull origin $BRANCH
37-
# else
38-
# echo "Branch does not exist — creating it."
39-
# git checkout -b $BRANCH
40-
# git push -u origin $BRANCH
41-
# fi
42-
# echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
25+
# git config user.name "github-actions[bot]"
26+
# git config user.email "github-actions[bot]@users.noreply.github.com"
27+
# git remote set-url origin "https://${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository }}.git"
4328

4429
- name: Check Branch & PR
4530
id: check_branch_pr
4631
env:
4732
GH_TOKEN: ${{ secrets.PAT_TOKEN }} # PAT or GITHUB_TOKEN with write access
4833
run: |
49-
BRANCH="generated-template"
5034
git fetch origin
5135
5236
# --- Check if branch exists remotely ---
53-
if git show-ref --verify --quiet refs/remotes/origin/$BRANCH; then
54-
echo "✅ Branch $BRANCH exists remotely."
55-
git checkout $BRANCH
56-
git pull origin $BRANCH
37+
if git show-ref --verify --quiet refs/remotes/origin/$GENERATED_BRANCH_NAME; then
38+
echo "✅ Branch $GENERATED_BRANCH_NAME exists remotely."
39+
git checkout $GENERATED_BRANCH_NAME
40+
git pull origin $GENERATED_BRANCH_NAME
5741
echo "branch_exists=true" >> $GITHUB_OUTPUT
5842
else
59-
echo "🚫 Branch $BRANCH does not exist."
60-
git checkout -b $BRANCH
61-
git push -u origin $BRANCH
43+
echo "🚫 Branch $GENERATED_BRANCH_NAME does not exist."
44+
git checkout -b $GENERATED_BRANCH_NAME
45+
git push -u origin $GENERATED_BRANCH_NAME
6246
echo "branch_exists=false" >> $GITHUB_OUTPUT
6347
fi
64-
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
48+
echo "branch=${GENERATED_BRANCH_NAME}" >> $GITHUB_OUTPUT
6549

6650
# --- Check if PR already exists for this branch ---
67-
OPEN_PR=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number')
51+
OPEN_PR=$(gh pr list --head "$GENERATED_BRANCH_NAME" --state open --json number --jq '.[0].number')
6852
if [ -n "$OPEN_PR" ]; then
6953
echo "open_pr=true" >> $GITHUB_OUTPUT
7054
echo "open_pr_number=$OPEN_PR" >> $GITHUB_OUTPUT
71-
echo "📬 Open PR #$OPEN_PR found for branch $BRANCH."
55+
echo "📬 Open PR #$OPEN_PR found for branch $GENERATED_BRANCH_NAME."
7256
else
7357
echo "open_pr=false" >> $GITHUB_OUTPUT
74-
echo "📭 No open PR found for branch $BRANCH."
58+
echo "📭 No open PR found for branch $GENERATED_BRANCH_NAME."
7559
fi
7660

7761
- name: Make script executable
@@ -93,7 +77,7 @@ jobs:
9377
echo "✅ No changes detected."
9478
else
9579
git commit -m "ci: update generated cookiecutter template from example"
96-
git push origin generated-template
80+
git push origin $GENERATED_BRANCH_NAME
9781
echo "no_changes=false" >> $GITHUB_OUTPUT
9882
fi
9983
@@ -104,6 +88,6 @@ jobs:
10488
run: |
10589
gh pr create \
10690
--base main \
107-
--head generated-template \
91+
--head $GENERATED_BRANCH_NAME \
10892
--title "ci: update generated cookiecutter template from example" \
10993
--body "Automated PR created by workflow."

0 commit comments

Comments
 (0)