diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..56b1d2a --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +/.github/ @TimHess @bart-vmware diff --git a/.github/workflows/build-and-stage.yml b/.github/workflows/build-and-stage.yml index 0b5fb36..ea327f3 100644 --- a/.github/workflows/build-and-stage.yml +++ b/.github/workflows/build-and-stage.yml @@ -1,4 +1,5 @@ name: Build and stage + on: push: branches: @@ -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 @@ -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 }} - \ No newline at end of file diff --git a/.github/workflows/pr-cleanup.yml b/.github/workflows/pr-cleanup.yml index b55924b..337a717 100644 --- a/.github/workflows/pr-cleanup.yml +++ b/.github/workflows/pr-cleanup.yml @@ -2,7 +2,8 @@ name: Delete a preview environment on: pull_request: - types: [closed] + types: + - closed env: SLOT_NAME: pr-${{ github.event.number }} @@ -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 }} @@ -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 \ No newline at end of file + onlyRemoveDeployments: true diff --git a/.github/workflows/stage-prod-swap.yml b/.github/workflows/stage-prod-swap.yml index d917fe4..dccaff0 100644 --- a/.github/workflows/stage-prod-swap.yml +++ b/.github/workflows/stage-prod-swap.yml @@ -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 }} - \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 14a8050..e0ac0ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 templates_version=1.4.0 +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"] diff --git a/Steeltoe.NetCoreToolService.sln b/Steeltoe.NetCoreToolService.sln index 3515b13..45cfe0a 100644 --- a/Steeltoe.NetCoreToolService.sln +++ b/Steeltoe.NetCoreToolService.sln @@ -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 @@ -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} diff --git a/install-template.sh b/install-template.sh new file mode 100644 index 0000000..d2dcfdb --- /dev/null +++ b/install-template.sh @@ -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