11name : Build and stage
2+
23on :
34 push :
45 branches :
56 - main
67 pull_request :
78 branches :
89 - main
9- env :
10- DOTNET_CLI_TELEMETRY_OPTOUT : 1
11- DOTNET_SKIP_FIRST_TIME_EXPERIENCE : true
12- DOTNET_VERSION : ' 8.0'
1310
1411concurrency :
1512 group : ${{ github.workflow }}-${{ github.ref }}
@@ -18,11 +15,17 @@ concurrency:
1815permissions :
1916 checks : write
2017 contents : read
18+ pull-requests : write
2119 statuses : write
2220
2321jobs :
2422 build :
2523 name : Build and publish app
24+ env :
25+ DOTNET_CLI_TELEMETRY_OPTOUT : 1
26+ DOTNET_NOLOGO : true
27+ DOTNET_VERSION : ' 8.0'
28+ ASPNETCORE_ENVIRONMENT : Staging
2629 runs-on : ubuntu-latest
2730 steps :
2831 - uses : actions/checkout@v4
3942 with :
4043 path : ~/.nuget/packages
4144 key : ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
42- restore-keys : |
43- ${{ runner.os }}-nuget-
45+ restore-keys : ${{ runner.os }}-nuget-
4446
4547 - name : dotnet restore
4648 run : dotnet restore
5254 run : dotnet test --logger trx --results-directory "${{ runner.temp }}" --no-build
5355
5456 - name : dotnet test
55- uses : NasAmin/trx-parser@v0.5 .0
57+ uses : NasAmin/trx-parser@v0.6 .0
5658 with :
5759 TRX_PATH : " ${{ runner.temp }}"
5860 REPO_TOKEN : " ${{ secrets.GITHUB_TOKEN }}"
@@ -61,34 +63,61 @@ jobs:
6163 run : dotnet publish src/InitializrService/Steeltoe.InitializrService.csproj -o publish
6264
6365 - name : Upload artifact for deployment job
64- if : ${{ github.event_name != 'pull_request' }}
6566 uses : actions/upload-artifact@v4
6667 with :
6768 name : published-app
6869 path : publish
6970
7071 deploy :
7172 name : Deploy
72- environment : Staging
73- needs :
74- - build
73+ if : ${{ github.secret_source == 'Actions' }}
74+ environment :
75+ name : ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
76+ url : ${{ steps.deploy-to-webapp.outputs.webapp-url }}
77+ env :
78+ SLOT_NAME : ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
79+ needs : build
7580 runs-on : ubuntu-latest
76- if : ${{ github.event_name != 'pull_request' }}
7781 steps :
7882 - name : Download artifact from build job
7983 uses : actions/download-artifact@v4
8084 with :
8185 name : published-app
8286
83- - name : Log into Azure CLI with service principal
84- uses : azure/login@v1
87+ - name : Login to Azure
88+ uses : azure/login@v2
8589 with :
8690 creds : ${{ secrets.AZURE_CREDENTIALS }}
8791
88- - name : Deploy to Azure Web App
92+ - name : If PR, create a new staging slot
93+ if : ${{ github.event_name == 'pull_request' }}
94+ 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 }}
95+
96+ - name : Deploy to staging slot
8997 id : deploy-to-webapp
98+ if : ${{ github.secret_source == 'Actions' }}
9099 uses : azure/webapps-deploy@v3
91100 with :
92101 app-name : ${{ vars.AZURE_WEBAPP_NAME }}
93102 package : ' .'
94103 slot-name : ${{ vars.STAGING_SLOT_NAME }}
104+
105+ - name : If PR, comment with the preview link
106+ if : ${{ github.event_name == 'pull_request' }}
107+ uses : mshick/add-pr-comment@v2
108+ with :
109+ message : |
110+ ## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net
111+
112+ - Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch.
113+ - The preview link is shareable, but will be deleted when the pull request is merged or closed.
114+
115+ ### Need to pair with a custom NetCoreToolService address?
116+
117+ A team member can use this az CLI command, replacing <this-PR-number> and <other-pr-number> for valid numbers:
118+ ```bash
119+ az webapp config appsettings set --resource-group steeltoe --name initializr-service --slot pr-<this-PR-number> --settings InitializrService__NetCoreToolService__Uri=https://netcoretoolservice-pr-<other-pr-number>.azurewebsites.net/api
120+ ```
121+
122+ > *This is an automated message.*
123+ repo-token : ${{ secrets.GITHUB_TOKEN }}
0 commit comments