ASP.Net 7 deployment to Azure App Services using GitHub Actions
This is a simple reference post of the GitHub Actions yaml that I’m using to deploy a simple ASP.Net 7.0 to Azure App Services.
I was not planning to post anything today as this task was supposed a very simple Deployment Center configuration. But I ended up spending close to two hours troubleshooting due to the ff:
- The deployment center will generate an older GitHub actions YAML template (old .Net version and old action step versions)
- The same
dotnet <command>
that executes locally does not always result in the same behavior in actions. Part of the reason is because the generated YAML defaulted to thev1
ofactions/setup-dotnet
when the latest isv3
. - Online references I found at this time assume the source to be in the root directory of the repo. Setting
working-directory
to asrc
still resulted in some challenges.
For context, the my repo file structure is as follows
1
2
3
4
5
6
7
8
root
└───.github
│ └───workflows
│ │ ci.yml
└───src
│ RazType.csproj
│ RazType.sln
| **
So this is the yaml file that eventually worked. I added my learnings as in-line YAML comments. Hope this helps someone other than me :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
name: Build and deploy ASP.Net Core app to Azure Web App
on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
pull_request:
branches: [ main ]
env:
DOTNET_VERSION: '7.0.x' # Azure App Services Deployment Center defaults to 3.1. Docs mention 5.x and 6.x, but 7.0.x works for as of today (Jan 2023)
DOTNET_QUALITY: 'preview'
WORKING_DIR: src
BUILD_CONFIG: Debug
PROJECT_FILE: RazType.csproj # `dotnet publish` requires the does not work when specifying the .sln file
APPSERVICE_NAME: raztype
APPSERVICE_SLOT: Production
PACKAGE_PATH: '/publish' # Specifying '/' at the beginning of the path is required due to differences in action working-directory.
jobs:
build:
runs-on: windows-latest
defaults:
run:
# Since the directory is not in root, specify working directory for the dotnet commands to work.
working-directory: $
steps:
- uses: actions/checkout@v3
- name: Set up .NET 7.0.x
uses: actions/setup-dotnet@v3
with:
dotnet-version: $
dotnet-quality: $
- name: Restore Dependencies
# Split build step to restore and build (with --no-restore)
run: dotnet restore
- name: Build the .NET Solution
run: dotnet build -c $ --no-restore
- name: Test
run: dotnet test --no-build --no-restore --verbosity normal
- name: dotnet publish
# Azure App Services defaults to '-o $/myapp'
run: dotnet publish -c $ -o $
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
with:
name: .net-app
# Azure App Services defaults to $/myapp
path: $
deploy:
runs-on: windows-latest
defaults:
run:
working-directory: $
needs: build
environment:
name: 'Production'
url: $
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: .net-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v2
with:
app-name: $
slot-name: $
publish-profile: $
# Path = "." is the default and is the one that's working. The downloaded artifact does not contain the package folder of $PACKAGE_PATH
package: .
This post is licensed under CC BY 4.0 by the author.