Skip to content

Commit

Permalink
add PR deployments, update actions, align styles
Browse files Browse the repository at this point in the history
also ensure prod points at prod and staging points at staging when doing slot swaps
  • Loading branch information
TimHess committed Feb 28, 2025
1 parent f3e5484 commit 81d473c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 25 deletions.
62 changes: 39 additions & 23 deletions .github/workflows/build-and-stage.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
name: Build and stage

on:
push:
branches:
- main
pull_request:
branches:
- main
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_VERSION: '8.0'

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

permissions:
checks: write
contents: read
statuses: write
pull-requests: write

jobs:
build:
name: Build and publish app
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_NOLOGO: true
DOTNET_VERSION: '8.0'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -39,8 +39,7 @@ jobs:
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-nuget-
restore-keys: ${{ runner.os }}-nuget-

- name: dotnet restore
run: dotnet restore
Expand All @@ -49,46 +48,63 @@ jobs:
run: dotnet build --no-restore /p:TreatWarningsAsErrors=True

- name: dotnet test
run: dotnet test --logger trx --results-directory "${{ runner.temp }}" --no-build

- name: dotnet test
uses: NasAmin/[email protected]
with:
TRX_PATH: "${{ runner.temp }}"
REPO_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: dotnet test --no-build --configuration Release --collect:"XPlat Code Coverage" --logger "GitHubActions;summary.includeSkippedTests=true"

- name: dotnet publish
run: dotnet publish src/InitializrService/Steeltoe.InitializrService.csproj -o publish

- name: Upload artifact for deployment job
if: ${{ github.event_name != 'pull_request' }}
uses: actions/upload-artifact@v4
with:
name: published-app
path: publish

deploy:
name: Deploy
environment: Staging
needs:
- build
if: ${{ github.secret_source == 'Actions' }}
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
runs-on: ubuntu-latest
if: ${{ github.event_name != 'pull_request' }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: published-app

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

- name: Deploy to Azure Web App
- name: If PR, create a new staging slot
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
id: deploy-to-webapp
if: ${{ github.secret_source == 'Actions' }}
uses: azure/webapps-deploy@v3
with:
app-name: ${{ vars.AZURE_WEBAPP_NAME }}
package: '.'
slot-name: ${{ vars.STAGING_SLOT_NAME }}

# Need to pair with a custom NetCoreToolService address?
# az webapp config appsettings set --resource-group steeltoe --name initializr-service --slot pr-11 --settings InitializrService__NetCoreToolService__Uri=https://netcoretoolservice-pr-11.azurewebsites.net/api

- name: If PR, comment with the preview link
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.
> *This is an automated message.*
repo-token: ${{ secrets.GITHUB_TOKEN }}
33 changes: 33 additions & 0 deletions .github/workflows/pr-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Delete a preview environment

on:
pull_request:
types:
- closed

env:
SLOT_NAME: pr-${{ github.event.number }}

jobs:
delete-slot:
runs-on: ubuntu-latest

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

- name: Delete slot on staging site
run: az webapp deployment slot delete --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }}

delete-deployment:
runs-on: ubuntu-latest
permissions: write-all

steps:
- name: Delete Deployment Environment
uses: strumwolf/delete-deployment-environment@v3
with:
environment: "pr-${{ github.event.number }}"
token: ${{ secrets.GITHUB_TOKEN }}
21 changes: 19 additions & 2 deletions .github/workflows/stage-prod-swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ jobs:

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

- name: Change NetCoreToolService link to prod and restart
shell: bash
run: |
az webapp config appsettings set --resource-group steeltoe --name initializr-service --slot Staging --settings InitializrService__NetCoreToolService__Uri=https://netcoretoolservice.azurewebsites.net/api
az webapp restart --resource-group steeltoe --name initializr-service --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://initializr-service-staging.azurewebsites.net/actuator/info); 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 }}
run: az webapp deployment slot swap -s ${{ vars.STAGING_SLOT_NAME }} -n ${{ vars.AZURE_WEBAPP_NAME }} -g ${{ vars.AZURE_RESOURCE_GROUP }}

- name: Change NetCoreToolService link to staging and restart
run: |
az webapp config appsettings set --resource-group steeltoe --name initializr-service --slot Staging --settings InitializrService__NetCoreToolService__Uri=https://netcoretoolservice-staging.azurewebsites.net/api
az webapp restart --resource-group steeltoe --name initializr-service --slot Staging

0 comments on commit 81d473c

Please sign in to comment.