1+ name : Build, Test, and Deploy Try .NET API
2+
3+ on :
4+ push :
5+ branches : ["main"]
6+ pull_request :
7+ branches : ["main"]
8+ workflow_dispatch :
9+
10+ permissions :
11+ id-token : write
12+ contents : read
13+
14+ jobs :
15+ build-and-test :
16+ runs-on : ubuntu-latest
17+ environment : " BuildAndUploadImage"
18+
19+ steps :
20+ - uses : actions/checkout@v6
21+
22+ - name : Set up .NET
23+ uses : actions/setup-dotnet@v5
24+ with :
25+ global-json-file : global.json
26+
27+ - name : Set up dependency caching for faster builds
28+ uses : actions/cache@v5
29+ id : nuget-cache
30+ with :
31+ path : |
32+ ~/.nuget/packages
33+ ${{ github.workspace }}/**/obj/project.assets.json
34+ key : ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
35+ restore-keys : |
36+ ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
37+ ${{ runner.os }}-nuget-
38+
39+ - name : Restore with dotnet
40+ run : dotnet restore
41+
42+ - name : Build with dotnet
43+ run : dotnet build -p:ContinuousIntegrationBuild=True -p:ReleaseDateAttribute=True --configuration Release --no-restore
44+
45+ - name : Run .NET Tests
46+ run : dotnet test --no-build --configuration Release
47+
48+ - name : Set up Docker Buildx
49+ uses : docker/setup-buildx-action@v3
50+
51+ # Build but no push with a PR
52+ - name : Docker build (no push)
53+ if : github.event_name == 'pull_request' || github.event_name == 'merge_group'
54+ uses : docker/build-push-action@v6
55+ with :
56+ push : false
57+ tags : temp-pr-validation
58+ file : ./Dockerfile
59+
60+ - name : Build Container Image
61+ if : github.event_name != 'pull_request_target' && github.event_name != 'pull_request'
62+ uses : docker/build-push-action@v6
63+ with :
64+ tags : ${{ vars.DEVCONTAINER_REGISTRY }}/try:${{ github.sha }},${{ vars.DEVCONTAINER_REGISTRY }}/try:latest
65+ file : ./Dockerfile
66+ context : .
67+ outputs : type=docker,dest=${{ github.workspace }}/tryimage.tar
68+ cache-from : type=gha
69+ cache-to : type=gha,mode=max
70+
71+ - name : Upload artifact
72+ if : github.event_name != 'pull_request_target' && github.event_name != 'pull_request'
73+ uses : actions/upload-artifact@v6
74+ with :
75+ name : tryimage
76+ path : ${{ github.workspace }}/tryimage.tar
77+
78+ deploy-development :
79+ if : github.event_name != 'pull_request_target' && github.event_name != 'pull_request'
80+ runs-on : ubuntu-latest
81+ needs : build-and-test
82+ environment :
83+ name : " Development"
84+
85+ steps :
86+ - name : Azure Login
87+ uses : azure/login@v2
88+ with :
89+ client-id : ${{ secrets.ESSENTIALCSHARPDEV_CLIENT_ID }}
90+ tenant-id : ${{ secrets.ESSENTIALCSHARP_TENANT_ID }}
91+ subscription-id : ${{ secrets.ESSENTIALCSHARP_SUBSCRIPTION_ID }}
92+
93+ - name : Download artifact
94+ uses : actions/download-artifact@v7
95+ with :
96+ name : tryimage
97+ path : ${{ github.workspace }}
98+
99+ - name : Load image
100+ run : |
101+ docker load --input ${{ github.workspace }}/tryimage.tar
102+ docker image ls -a
103+
104+ - name : Log in to container registry
105+ uses : azure/CLI@v2
106+ env :
107+ REGISTRY_URL : ${{ vars.DEVCONTAINER_REGISTRY }}
108+ with :
109+ inlineScript : |
110+ REGISTRY_NAME=${REGISTRY_URL%.azurecr.io}
111+ az acr login --name $REGISTRY_NAME
112+
113+ - name : Push Image to Container Registry
114+ env :
115+ REGISTRY_URL : ${{ vars.DEVCONTAINER_REGISTRY }}
116+ run : docker push --all-tags $REGISTRY_URL/try
117+
118+ - name : Create and Deploy to Container App
119+ uses : azure/CLI@v2
120+ env :
121+ CONTAINER_APP_NAME : ${{ vars.CONTAINER_APP_NAME }}
122+ RESOURCEGROUP : ${{ vars.RESOURCEGROUP }}
123+ CONTAINER_APP_ENVIRONMENT : ${{ vars.CONTAINER_APP_ENVIRONMENT }}
124+ REGISTRY_URL : ${{ vars.DEVCONTAINER_REGISTRY }}
125+ SUBSCRIPTION_ID : ${{ secrets.ESSENTIALCSHARP_SUBSCRIPTION_ID }}
126+ MANAGED_IDENTITY_ID : ${{ secrets.MANAGED_IDENTITY_ID }}
127+ with :
128+ inlineScript : |
129+ az config set extension.use_dynamic_install=yes_without_prompt
130+ az extension add --name containerapp --upgrade
131+ az containerapp up \
132+ -n $CONTAINER_APP_NAME \
133+ -g $RESOURCEGROUP \
134+ --image $REGISTRY_URL/try:${{ github.sha }} \
135+ --environment $CONTAINER_APP_ENVIRONMENT \
136+ --registry-server $REGISTRY_URL \
137+ --ingress external \
138+ --target-port 8080 \
139+ --user-assigned /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCEGROUP/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$MANAGED_IDENTITY_ID \
140+ --registry-identity /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCEGROUP/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$MANAGED_IDENTITY_ID
141+
142+
143+ - name : Logout of Azure CLI
144+ if : always()
145+ uses : azure/CLI@v2
146+ with :
147+ inlineScript : |
148+ az logout
149+ az cache purge
150+ az account clear
0 commit comments