Skip to content

Commit 21cfd96

Browse files
authored
Merge pull request #11 from SteeltoeOSS/workflows
Allow including templates from source during PR builds
2 parents f7baefd + 4e172f6 commit 21cfd96

7 files changed

+82
-37
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.github/ @TimHess @bart-vmware

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: Build and stage
2+
23
on:
34
push:
45
branches:
@@ -13,30 +14,36 @@ concurrency:
1314

1415
permissions:
1516
contents: read
16-
pull-requests: 'write'
17+
pull-requests: write
1718

1819
env:
1920
IMAGE_NAME: net-core-tool-service
2021
IMAGE_TAG: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || github.run_id }}
2122

2223
jobs:
23-
build-push-deploy:
24+
build-push:
2425
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 }}
3026
runs-on: ubuntu-latest
3127
steps:
3228
- uses: actions/checkout@v4
3329
with:
3430
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.
3531

36-
- name: Login to Azure
37-
uses: azure/login@v1
38-
with:
39-
creds: ${{ secrets.AZURE_CREDENTIALS }}
32+
- name: Detect template source from PR body
33+
env:
34+
PullRequestBody: ${{ github.event.pull_request.body }}
35+
run: |
36+
cat << EOF > /tmp/pull_request_body.txt
37+
$PullRequestBody
38+
EOF
39+
40+
CheckoutTarget=$(grep "template_checkout_target=" /tmp/pull_request_body.txt | awk -F= '{print $2}')
41+
if [ "$CheckoutTarget" = "" ]; then
42+
echo "Did not find a checkout target for templates."
43+
else
44+
echo "Found checkout target '$CheckoutTarget' in PR body, this build will use templates from source."
45+
echo "TEMPLATE_CHECKOUT_TARGET=$CheckoutTarget" >> $GITHUB_ENV
46+
fi
4047
4148
- name: Login to container registry
4249
uses: azure/docker-login@v1
@@ -46,33 +53,48 @@ jobs:
4653
password: "${{ secrets.DOCKER_PASSWORD }}"
4754

4855
- name: Build image
49-
run: docker build . --file "Dockerfile" -t ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
56+
run: docker build . -t ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} --build-arg TEMPLATE_CHECKOUT_TARGET=${{ env.TEMPLATE_CHECKOUT_TARGET }}
5057

5158
- name: Push image
5259
run: docker push ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
5360

61+
deploy:
62+
name: Deploy
63+
if: ${{ github.secret_source == 'Actions' }}
64+
environment:
65+
name: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
66+
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
67+
env:
68+
SLOT_NAME: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
69+
needs: build-push
70+
runs-on: ubuntu-latest
71+
steps:
72+
- name: Login to Azure
73+
uses: azure/login@v2
74+
with:
75+
creds: ${{ secrets.AZURE_CREDENTIALS }}
76+
5477
- name: If PR, create a new staging slot
5578
if: ${{ github.event_name == 'pull_request' }}
5679
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 }}
5780

5881
- name: Deploy to staging slot
59-
uses: azure/webapps-deploy@v3
6082
id: deploy-to-webapp
61-
with:
83+
uses: azure/webapps-deploy@v3
84+
with:
6285
app-name: ${{ vars.AZURE_WEBAPP_NAME }}
6386
images: ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
6487
slot-name: ${{ env.SLOT_NAME }}
65-
88+
6689
- name: If PR, comment with the preview link
6790
if: ${{ github.event_name == 'pull_request' }}
6891
uses: mshick/add-pr-comment@v2
6992
with:
7093
message: |
71-
## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net
72-
94+
## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net
95+
7396
- Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch.
7497
- The preview link is shareable, but will be deleted when the pull request is merged or closed.
7598
7699
> *This is an automated message.*
77100
repo-token: ${{ secrets.GITHUB_TOKEN }}
78-

.github/workflows/pr-cleanup.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: Delete a preview environment
22

33
on:
44
pull_request:
5-
types: [closed]
5+
types:
6+
- closed
67

78
env:
89
SLOT_NAME: pr-${{ github.event.number }}
@@ -13,10 +14,10 @@ jobs:
1314

1415
steps:
1516
- name: Log into Azure CLI with service principal
16-
uses: azure/login@v1
17+
uses: azure/login@v2
1718
with:
1819
creds: ${{ secrets.AZURE_CREDENTIALS }}
19-
20+
2021
- name: Delete slot on staging site
2122
run: az webapp deployment slot delete --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }}
2223

@@ -25,8 +26,8 @@ jobs:
2526

2627
steps:
2728
- name: Delete Deployment Environment
28-
uses: strumwolf/delete-deployment-environment@v2
29+
uses: strumwolf/delete-deployment-environment@v3
2930
with:
3031
environment: "pr-${{ github.event.number }}"
3132
token: ${{ secrets.GITHUB_TOKEN }}
32-
onlyRemoveDeployments: true
33+
onlyRemoveDeployments: true

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ jobs:
88
name: Promote to production
99
runs-on: ubuntu-latest
1010
environment:
11-
name: 'Production'
11+
name: Production
1212
url: 'https://${{ vars.AZURE_WEBAPP_NAME }}.azurewebsites.net/'
1313

1414
steps:
1515
- name: Log into Azure CLI with service principal
16-
uses: azure/login@v1
16+
uses: azure/login@v2
1717
with:
1818
creds: ${{ secrets.AZURE_CREDENTIALS }}
1919

2020
- name: Swap slots
2121
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: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
33
WORKDIR /source
44
COPY . .
5-
RUN dotnet restore
6-
RUN dotnet publish -c release -o /srv --no-restore
5+
RUN dotnet restore src/NetCoreToolService
6+
RUN dotnet build src/NetCoreToolService --configuration Release --no-restore
7+
RUN dotnet publish src/NetCoreToolService --output /srv --no-build
78

89
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
11-
RUN dotnet new --install Steeltoe.NetCoreTool.Templates::${templates_version} &&\
12-
dotnet new --list | grep steeltoe-webapi
13-
# WORKDIR /usr/local/src
14-
# RUN git clone https://github.com/SteeltoeOSS/NetCoreToolTemplates
15-
# RUN git -C NetCoreToolTemplates checkout release/1.2
16-
# RUN dotnet new --install NetCoreToolTemplates/src/Content
10+
ARG templates_version=1.4.0
11+
ARG TEMPLATE_CHECKOUT_TARGET
1712
WORKDIR /srv
1813
COPY --from=build /srv .
19-
ENV DOTNET_URLS http://0.0.0.0:80
14+
COPY install-template.sh /srv/install-template.sh
15+
RUN chmod +x /srv/install-template.sh
16+
RUN /srv/install-template.sh
17+
ENV DOTNET_URLS=http://0.0.0.0:80
2018
ENTRYPOINT ["dotnet", "Steeltoe.NetCoreToolService.dll"]

Steeltoe.NetCoreToolService.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1919
Directory.Build.props = Directory.Build.props
2020
docker-compose.yaml = docker-compose.yaml
2121
Dockerfile = Dockerfile
22+
install-template.sh = install-template.sh
2223
stylecop.json = stylecop.json
2324
Version.props = Version.props
2425
EndProjectSection
2526
EndProject
27+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
28+
EndProject
29+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{4D3C2BC0-3714-42D2-BB89-057E57D92BAF}"
30+
ProjectSection(SolutionItems) = preProject
31+
.github\workflows\build-and-stage.yml = .github\workflows\build-and-stage.yml
32+
.github\workflows\pr-cleanup.yml = .github\workflows\pr-cleanup.yml
33+
.github\workflows\stage-prod-swap.yml = .github\workflows\stage-prod-swap.yml
34+
EndProjectSection
35+
EndProject
2636
Global
2737
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2838
Debug|Any CPU = Debug|Any CPU
@@ -64,6 +74,7 @@ Global
6474
GlobalSection(NestedProjects) = preSolution
6575
{1462EDFE-F1FC-48C2-80C1-917317EE3C97} = {C742A7B8-80CA-4365-85CA-C29AA744CE54}
6676
{6BD6C793-E555-475F-A2E1-12D7F3F56A3B} = {410C0E72-737F-4168-AECA-2F6D19EE86D5}
77+
{4D3C2BC0-3714-42D2-BB89-057E57D92BAF} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
6778
EndGlobalSection
6879
GlobalSection(ExtensibilityGlobals) = postSolution
6980
SolutionGuid = {D8EFB01A-92BF-418B-B2B2-A12045B772E2}

install-template.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env sh
2+
3+
# dotnet nuget add source https://pkgs.dev.azure.com/dotnet/Steeltoe/_packaging/dev/nuget/v3/index.json -n SteeltoeDev
4+
5+
if [[ -z "$TEMPLATE_CHECKOUT_TARGET" ]] ;then
6+
dotnet new install Steeltoe.NetCoreTool.Templates::${templates_version} &&\
7+
dotnet new --list | grep steeltoe-webapi
8+
else
9+
cd /usr/local/src
10+
git clone https://github.com/SteeltoeOSS/NetCoreToolTemplates
11+
git -C NetCoreToolTemplates checkout $TEMPLATE_CHECKOUT_TARGET
12+
dotnet new install NetCoreToolTemplates/src/Content
13+
fi

0 commit comments

Comments
 (0)