Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workflow updates #132

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions .github/workflows/build-and-stage.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
name: Build and stage

on:
pull_request:
push:
branches:
- main
push:
pull_request:
branches:
- main
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: 'read'
pull-requests: 'write'
contents: read
pull-requests: write

env:
IMAGE_NAME: initializr-web
IMAGE_TAG: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || github.run_id }}
HAS_AZURE_CREDENTIALS: secrets.AZURE_CREDENTIALS != ''

jobs:
build-push-deploy:
build-push:
name: Build and push image
environment:
name: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
env:
SLOT_NAME: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Login to Azure
if: ${{ env.HAS_AZURE_CREDENTIALS }}
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Login to container registry
if: ${{ env.HAS_AZURE_CREDENTIALS }}
uses: azure/docker-login@v1
with:
login-server: "${{ vars.DOCKER_REGISTRY }}"
Expand All @@ -51,32 +38,52 @@ jobs:
run: docker build . --file "Dockerfile" -t ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}

- name: Push image
if: ${{ env.HAS_AZURE_CREDENTIALS }}
run: docker push ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}

deploy:
if: ${{ github.secret_source == 'Actions' }}
name: Deploy
environment:
name: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
env:
SLOT_NAME: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
needs: build-push
runs-on: ubuntu-latest
steps:
- name: Login to Azure
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: If PR, create a new staging slot
if: ${{ github.event_name == 'pull_request' && env.HAS_AZURE_CREDENTIALS }}
if: ${{ github.event_name == 'pull_request' }}
run: az webapp deployment slot create --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }} --configuration-source ${{ vars.STAGING_SLOT_NAME }}

- name: Deploy to staging slot
if: ${{ env.HAS_AZURE_CREDENTIALS }}
uses: azure/webapps-deploy@v3
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: ${{ vars.AZURE_WEBAPP_NAME }}
images: ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
slot-name: ${{ env.SLOT_NAME }}

- name: If PR, comment with the preview link
if: ${{ github.event_name == 'pull_request' && env.HAS_AZURE_CREDENTIALS }}
if: ${{ github.event_name == 'pull_request' }}
uses: mshick/add-pr-comment@v2
with:
message: |
## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net

- Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch.
- The preview link is shareable, but will be deleted when the pull request is merged or closed.

### Need to pair with a custom InitializrService address?

A team member can use this az CLI command, replacing <this-PR-number> and <other-pr-number> for valid numbers:
```bash
az webapp config appsettings set --resource-group steeltoe --name initializr-web --slot pr-<this-PR-number> --settings "INITIALIZR_SERVICE_HOST=initializr-service-pr-<other-pr-number>.azurewebsites.net" "INITIALIZR_SERVICE_URI=https://initializr-service-pr-<other-pr-number>.azurewebsites.net/api/"
```

> *This is an automated message.*
repo-token: ${{ secrets.GITHUB_TOKEN }}

5 changes: 3 additions & 2 deletions .github/workflows/pr-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Delete a preview environment

on:
pull_request:
types: [closed]
types:
- closed

env:
SLOT_NAME: pr-${{ github.event.number }}
Expand All @@ -13,7 +14,7 @@ jobs:

steps:
- name: Log into Azure CLI with service principal
uses: azure/login@v1
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

Expand Down
21 changes: 19 additions & 2 deletions .github/workflows/stage-prod-swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@ jobs:
name: Promote to production
runs-on: ubuntu-latest
environment:
name: 'Production'
name: Production
url: 'https://${{ vars.AZURE_WEBAPP_NAME }}.azurewebsites.net/'

steps:
- name: Log into Azure CLI with service principal
uses: azure/login@v1
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Change InitializrService link to prod and restart
shell: bash
run: |
az webapp config appsettings set --resource-group steeltoe --name initializr-web --slot Staging --settings "INITIALIZR_SERVICE_HOST=initializr-service.azurewebsites.net" "INITIALIZR_SERVICE_URI=https://initializr-service.azurewebsites.net/api/"
az webapp restart --resource-group steeltoe --name initializr-web --slot Staging
echo "Waiting 60 seconds until beginning to curl to see if the site is back up"
sleep 60
until $(curl --output /dev/null --silent --head --fail https://start-staging.steeltoe.io); do
printf '.'
sleep 5
done

- name: Swap slots
run: az webapp deployment slot swap -s ${{ vars.STAGING_SLOT_NAME }} -n ${{ vars.AZURE_WEBAPP_NAME }} -g ${{ vars.AZURE_RESOURCE_GROUP }}

- name: Change InitializrService link to staging and restart
run: |
az webapp config appsettings set --resource-group steeltoe --name initializr-web --slot Staging --settings "INITIALIZR_SERVICE_HOST=initializr-service-staging.azurewebsites.net" "INITIALIZR_SERVICE_URI=https://initializr-service-staging.azurewebsites.net/api/"
az webapp restart --resource-group steeltoe --name initializr-web --slot Staging