|
| 1 | +name: Build and Deploy Website |
| 2 | + |
| 3 | +on: |
| 4 | + push: # No branches filter, so runs on all branches |
| 5 | + pull_request: |
| 6 | + types: [ closed ] # Trigger on PR close |
| 7 | + branches: [ main ] # Only for PRs targeting main branch |
| 8 | + |
| 9 | +jobs: |
| 10 | + test-api: |
| 11 | + # This job runs on any branch push |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Test API endpoint |
| 15 | + id: test_api |
| 16 | + uses: fjogeleit/http-request-action@v1 |
| 17 | + with: |
| 18 | + url: '${{ secrets.API_BASE_URL }}/' |
| 19 | + method: 'GET' |
| 20 | + customHeaders: '{"Authorization": "Bearer ${{ secrets.API_TOKEN }}"}' |
| 21 | + |
| 22 | + - name: Check API response |
| 23 | + run: | |
| 24 | + echo "API Response Status: ${{ steps.test_api.outputs.status }}" |
| 25 | + echo "API Response Body: ${{ steps.test_api.outputs.response }}" |
| 26 | +
|
| 27 | + - name: Failed API response (not status code 200) |
| 28 | + if: ${{ steps.test_api.outputs.status != '200' }} |
| 29 | + run: | |
| 30 | + echo "Error: API returned status code ${{ steps.test_api.outputs.status }}, expected 200" |
| 31 | + exit 1 |
| 32 | +
|
| 33 | + build-and-deploy: |
| 34 | + # Only run this job when a PR is merged to main (not on direct pushes or when PR is closed without merging) |
| 35 | + if: github.event_name == 'pull_request' && github.event.pull_request.merged == true |
| 36 | + runs-on: ubuntu-latest |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Checkout code |
| 40 | + uses: actions/checkout@v3 |
| 41 | + |
| 42 | + - name: Build site |
| 43 | + id: build |
| 44 | + uses: fjogeleit/http-request-action@v1 |
| 45 | + with: |
| 46 | + url: '${{ secrets.API_BASE_URL }}/sites/${{ vars.SITE }}/build' |
| 47 | + method: 'POST' |
| 48 | + customHeaders: '{"Authorization": "Bearer ${{ secrets.API_TOKEN }}"}' |
| 49 | + |
| 50 | + - name: Check build status |
| 51 | + if: ${{ fromJson(steps.build.outputs.response).status != 0 }} |
| 52 | + run: | |
| 53 | + echo "Build failed with status ${{ fromJson(steps.build.outputs.response).status }}" |
| 54 | + echo "Output: ${{ fromJson(steps.build.outputs.response).output }}" |
| 55 | + exit 1 |
| 56 | +
|
| 57 | + - name: Deploy site if build succeeded |
| 58 | + id: deploy |
| 59 | + if: ${{ fromJson(steps.build.outputs.response).status == 0 }} |
| 60 | + uses: fjogeleit/http-request-action@v1 |
| 61 | + with: |
| 62 | + url: '${{ secrets.API_BASE_URL }}/sites/${{ vars.SITE }}/deploy' |
| 63 | + method: 'POST' |
| 64 | + customHeaders: '{"Authorization": "Bearer ${{ secrets.API_TOKEN }}"}' |
| 65 | + |
| 66 | + - name: Check deploy status |
| 67 | + if: ${{ fromJson(steps.deploy.outputs.response).status != 0 }} |
| 68 | + run: | |
| 69 | + echo "Deploy failed with status ${{ fromJson(steps.deploy.outputs.response).status }}" |
| 70 | + echo "Output: ${{ fromJson(steps.deploy.outputs.response).output }}" |
| 71 | + exit 1 |
| 72 | +
|
| 73 | + - name: Deployment summary |
| 74 | + if: ${{ fromJson(steps.deploy.outputs.response).status == 0 }} |
| 75 | + run: | |
| 76 | + echo "✅ Build and deployment completed successfully!" |
| 77 | + echo "Build output: ${{ fromJson(steps.build.outputs.response).output }}" |
| 78 | + echo "Deploy output: ${{ fromJson(steps.deploy.outputs.response).output }}" |
| 79 | +
|
0 commit comments