|
| 1 | +name: Generate Template |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: write |
| 8 | + pull-requests: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + generate-template: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + env: |
| 14 | + GENERATED_BRANCH_NAME: generated-template |
| 15 | + steps: |
| 16 | + - name: Checkout main |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + ref: main |
| 20 | + fetch-depth: 0 |
| 21 | + token: ${{ secrets.PAT_TOKEN }} |
| 22 | + |
| 23 | + - name: Configure Git for push |
| 24 | + run: | |
| 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" |
| 28 | +
|
| 29 | + - name: Check Branch & PR |
| 30 | + id: check_branch_pr |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ secrets.PAT_TOKEN }} # PAT or GITHUB_TOKEN with write access |
| 33 | + run: | |
| 34 | + git fetch origin |
| 35 | + |
| 36 | + # --- Check if branch exists remotely --- |
| 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 |
| 41 | + echo "branch_exists=true" >> $GITHUB_OUTPUT |
| 42 | + else |
| 43 | + echo "🚫 Branch $GENERATED_BRANCH_NAME does not exist." |
| 44 | + git checkout -b $GENERATED_BRANCH_NAME |
| 45 | + git push -u origin $GENERATED_BRANCH_NAME |
| 46 | + echo "branch_exists=false" >> $GITHUB_OUTPUT |
| 47 | + fi |
| 48 | + echo "branch=${GENERATED_BRANCH_NAME}" >> $GITHUB_OUTPUT |
| 49 | + |
| 50 | + # --- Check if PR already exists for this branch --- |
| 51 | + OPEN_PR=$(gh pr list --head "$GENERATED_BRANCH_NAME" --state open --json number --jq '.[0].number') |
| 52 | + if [ -n "$OPEN_PR" ]; then |
| 53 | + echo "open_pr=true" >> $GITHUB_OUTPUT |
| 54 | + echo "open_pr_number=$OPEN_PR" >> $GITHUB_OUTPUT |
| 55 | + echo "📬 Open PR #$OPEN_PR found for branch $GENERATED_BRANCH_NAME." |
| 56 | + else |
| 57 | + echo "open_pr=false" >> $GITHUB_OUTPUT |
| 58 | + echo "📭 No open PR found for branch $GENERATED_BRANCH_NAME." |
| 59 | + fi |
| 60 | + |
| 61 | + - name: Make script executable |
| 62 | + run: chmod +x ./generate-cookiecutter-template-from-example-project.sh |
| 63 | + |
| 64 | + - name: Generate cookiecutter template |
| 65 | + run: ./generate-cookiecutter-template-from-example-project.sh |
| 66 | + |
| 67 | + - name: Copy generated template to {{cookiecutter.app_name}} |
| 68 | + run: | |
| 69 | + rsync -a --delete --exclude='.git' ./example-template/ ./{{cookiecutter.app_name}}/ |
| 70 | +
|
| 71 | + - name: Commit and push changes |
| 72 | + id: commit |
| 73 | + run: | |
| 74 | + git add . |
| 75 | + if git diff --cached --quiet; then |
| 76 | + echo "no_changes=true" >> $GITHUB_OUTPUT |
| 77 | + echo "✅ No changes detected." |
| 78 | + else |
| 79 | + git commit -m "ci: update generated cookiecutter template from example" |
| 80 | + git push origin $GENERATED_BRANCH_NAME |
| 81 | + echo "no_changes=false" >> $GITHUB_OUTPUT |
| 82 | + fi |
| 83 | +
|
| 84 | + - name: Create or update Pull Request |
| 85 | + if: steps.commit.outputs.no_changes == 'false' && steps.check_branch_pr.outputs.open_pr == 'false' |
| 86 | + env: |
| 87 | + GH_TOKEN: ${{ secrets.PAT_TOKEN }} |
| 88 | + run: | |
| 89 | + gh pr create \ |
| 90 | + --base main \ |
| 91 | + --head $GENERATED_BRANCH_NAME \ |
| 92 | + --title "ci: update generated cookiecutter template from example" \ |
| 93 | + --body "Automated PR created by workflow." |
0 commit comments