Skip to content

Commit d17eeff

Browse files
authored
Switch back to running in a container, add pr workflow (#10)
1 parent 15ab821 commit d17eeff

File tree

5 files changed

+107
-66
lines changed

5 files changed

+107
-66
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ indent_size = 4
2121

2222
[*.json]
2323
indent_size = 2
24+
25+
[Dockerfile,*.sh]
26+
end_of_line = lf

.github/workflows/build-and-stage.yml

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,73 @@ on:
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

1410
concurrency:
1511
group: ${{ github.workflow }}-${{ github.ref }}
1612
cancel-in-progress: true
1713

1814
permissions:
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

2322
jobs:
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-
uses: NasAmin/trx-parser@v0.5.0
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+

.github/workflows/pr-cleanup.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Delete a preview environment
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
env:
8+
SLOT_NAME: pr-${{ github.event.number }}
9+
10+
jobs:
11+
delete-slot:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Log into Azure CLI with service principal
16+
uses: azure/login@v1
17+
with:
18+
creds: ${{ secrets.AZURE_CREDENTIALS }}
19+
20+
- name: Delete slot on staging site
21+
run: az webapp deployment slot delete --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }}
22+
23+
delete-deployment:
24+
runs-on: ubuntu-latest
25+
26+
steps:
27+
- name: Delete Deployment Environment
28+
uses: strumwolf/delete-deployment-environment@v2
29+
with:
30+
environment: "pr-${{ github.event.number }}"
31+
token: ${{ secrets.GITHUB_TOKEN }}
32+
onlyRemoveDeployments: true

.github/workflows/stage-prod-swap.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Swap staging slot contents into production
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
promote-to-production:
8+
name: Promote to production
9+
runs-on: ubuntu-latest
10+
environment:
11+
name: 'Production'
12+
url: 'https://${{ vars.AZURE_WEBAPP_NAME }}.azurewebsites.net/'
13+
14+
steps:
15+
- name: Log into Azure CLI with service principal
16+
uses: azure/login@v1
17+
with:
18+
creds: ${{ secrets.AZURE_CREDENTIALS }}
19+
20+
- name: Swap slots
21+
run: az webapp deployment slot swap -s ${{ vars.STAGING_SLOT_NAME }} -n ${{ vars.AZURE_WEBAPP_NAME }} -g ${{ vars.AZURE_RESOURCE_GROUP }}
22+

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
2+
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
33
WORKDIR /source
44
COPY . .
55
RUN dotnet restore
66
RUN dotnet publish -c release -o /srv --no-restore
77

8-
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine
9-
ARG templates_version=1.3.0-g4205821715
10-
RUN dotnet nuget add source https://pkgs.dev.azure.com/dotnet/Steeltoe/_packaging/dev/nuget/v3/index.json -n SteeltoeDev
8+
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine
9+
ARG templates_version=1.3.0
10+
#RUN dotnet nuget add source https://pkgs.dev.azure.com/dotnet/Steeltoe/_packaging/dev/nuget/v3/index.json -n SteeltoeDev
1111
RUN dotnet new --install Steeltoe.NetCoreTool.Templates::${templates_version} &&\
1212
dotnet new --list | grep steeltoe-webapi
1313
# WORKDIR /usr/local/src

0 commit comments

Comments
 (0)