github-actionsazure-resource-managerazure-bicep

Skip App Service Provisioning if Web App already exists


I have a simple hello world style bicep code for app creation (and only now learning devops, so, very new to this). full code is available here.

    #Download the bicep templates from previous job
    - name: Download artifact from build job
      uses: actions/download-artifact@v4
      with:
        name: bicep-template
        path: bicep-template
        
   #Login in your azure subscription using a service principal (credentials stored as GitHub Secret in repo)
    - name: Azure Login
      uses: azure/login@v2
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}
           
    # Deploy Azure WebApp using Bicep file
    - name: deploy
      uses: azure/arm-deploy@v2
      with:
        subscriptionId: ${{ env.SUBSCRIPTION-ID }}
        resourceGroupName: ${{ env.RESOURCE-GROUP }}
        template: bicep-template/webapp.bicep
        parameters: 'webAppName=${{ env.WEBAPP-NAME }} location=${{ env.LOCATION }}'
        failOnStdErr: false   

is there way to put some kind of if else, so that, I could stop trying to provision the web app, and simply show a message that app already exists and move on to the deploy stage?

note: as of now, it does not seem to create any problems. when I re-run the jobs on github actions, it naturally skips the app provisioning. I just want to put a message that, app is already there.


Solution

  • @Jay

    I dont exactly see the need of the condition check you want to pass, as running the script on each pipeline run remains idempotent, ensuring results remain the same if there are no changes.

    enter image description here https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep

    However, if you need to add a condition to skip deployment, if a resource exists, you could use an Az cli command to verify its existence, assigning true or false based on this check, which you can then use as a condtion for the actual deploy job.

    - name: Check if App Service exists
      id: check_app
      run: |
        if az webapp list --resource-group ${{ env.RESOURCE-GROUP }} --query "[?name=='${{ env.WEBAPP-NAME }}']" | grep -q ${{ env.WEBAPP-NAME }}; then
          echo "exists=true" >> $GITHUB_ENV
          echo "WebApp Exists"
        else
          echo "exists=false" >> $GITHUB_ENV
          echo "WebApp doesnt Exists"
        fi
    
    - name: Deploy Azure WebApp using Bicep file
      if: env.exists == 'false'
      uses: azure/arm-deploy@v2
      with:
        subscriptionId: ${{ env.SUBSCRIPTION-ID }}
        resourceGroupName: ${{ env.RESOURCE-GROUP }}
        template: bicep-template/webapp.bicep
        parameters: 'webAppName=${{ env.WEBAPP-NAME }} location=${{ env.LOCATION }}'
        failOnStdErr: false
    
    

    enter image description here