|
1 | | -name: Build and deploy containers to Azure |
| 1 | +name: Deploy API Image to Azure App Service |
2 | 2 |
|
3 | 3 | on: |
4 | | - push: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build and Push Docker Images"] |
| 6 | + types: [ completed ] |
5 | 7 | branches: [ main ] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +env: |
| 11 | + AZURE_WEBAPP_NAME: tps-app-scripting-editor |
| 12 | + AZURE_RESOURCE_GROUP: tps-app-scripting-rg |
| 13 | + REGISTRY: tpsappscriptingacr.azurecr.io |
| 14 | + IMAGE_API: app-scripting-editor-api |
| 15 | + # optional: use your Front Door host for health check; leave blank to use default *.azurewebsites.net |
| 16 | + FRONTDOOR_HOST: app-scripting-editor.trackmangolfdev.com |
6 | 17 |
|
7 | 18 | jobs: |
8 | | - build-and-push: |
| 19 | + deploy: |
9 | 20 | runs-on: ubuntu-latest |
10 | | - env: |
11 | | - REGISTRY: tpsappscriptingacr.azurecr.io |
12 | | - outputs: |
13 | | - registry: ${{ steps.registry.outputs.registry }} |
14 | | - steps: |
15 | | - - uses: actions/checkout@v4 |
16 | | - |
17 | | - - name: Assert required ACR secrets are set |
18 | | - run: | |
19 | | - if [ -z "${{ secrets.ACR_USERNAME }}" ]; then echo "ERROR: secrets.ACR_USERNAME is not set"; exit 1; fi |
20 | | - if [ -z "${{ secrets.ACR_PASSWORD }}" ]; then echo "ERROR: secrets.ACR_PASSWORD is not set"; exit 1; fi |
| 21 | + permissions: |
| 22 | + id-token: write |
| 23 | + contents: read |
21 | 24 |
|
22 | | - - name: Determine registry to use |
23 | | - id: registry |
24 | | - run: | |
25 | | - if [ -n "${{ secrets.ACR_LOGIN_SERVER }}" ]; then echo "registry=${{ secrets.ACR_LOGIN_SERVER }}" >> $GITHUB_OUTPUT; else echo "registry=${{ env.REGISTRY }}" >> $GITHUB_OUTPUT; fi |
26 | | -
|
27 | | - - name: Log in to Azure Container Registry |
28 | | - uses: azure/docker-login@v1 |
29 | | - with: |
30 | | - login-server: ${{ steps.registry.outputs.registry }} |
31 | | - username: ${{ secrets.ACR_USERNAME }} |
32 | | - password: ${{ secrets.ACR_PASSWORD }} |
33 | | - |
34 | | - - name: Build and push editor image |
35 | | - run: | |
36 | | - docker build \ |
37 | | - --build-arg VITE_APP_BUILD_TIME=${{ github.event.head_commit.timestamp }} \ |
38 | | - --build-arg VITE_APP_COMMIT_SHA=${{ github.sha }} \ |
39 | | - --build-arg VITE_APP_VERSION=1.0.0 \ |
40 | | - -t ${{ steps.registry.outputs.registry }}/app-scripting-editor:latest . |
41 | | - docker push ${{ steps.registry.outputs.registry }}/app-scripting-editor:latest |
42 | | -
|
43 | | - - name: Build and push editor-api image |
44 | | - run: | |
45 | | - docker build \ |
46 | | - --build-arg REGISTRY=${{ steps.registry.outputs.registry }} \ |
47 | | - --build-arg VITE_APP_BUILD_TIME=${{ github.event.head_commit.timestamp }} \ |
48 | | - --build-arg VITE_APP_COMMIT_SHA=${{ github.sha }} \ |
49 | | - --build-arg VITE_APP_VERSION=1.0.0 \ |
50 | | - -t ${{ steps.registry.outputs.registry }}/app-scripting-editor-api:latest -f server/Dockerfile . |
51 | | - docker push ${{ steps.registry.outputs.registry }}/app-scripting-editor-api:latest |
52 | | -
|
53 | | - deploy-to-appservice: |
54 | | - needs: build-and-push |
55 | | - runs-on: ubuntu-latest |
56 | 25 | steps: |
57 | | - - name: Check required App Service secrets |
58 | | - id: check-secrets |
59 | | - run: | |
60 | | - if [ -z "${{ secrets.EDITOR_APP_NAME }}" ] || [ -z "${{ secrets.RESOURCE_GROUP }}" ] || [ -z "${{ secrets.EDITOR_API_APP_NAME }}" ]; then |
61 | | - echo "deploy_ready=false" >> $GITHUB_OUTPUT |
62 | | - echo "One or more App Service secrets are missing; skipping deploy steps." |
63 | | - else |
64 | | - echo "deploy_ready=true" >> $GITHUB_OUTPUT |
65 | | - fi |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v4 |
66 | 28 |
|
67 | | - - name: Azure Login |
68 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
69 | | - uses: azure/login@v1 |
| 29 | + - name: Azure login (OIDC) |
| 30 | + uses: azure/login@v2 |
70 | 31 | with: |
71 | | - creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 32 | + client-id: ${{ secrets.AZURE_CLIENT_ID }} |
| 33 | + tenant-id: ${{ secrets.AZURE_TENANT_ID }} |
| 34 | + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
72 | 35 |
|
73 | | - - name: Deploy editor to App Service (container) |
74 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
| 36 | + - name: Set API image on App Service |
75 | 37 | run: | |
76 | | - # Deploy the API image to the public editor App Service so the same hostname serves the SPA and the /api endpoints. |
77 | | - az webapp config container set --name ${{ secrets.EDITOR_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} --docker-custom-image-name ${{ needs.build-and-push.outputs.registry }}/app-scripting-editor-api:latest |
| 38 | + az webapp config container set --name "${{ env.AZURE_WEBAPP_NAME }}" --resource-group "${{ env.AZURE_RESOURCE_GROUP }}" --docker-custom-image-name "${{ env.REGISTRY }}/${{ env.IMAGE_API }}:latest" --docker-registry-server-url "https://${{ env.REGISTRY }}" --docker-registry-server-user "${{ secrets.ACR_USERNAME }}" --docker-registry-server-password "${{ secrets.ACR_PASSWORD }}" |
78 | 39 |
|
79 | | - - name: Set WEBSITES_PORT for editor (public) app |
80 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
| 40 | + - name: Ensure WEBSITES_PORT=4000 (Express listens here) |
81 | 41 | run: | |
82 | | - echo "Setting WEBSITES_PORT=4000 for ${{ secrets.EDITOR_APP_NAME }}" |
83 | | - az webapp config appsettings set --name ${{ secrets.EDITOR_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} --settings WEBSITES_PORT=4000 |
| 42 | + az webapp config appsettings set --name "${{ env.AZURE_WEBAPP_NAME }}" --resource-group "${{ env.AZURE_RESOURCE_GROUP }}" --settings WEBSITES_PORT=4000 |
84 | 43 |
|
85 | | - - name: Deploy editor-api to App Service (container) |
86 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
| 44 | + - name: Restart App |
87 | 45 | run: | |
88 | | - az webapp config container set --name ${{ secrets.EDITOR_API_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} --docker-custom-image-name ${{ needs.build-and-push.outputs.registry }}/app-scripting-editor-api:latest |
| 46 | + az webapp restart --name "${{ env.AZURE_WEBAPP_NAME }}" --resource-group "${{ env.AZURE_RESOURCE_GROUP }}" |
89 | 47 |
|
90 | | - - name: Set WEBSITES_PORT for editor-api |
91 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
| 48 | + - name: Determine public host for health check |
| 49 | + id: host |
92 | 50 | run: | |
93 | | - echo "Setting WEBSITES_PORT=4000 for ${EDITOR_API_APP_NAME}" |
94 | | - az webapp config appsettings set --name ${{ secrets.EDITOR_API_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} --settings WEBSITES_PORT=4000 |
95 | | -
|
96 | | - - name: Smoke test editor-api |
97 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
98 | | - run: | |
99 | | - host=$(az webapp show --name ${{ secrets.EDITOR_API_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} --query defaultHostName -o tsv) |
100 | | - echo "Testing https://$host/api/health" |
101 | | - for i in 1 2 3 4 5 6 7 8 9 10; do |
102 | | - status=$(curl -s -o /dev/null -w "%{http_code}" "https://$host/api/health" || echo "000") |
103 | | - if [ "$status" = "200" ]; then |
104 | | - echo "OK: health returned 200" |
105 | | - exit 0 |
106 | | - fi |
107 | | - echo "Attempt $i: status=$status; retrying in 5s..." |
108 | | - sleep 5 |
109 | | - done |
110 | | - echo "ERROR: editor-api did not become healthy" |
111 | | - az webapp log tail --name ${{ secrets.EDITOR_API_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} & |
112 | | - exit 1 |
| 51 | + if [ -n "${{ env.FRONTDOOR_HOST }}" ]; then |
| 52 | + echo "host=${{ env.FRONTDOOR_HOST }}" >> "$GITHUB_OUTPUT" |
| 53 | + else |
| 54 | + host=$(az webapp show --name "${{ env.AZURE_WEBAPP_NAME }}" --resource-group "${{ env.AZURE_RESOURCE_GROUP }}" --query defaultHostName -o tsv) |
| 55 | + echo "host=${host}" >> "$GITHUB_OUTPUT" |
| 56 | + fi |
113 | 57 |
|
114 | | - - name: Smoke test public editor app |
115 | | - if: ${{ steps.check-secrets.outputs.deploy_ready == 'true' }} |
| 58 | + - name: Wait for /api/health = 200 |
116 | 59 | run: | |
117 | | - host=$(az webapp show --name ${{ secrets.EDITOR_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} --query defaultHostName -o tsv) |
118 | | - echo "Testing https://$host/api/health" |
119 | | - for i in 1 2 3 4 5 6 7 8 9 10; do |
120 | | - status=$(curl -s -o /dev/null -w "%{http_code}" "https://$host/api/health" || echo "000") |
121 | | - if [ "$status" = "200" ]; then |
122 | | - echo "OK: public editor health returned 200" |
| 60 | + set -e |
| 61 | + url="https://${{ steps.host.outputs.host }}/api/health" |
| 62 | + echo "Checking $url ..." |
| 63 | + for i in {1..20}; do |
| 64 | + code=$(curl -s -o /dev/null -w "%{http_code}" "$url" || echo "000") |
| 65 | + echo "Attempt $i -> $code" |
| 66 | + if [ "$code" = "200" ]; then |
| 67 | + echo "Healthy." |
123 | 68 | exit 0 |
124 | 69 | fi |
125 | | - echo "Attempt $i: status=$status; retrying in 5s..." |
126 | 70 | sleep 5 |
127 | 71 | done |
128 | | - echo "ERROR: public editor did not become healthy" |
129 | | - az webapp log tail --name ${{ secrets.EDITOR_APP_NAME }} --resource-group ${{ secrets.RESOURCE_GROUP }} & |
| 72 | + echo "ERROR: health never reached 200" |
130 | 73 | exit 1 |
0 commit comments