Skip to content

Commit 36818d7

Browse files
committed
ci: enhance generate-template workflow to check for existing branch and PR
1 parent 261e000 commit 36818d7

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

.github/workflows/generate-template.yaml

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,54 @@ jobs:
2525
git config user.email "github-actions[bot]@users.noreply.github.com"
2626
git remote set-url origin "https://${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository }}.git"
2727
28-
- name: Prepare or reuse generated-template branch
29-
id: prepare_branch
28+
# - name: Prepare or reuse generated-template branch
29+
# id: prepare_branch
30+
# 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
43+
44+
- name: Check Branch & PR
45+
id: check_branch_pr
46+
env:
47+
GH_TOKEN: ${{ secrets.PAT_TOKEN }} # PAT or GITHUB_TOKEN with write access
3048
run: |
3149
BRANCH="generated-template"
3250
git fetch origin
51+
52+
# --- Check if branch exists remotely ---
3353
if git show-ref --verify --quiet refs/remotes/origin/$BRANCH; then
34-
echo "Branch exists — checking it out."
54+
echo "Branch $BRANCH exists remotely."
3555
git checkout $BRANCH
3656
git pull origin $BRANCH
57+
echo "branch_exists=true" >> $GITHUB_OUTPUT
3758
else
38-
echo "Branch does not exist — creating it."
59+
echo "🚫 Branch $BRANCH does not exist."
3960
git checkout -b $BRANCH
4061
git push -u origin $BRANCH
62+
echo "branch_exists=false" >> $GITHUB_OUTPUT
4163
fi
4264
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
65+
66+
# --- Check if PR already exists for this branch ---
67+
OPEN_PR=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number')
68+
if [ -n "$OPEN_PR" ]; then
69+
echo "open_pr=true" >> $GITHUB_OUTPUT
70+
echo "open_pr_number=$OPEN_PR" >> $GITHUB_OUTPUT
71+
echo "📬 Open PR #$OPEN_PR found for branch $BRANCH."
72+
else
73+
echo "open_pr=false" >> $GITHUB_OUTPUT
74+
echo "📭 No open PR found for branch $BRANCH."
75+
fi
4376

4477
- name: Make script executable
4578
run: chmod +x ./generate-cookiecutter-template-from-example-project.sh
@@ -61,20 +94,16 @@ jobs:
6194
else
6295
git commit -m "ci: update generated cookiecutter template from example"
6396
git push origin generated-template
64-
sleep 10
6597
echo "no_changes=false" >> $GITHUB_OUTPUT
6698
fi
6799
68100
- name: Create or update Pull Request
69-
if: steps.commit.outputs.no_changes == 'false'
70-
uses: peter-evans/create-pull-request@v7
71-
with:
72-
token: ${{ secrets.PAT_TOKEN }} # ✅ use same working PAT
73-
branch: ${{ steps.prepare_branch.outputs.branch }}
74-
title: "ci: update generated cookiecutter template from example"
75-
body: |
76-
This PR was created automatically by the Generate Template workflow.
77-
It contains updates generated from the example project.
78-
commit-message: "ci: update generated cookiecutter template from example"
79-
labels: automated-pr
80-
delete-branch: false
101+
if: steps.commit.outputs.no_changes == 'false' && steps.check_branch_pr.outputs.open_pr == 'false'
102+
env:
103+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
104+
run: |
105+
gh pr create \
106+
--base main \
107+
--head generated-template \
108+
--title "ci: update generated cookiecutter template from example" \
109+
--body "Automated PR created by workflow."

0 commit comments

Comments
 (0)