Generate Template #19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Generate Template | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| generate-template: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout main branch | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| token: ${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }} | |
| persist-credentials: true | |
| fetch-depth: 0 | |
| # 🧩 Configure Git credentials and remote | |
| - name: Configure Git auth | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| git remote set-url origin "https://${{ secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" | |
| echo "✅ Git authentication configured." | |
| - name: Prepare or reuse generated-template branch | |
| id: prepare_branch | |
| run: | | |
| BRANCH="generated-template" | |
| git fetch origin | |
| if git show-ref --verify --quiet refs/remotes/origin/$BRANCH; then | |
| echo "Branch exists — checking out and pulling latest." | |
| git checkout $BRANCH | |
| git pull origin $BRANCH | |
| else | |
| echo "Branch does not exist — creating new one." | |
| git checkout -b $BRANCH | |
| git push --set-upstream origin $BRANCH | |
| fi | |
| echo "branch=${BRANCH}" >> $GITHUB_OUTPUT | |
| - name: Make script executable | |
| run: chmod +x ./generate-cookiecutter-template-from-example-project.sh | |
| - name: Generate cookiecutter template | |
| run: ./generate-cookiecutter-template-from-example-project.sh | |
| - name: Copy generated template to {{cookiecutter.app_name}} | |
| run: | | |
| rsync -a --delete --exclude='.git' ./example-template/ ./{{cookiecutter.app_name}}/ | |
| - name: Debug Git remote info | |
| run: | | |
| git remote -v | |
| git branch --show-current | |
| git status | |
| - name: Commit and push changes | |
| id: commit | |
| run: | | |
| git add . | |
| if git diff --cached --quiet; then | |
| echo "no_changes=true" >> $GITHUB_OUTPUT | |
| echo "✅ No changes detected." | |
| else | |
| git commit -m "ci: update generated cookiecutter template from example" | |
| git push origin generated-template | |
| echo "no_changes=false" >> $GITHUB_OUTPUT | |
| fi | |
| # 🧪 Verify that the commit is visible on GitHub | |
| - name: Verify commit visibility | |
| if: steps.commit.outputs.no_changes == 'false' | |
| run: | | |
| echo "🧾 Local HEAD commit:" | |
| git log -1 --oneline | |
| echo "⏳ Waiting a few seconds for GitHub to index the push..." | |
| sleep 5 | |
| git fetch origin generated-template | |
| echo "🧾 Remote branch HEAD:" | |
| git log origin/generated-template -1 --oneline | |
| LOCAL_SHA=$(git rev-parse HEAD) | |
| REMOTE_SHA=$(git rev-parse origin/generated-template) | |
| if [ "$LOCAL_SHA" = "$REMOTE_SHA" ]; then | |
| echo "✅ Verified: commit is on GitHub." | |
| else | |
| echo "⚠️ Warning: commit not yet visible remotely (may take a few seconds)." | |
| fi | |
| - name: Create or Update Pull Request | |
| if: steps.commit.outputs.no_changes == 'false' | |
| uses: peter-evans/create-pull-request@v7 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| base: main | |
| branch: ${{ steps.prepare_branch.outputs.branch }} | |
| title: "ci: update generated cookiecutter template from example" | |
| body: | | |
| This PR was created automatically by the Generate Template workflow. | |
| It contains updates generated from the example project. | |
| commit-message: "ci: update generated cookiecutter template from example" | |
| labels: automated-pr | |
| delete-branch: false |