66 pull_request :
77 branches :
88 - main
9- env :
10- DOTNET_CLI_TELEMETRY_OPTOUT : 1
11- DOTNET_SKIP_FIRST_TIME_EXPERIENCE : true
12- DOTNET_VERSION : ' 8.0'
139
1410concurrency :
1511 group : ${{ github.workflow }}-${{ github.ref }}
1612 cancel-in-progress : true
1713
1814permissions :
19- checks : write
2015 contents : read
21- statuses : write
16+ pull-requests : ' write'
17+
18+ env :
19+ IMAGE_NAME : net-core-tool-service
20+ IMAGE_TAG : ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || github.run_id }}
2221
2322jobs :
24- build :
25- name : Build and publish app
23+ build-push-deploy :
24+ name : Build and push image
25+ environment :
26+ name : ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
27+ url : ${{ steps.deploy-to-webapp.outputs.webapp-url }}
28+ env :
29+ SLOT_NAME : ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
2630 runs-on : ubuntu-latest
2731 steps :
2832 - uses : actions/checkout@v4
2933 with :
3034 fetch-depth : 0 # avoid shallow clone so nbgv can do its work.
3135
32- - name : Set up .NET Core
33- uses : actions/setup-dotnet@v4
36+ - name : Login to Azure
37+ uses : azure/login@v1
3438 with :
35- dotnet-version : ${{ env.DOTNET_VERSION }}
39+ creds : ${{ secrets.AZURE_CREDENTIALS }}
3640
37- - name : Set up dependency caching for faster builds
38- uses : actions/cache@v4
41+ - name : Login to container registry
42+ uses : azure/docker-login@v1
3943 with :
40- path : ~/.nuget/packages
41- key : ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
42- restore-keys : |
43- ${{ runner.os }}-nuget-
44-
45- - name : dotnet restore
46- run : dotnet restore
44+ login-server : " ${{ vars.DOCKER_REGISTRY }}"
45+ username : " ${{ secrets.DOCKER_USERNAME }}"
46+ password : " ${{ secrets.DOCKER_PASSWORD }}"
4747
48- - name : dotnet build
49- run : dotnet build --no-restore /p:TreatWarningsAsErrors=True
48+ - name : Build image
49+ run : docker build . --file "Dockerfile" -t ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
5050
51- - name : dotnet test
52- run : dotnet test --logger trx --results-directory " ${{ runner.temp }}" --no-build
51+ - name : Push image
52+ run : docker push ${{ vars.DOCKER_REGISTRY }}/ ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
5353
54- - name : dotnet test
55- 56- with :
57- TRX_PATH : " ${{ runner.temp }}"
58- REPO_TOKEN : " ${{ secrets.GITHUB_TOKEN }}"
59-
60- - name : dotnet publish
61- run : dotnet publish src/NetCoreToolService/Steeltoe.NetCoreToolService.csproj -o publish
62-
63- - name : Upload artifact for deployment job
64- if : ${{ github.event_name != 'pull_request' }}
65- uses : actions/upload-artifact@v4
66- with :
67- name : published-app
68- path : publish
54+ - name : If PR, create a new staging slot
55+ if : ${{ github.event_name == 'pull_request' }}
56+ 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 }}
6957
70- deploy :
71- name : Deploy
72- environment : production
73- needs :
74- - build
75- runs-on : ubuntu-latest
76- if : ${{ github.event_name != 'pull_request' }}
77- steps :
78- - name : Download artifact from build job
79- uses : actions/download-artifact@v4
80- with :
81- name : published-app
82-
83- - name : Log into Azure CLI with service principal
84- uses : azure/login@v1
85- with :
86- creds : ${{ secrets.AZURE_CREDENTIALS }}
87-
88- - name : Deploy to Azure Web App
89- id : deploy-to-webapp
58+ - name : Deploy to staging slot
9059 uses : azure/webapps-deploy@v3
91- with :
60+ id : deploy-to-webapp
61+ with :
9262 app-name : ${{ vars.AZURE_WEBAPP_NAME }}
93- package : ' .'
94- slot-name : ' production'
63+ images : ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
64+ slot-name : ${{ env.SLOT_NAME }}
65+
66+ - name : If PR, comment with the preview link
67+ if : ${{ github.event_name == 'pull_request' }}
68+ uses : mshick/add-pr-comment@v2
69+ with :
70+ message : |
71+ ## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net
72+
73+ - Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch.
74+ - The preview link is shareable, but will be deleted when the pull request is merged or closed.
75+
76+ > *This is an automated message.*
77+ repo-token : ${{ secrets.GITHUB_TOKEN }}
78+
0 commit comments