|
| 1 | +name: Build and Deploy Template |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - template |
| 7 | + |
| 8 | +jobs: |
| 9 | + test-and-deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout template branch |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + ref: template |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Install uv |
| 23 | + uses: astral-sh/setup-uv@v5 |
| 24 | + |
| 25 | + - name: Run template tests |
| 26 | + run: make test |
| 27 | + |
| 28 | + - name: Build template output |
| 29 | + run: make build |
| 30 | + |
| 31 | + - name: Get template commit SHA |
| 32 | + id: template_sha |
| 33 | + run: echo "sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
| 34 | + |
| 35 | + - name: Clone repository for main branch |
| 36 | + run: | |
| 37 | + cd .. |
| 38 | + git clone https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git main-repo |
| 39 | + cd main-repo |
| 40 | + # Try to checkout main, or create orphan if it doesn't exist |
| 41 | + git checkout main 2>/dev/null || git checkout --orphan main |
| 42 | +
|
| 43 | + - name: Apply build output to main branch |
| 44 | + run: | |
| 45 | + cd ../main-repo |
| 46 | + # Remove all files except .git |
| 47 | + find . -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} + |
| 48 | + # Copy build output |
| 49 | + cp -r ../python_template/build_output/. . |
| 50 | + # Remove copier answers file (shouldn't be in generated project) |
| 51 | + rm -f .copier-answers.yml |
| 52 | + |
| 53 | + - name: Commit and create PR |
| 54 | + run: | |
| 55 | + cd ../main-repo |
| 56 | + git config user.name "github-actions[bot]" |
| 57 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 58 | + git add -A |
| 59 | + |
| 60 | + # Check if there are changes |
| 61 | + if git diff --staged --quiet; then |
| 62 | + echo "No changes to commit" |
| 63 | + echo "has_changes=false" >> $GITHUB_ENV |
| 64 | + else |
| 65 | + # If main doesn't exist on remote, push it first |
| 66 | + if ! git ls-remote --heads origin main | grep -q main; then |
| 67 | + echo "Creating main branch" |
| 68 | + git commit -m "Initial commit from template@${{ steps.template_sha.outputs.sha }}" |
| 69 | + git push origin main |
| 70 | + fi |
| 71 | + |
| 72 | + # Create and push PR branch |
| 73 | + git checkout -b template-build |
| 74 | + git commit -m "Build from template@${{ steps.template_sha.outputs.sha }}" --allow-empty |
| 75 | + git push -f origin template-build |
| 76 | + echo "has_changes=true" >> $GITHUB_ENV |
| 77 | + fi |
| 78 | +
|
| 79 | + - name: Create Pull Request |
| 80 | + if: env.has_changes == 'true' |
| 81 | + env: |
| 82 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + run: | |
| 84 | + # Try to create PR, if it fails because PR exists, update it |
| 85 | + if ! gh pr create \ |
| 86 | + --base main \ |
| 87 | + --head template-build \ |
| 88 | + --title "Build from template@${{ steps.template_sha.outputs.sha }}" \ |
| 89 | + --body "## 🤖 Automated Template Build |
| 90 | +
|
| 91 | + This PR contains the rendered template from the latest changes on the \`template\` branch. |
| 92 | +
|
| 93 | + **Source commit:** \`${{ steps.template_sha.outputs.sha }}\` |
| 94 | + **Build timestamp:** ${{ github.event.head_commit.timestamp }} |
| 95 | +
|
| 96 | + ### ✅ Validation |
| 97 | + - Template tests passed |
| 98 | + - Pre-commit hooks validated" 2>&1 | tee /tmp/pr-output.txt; then |
| 99 | + # Check if error is because PR already exists |
| 100 | + if grep -q "already exists" /tmp/pr-output.txt; then |
| 101 | + echo "PR already exists, updating it" |
| 102 | + gh pr edit template-build \ |
| 103 | + --title "Build from template@${{ steps.template_sha.outputs.sha }}" \ |
| 104 | + --body "## 🤖 Automated Template Build |
| 105 | +
|
| 106 | + This PR contains the rendered template from the latest changes on the \`template\` branch. |
| 107 | +
|
| 108 | + **Source commit:** \`${{ steps.template_sha.outputs.sha }}\` |
| 109 | + **Build timestamp:** ${{ github.event.head_commit.timestamp }} |
| 110 | +
|
| 111 | + ### ✅ Validation |
| 112 | + - Template tests passed |
| 113 | + - Pre-commit hooks validated" |
| 114 | + else |
| 115 | + echo "Failed to create PR" |
| 116 | + cat /tmp/pr-output.txt |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + else |
| 120 | + echo "PR created successfully" |
| 121 | + fi |
0 commit comments