skip to Main Content

I am using the following GH Action to Build/Publish my .NET API.

#https://stackoverflow.com/questions/68327652/error-with-github-action-deploy-to-azure-web-app
name: API-zip

on: 
  push:
    # only trigger on branches, not on tags
    branches: '**'

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master

    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 8.0.x
        dotnet-quality: 'preview'

    - name: dotnet publish
      run: dotnet publish 'App.ApiApp.Api.csproj' -r linux-x64 -f net8.0 -c Release -o ${{env.DOTNET_ROOT}}/app --self-contained --nologo

    - name: Upload Build Artifact
      uses: actions/upload-artifact@v2
      with:
        name: app-${{ github.run_number }}
        path: ${{env.DOTNET_ROOT}}/app

  publish:
    runs-on: ubuntu-latest
    needs: build
    
    steps:
    - name: Downlod Artifacts 
      uses: actions/download-artifact@v3

    - name: List Files
      run: ls -R

    - name: Azure Login
      uses: azure/CLI@v1
      with:
        azcliversion: 2.52.0
        inlineScript: |
          az login --service-principal 
          -u ** 
          -p ** 
          -t **

    - name: Deploy to Azure
      uses: azure/CLI@v1
      with:
        azcliversion: 2.52.0
        inlineScript: |
          zip -r app-${{ github.run_number }}.zip .
          ls -R
          az webapp deployment source config-zip --resource-group App --name App-zip --src app-${{ github.run_number }}.zip

It successfully completes and publishes a ZIP file to my Linux App Service.

enter image description here

However, when I attempt to view the App, it’s still showing me the wwwroot default HTML page. I do have WEBSITE_RUN_FROM_PACKAGE=1 in my App Settings. I noticed when trouble shooting I extracted the ZIP file locally that it’s not zipping the raw files, but putting them in a folder first (see image)

enter image description here

Is this part of the problem? I tried just zipping the files themselves and re-uploading manually (to eliminate the parent folder) but it still didn’t pickup the App.

What is wrong here?

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out using this updated GH Action:

    #https://stackoverflow.com/questions/68327652/error-with-github-action-deploy-to-azure-web-app
    name: api
    
    on: [workflow_dispatch]
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@master
    
        - name: Setup .NET
          uses: actions/setup-dotnet@v3
          with:
            dotnet-version: 8.0.x
            dotnet-quality: 'preview'
    
        - name: dotnet publish
          run: dotnet publish 'appapp.csproj' -r linux-x64 -f net8.0 -c Release -o ${{env.DOTNET_ROOT}}/app --self-contained --nologo
    
        - name: Upload Build Artifact
          uses: actions/upload-artifact@v3
          with:
            name: app-${{ github.run_number }}
            path: ${{env.DOTNET_ROOT}}/app
            retention-days: 1
    
      publish:
        runs-on: ubuntu-latest
        needs: build
        
        steps:
        - name: Downlod Artifacts 
          uses: actions/download-artifact@v3
    
        - name: Azure Login
          uses: azure/CLI@v1
          with:
            azcliversion: 2.52.0
            inlineScript: |
              az login --service-principal 
              -u z 
              -p y 
              -t x
    
        - name: Deploy to Azure
          uses: azure/CLI@v1
          with:
            azcliversion: 2.52.0
            inlineScript: |
              zip -r -j app-${{ github.run_number }}.zip ./app-${{ github.run_number }}
              ls -a
              az webapp deployment source config-zip --resource-group app --name app --src app-${{ github.run_number }}.zip --timeout 360
    

  2. As .net 8.0 is still in Preview not all the Features are available for Deploying your Asp.net core API. Thus while deploying the Asp.net core API or any API in Azure Web app its necessary to use Deployment mode as self contained while deploying your Web API to Web app like below:-

    Reference – SO Thread answer By Pravallika and comment by YMC

    I created one Asp.net core 8.0 Web API and while deployment selected Self contained as Deployment Type like below:-

    enter image description here

    And After clicking on Publish the Web API was deployed successfully like below:-

    enter image description here

    I have also connected one APIM instance for managing the API while deployment like below, you can skip this step:-

    enter image description here

    UPDATED Solution:-

    By default az webapp deployment source config-zip does not contain a parameter to add –self-contained as Deployment mode, Thus you need to add your --self-contained zip artifact in your az webapp deployment command in your github workflow like below:-

    My updated github workflow steps with the cli zip method:-

    • Make sure the artifact path you are using is correct, Cross-check it in your Download Artifact step in the deploy stage.
    • My downloaded artifact path where I added .zip suffix :-
      /home/runner/work/WebApplication16/WebApplication16.zip

    Also, I have added the Azure CLI service principal credentials as a github secret, Refer below:-

    Created service principal with contributor role at subscription level and added the output as a github secret:-

    az ad sp create-for-rbac --name "sp-name" --role contributor --scopes /subscriptions/xxxxxxxsub-id --sdk-auth
    

    Output:-

    enter image description here

    Copied this output in the secrets:-

    enter image description here

    My github action workflow script with zip command:-

    #https://stackoverflow.com/questions/68327652/error-with-github-action-deploy-to-azure-web-app
    name: API-zip
    
    on: 
      push:
        # only trigger on branches, not on tags
        branches: '**'
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@master
    
        - name: Setup .NET
          uses: actions/setup-dotnet@v3
          with:
            dotnet-version: 8.0.x
            dotnet-quality: 'preview'
            
        - name: Build with dotnet
          run: dotnet build --configuration Release 
    
        - name: dotnet publish
          run: dotnet publish -c Release --self-contained -r linux-x64 -o ${{ github.workspace }}/.net-app
            # Adjust the runtime identifier (-r) to match your target platform.
    
        - name: Upload artifact for deployment job
          uses: actions/upload-artifact@v2
          with:
              name: .net-app
              path: ${{ github.workspace }}/.net-app
    
      publish:
        runs-on: ubuntu-latest
        needs: build
        
        steps:
        - name: Download artifact from build job
          uses: actions/download-artifact@v2
          with:
              name: .net-app
    
        - name: Configure Azure credentials
          uses: azure/login@v1
          with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}
    
        - name: Deploy to Azure
          uses: azure/CLI@v1
          with:
            azcliversion: 2.52.0
            inlineScript: |
              zip -r /home/runner/work/WebApplication16/WebApplication16.zip .
              ls -R
              az webapp deployment source config-zip --resource-group valleyrg54 --name siliconwebapp098 --src /home/runner/work/WebApplication16/WebApplication16.zip
    
    

    Output:-

    enter image description here

    enter image description here

    API got deployed in Azure web app and is running successfully with /WeatherForecast:-

    https://.azurewebsites.net/WeatherForecast

    enter image description here

    An alternative method is to use azure/webapps-deploy@v2 task like below:-

    My github action workflow:-

    I have created a default workflow by connecting my github repository with the Azure Web app via Deployment Center and then edited the workflow code like below:-

    enter image description here

    My repository with .net 8.0 core API:-

    enter image description here

    Edited the workflow file with --self-contained and -r linux-x64 flag like below:-

    Workflow code:-

    name: Build and deploy ASP.NET Core app to Azure Web App - valleywebapp21
    
    on:
      push:
        branches:
          - main
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v2
    
          - name: Set up .NET Core
            uses: actions/setup-dotnet@v1
            with:
              dotnet-version: '8.x'
              include-prerelease: true
    
          - name: Build with dotnet
            run: dotnet build --configuration Release
    
          - name: dotnet publish
            run: dotnet publish -c Release --self-contained -r linux-x64 -o ${{ github.workspace }}/.net-app
            # Adjust the runtime identifier (-r) to match your target platform.
    
          - name: Upload artifact for deployment job
            uses: actions/upload-artifact@v2
            with:
              name: .net-app
              path: ${{ github.workspace }}/.net-app
    
      deploy:
        runs-on: ubuntu-latest
        needs: build
        environment:
          name: 'Production'
          url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    
        steps:
          - name: Download artifact from build job
            uses: actions/download-artifact@v2
            with:
              name: .net-app
    
          - name: Deploy to Azure Web App
            id: deploy-to-webapp
            uses: azure/webapps-deploy@v2
            with:
              app-name: 'valleywebapp21'
              slot-name: 'Production'
              publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_3AFD052BFF7A47D398D058E0DB22F706 }}
              package: /home/runner/work/WebApplication16/WebApplication16
    

    Cross check where your artifacts are downloaded in your build job in deploy stage and add that path in the package at Deploy to Azure Web App step as my script above:-

    enter image description here

    Web API deployed successfully:-

    enter image description here

    I am able to access WeatherForecast API like below after deployment:-

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search