Skip to content

Allow including templates from source during PR builds #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.github/ @TimHess @bart-vmware
58 changes: 40 additions & 18 deletions .github/workflows/build-and-stage.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Build and stage

on:
push:
branches:
Expand All @@ -13,30 +14,36 @@ concurrency:

permissions:
contents: read
pull-requests: 'write'
pull-requests: write

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

jobs:
build-push-deploy:
build-push:
name: Build and push image
environment:
name: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
env:
SLOT_NAME: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.

- name: Login to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Detect template source from PR body
env:
PullRequestBody: ${{ github.event.pull_request.body }}
run: |
cat << EOF > /tmp/pull_request_body.txt
$PullRequestBody
EOF

CheckoutTarget=$(grep "template_checkout_target=" /tmp/pull_request_body.txt | awk -F= '{print $2}')
if [ "$CheckoutTarget" = "" ]; then
echo "Did not find a checkout target for templates."
else
echo "Found checkout target '$CheckoutTarget' in PR body, this build will use templates from source."
echo "TEMPLATE_CHECKOUT_TARGET=$CheckoutTarget" >> $GITHUB_ENV
fi

- name: Login to container registry
uses: azure/docker-login@v1
Expand All @@ -46,33 +53,48 @@ jobs:
password: "${{ secrets.DOCKER_PASSWORD }}"

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

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

deploy:
name: Deploy
if: ${{ github.secret_source == 'Actions' }}
environment:
name: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
env:
SLOT_NAME: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || vars.STAGING_SLOT_NAME }}
needs: build-push
runs-on: ubuntu-latest
steps:
- name: Login to Azure
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: If PR, create a new staging slot
if: ${{ github.event_name == 'pull_request' }}
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 }}

- name: Deploy to staging slot
uses: azure/webapps-deploy@v3
id: deploy-to-webapp
with:
uses: azure/webapps-deploy@v3
with:
app-name: ${{ vars.AZURE_WEBAPP_NAME }}
images: ${{ vars.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
slot-name: ${{ env.SLOT_NAME }}

- name: If PR, comment with the preview link
if: ${{ github.event_name == 'pull_request' }}
uses: mshick/add-pr-comment@v2
with:
message: |
## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net
## Preview link: https://${{ vars.AZURE_WEBAPP_NAME }}-${{ env.SLOT_NAME }}.azurewebsites.net

- Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch.
- The preview link is shareable, but will be deleted when the pull request is merged or closed.

> *This is an automated message.*
repo-token: ${{ secrets.GITHUB_TOKEN }}

11 changes: 6 additions & 5 deletions .github/workflows/pr-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Delete a preview environment

on:
pull_request:
types: [closed]
types:
- closed

env:
SLOT_NAME: pr-${{ github.event.number }}
Expand All @@ -13,10 +14,10 @@ jobs:

steps:
- name: Log into Azure CLI with service principal
uses: azure/login@v1
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Delete slot on staging site
run: az webapp deployment slot delete --resource-group ${{ vars.AZURE_RESOURCE_GROUP }} --name ${{ vars.AZURE_WEBAPP_NAME}} --slot ${{ env.SLOT_NAME }}

Expand All @@ -25,8 +26,8 @@ jobs:

steps:
- name: Delete Deployment Environment
uses: strumwolf/delete-deployment-environment@v2
uses: strumwolf/delete-deployment-environment@v3
with:
environment: "pr-${{ github.event.number }}"
token: ${{ secrets.GITHUB_TOKEN }}
onlyRemoveDeployments: true
onlyRemoveDeployments: true
5 changes: 2 additions & 3 deletions .github/workflows/stage-prod-swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ jobs:
name: Promote to production
runs-on: ubuntu-latest
environment:
name: 'Production'
name: Production
url: 'https://${{ vars.AZURE_WEBAPP_NAME }}.azurewebsites.net/'

steps:
- name: Log into Azure CLI with service principal
uses: azure/login@v1
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Swap slots
run: az webapp deployment slot swap -s ${{ vars.STAGING_SLOT_NAME }} -n ${{ vars.AZURE_WEBAPP_NAME }} -g ${{ vars.AZURE_RESOURCE_GROUP }}

18 changes: 8 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /source
COPY . .
RUN dotnet restore
RUN dotnet publish -c release -o /srv --no-restore
RUN dotnet restore src/NetCoreToolService
RUN dotnet build src/NetCoreToolService --configuration Release --no-restore
RUN dotnet publish src/NetCoreToolService --output /srv --no-build

FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine
ARG templates_version=1.3.0
#RUN dotnet nuget add source https://pkgs.dev.azure.com/dotnet/Steeltoe/_packaging/dev/nuget/v3/index.json -n SteeltoeDev
RUN dotnet new --install Steeltoe.NetCoreTool.Templates::${templates_version} &&\
dotnet new --list | grep steeltoe-webapi
# WORKDIR /usr/local/src
# RUN git clone https://github.com/SteeltoeOSS/NetCoreToolTemplates
# RUN git -C NetCoreToolTemplates checkout release/1.2
# RUN dotnet new --install NetCoreToolTemplates/src/Content
ARG TEMPLATE_CHECKOUT_TARGET
WORKDIR /srv
COPY --from=build /srv .
ENV DOTNET_URLS http://0.0.0.0:80
COPY install-template.sh /srv/install-template.sh
RUN chmod +x /srv/install-template.sh
RUN /srv/install-template.sh
ENV DOTNET_URLS=http://0.0.0.0:80
ENTRYPOINT ["dotnet", "Steeltoe.NetCoreToolService.dll"]
11 changes: 11 additions & 0 deletions Steeltoe.NetCoreToolService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.props = Directory.Build.props
docker-compose.yaml = docker-compose.yaml
Dockerfile = Dockerfile
install-template.sh = install-template.sh
stylecop.json = stylecop.json
Version.props = Version.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{4D3C2BC0-3714-42D2-BB89-057E57D92BAF}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build-and-stage.yml = .github\workflows\build-and-stage.yml
.github\workflows\pr-cleanup.yml = .github\workflows\pr-cleanup.yml
.github\workflows\stage-prod-swap.yml = .github\workflows\stage-prod-swap.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -64,6 +74,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{1462EDFE-F1FC-48C2-80C1-917317EE3C97} = {C742A7B8-80CA-4365-85CA-C29AA744CE54}
{6BD6C793-E555-475F-A2E1-12D7F3F56A3B} = {410C0E72-737F-4168-AECA-2F6D19EE86D5}
{4D3C2BC0-3714-42D2-BB89-057E57D92BAF} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D8EFB01A-92BF-418B-B2B2-A12045B772E2}
Expand Down
13 changes: 13 additions & 0 deletions install-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env sh

# dotnet nuget add source https://pkgs.dev.azure.com/dotnet/Steeltoe/_packaging/dev/nuget/v3/index.json -n SteeltoeDev

if [[ -z "$TEMPLATE_CHECKOUT_TARGET" ]] ;then
dotnet new install Steeltoe.NetCoreTool.Templates::${templates_version} &&\
dotnet new --list | grep steeltoe-webapi
else
cd /usr/local/src
git clone https://github.com/SteeltoeOSS/NetCoreToolTemplates
git -C NetCoreToolTemplates checkout $TEMPLATE_CHECKOUT_TARGET
dotnet new install NetCoreToolTemplates/src/Content
fi