Search code examples
javamavenazure-functionsdependency-management

How to delete /home/site/wwwroot/lib folder content before zip deployment?


I'm deploying a Java Azure Function using zip deployment, and the dependencies are managed through the pom.xml file. These dependencies are packaged as JAR files into the /lib folder within /home/site/wwwroot during deployment.

The issue is that when I update dependencies in the pom.xml (e.g., upgrading versions of JARs), the new JARs are added to the /lib folder, but the old JARs remain. This causes conflicts because the function ends up with multiple versions of the same dependency.

Currently, I have to manually delete the old JARs from the /home/site/wwwroot/lib folder using the Azure Kudu Console (rm -rf /home/site/wwwroot/lib) or similar methods. This is not ideal for automation or scalability.

Is there a way to automatically clean up the /lib folder to remove outdated JARs during zip deployment?

Github action workflow:

name: Deploy Azure Functions to Stage

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Login via Az module
        uses: azure/[email protected]
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Deploy Azure Functions in Stage
        uses: Azure/[email protected]
        id: fa
        with:
          app-name: ${{ env.AZURE_STAGE_FUNCTIONAPP_NAME }}
          package: publish-target/target/azure-functions/${{ env.POM_FUNCTIONAPP_NAME }}

Solution

  • Add a step in your GitHub workflow(.yml file) to restore the dependencies:

    name: 'Restore Project Dependencies Using Mvn'
            shell: pwsh
            run: |
              pushd './${{ env.PACKAGE_DIRECTORY }}'
              mvn clean package
              popd
    

    I have created a simple Java function and deployed it to staging slot using GitHub actions.

    Complete GitHub workflow:

    name: Build and deploy Java project to Azure Function App - function_app_name 
    
    on:
      push:
        branches:
          - main
      workflow_dispatch:
    
    env:
      AZURE_FUNCTIONAPP_NAME: function_app_name # set this to your function app name on Azure
      PACKAGE_DIRECTORY: '.' # set this to the directory which contains pom.xml file
      JAVA_VERSION: '17' # set this to the java version to use
    
    jobs:
      build-and-deploy:
        runs-on: windows-latest
        
        steps:
          - name: 'Checkout GitHub Action'
            uses: actions/checkout@v4
    
          - name: Setup Java Sdk ${{ env.JAVA_VERSION }}
            uses: actions/setup-java@v4
            with:
              java-version: ${{ env.JAVA_VERSION }}
              distribution: 'microsoft'
    
          - name: 'Restore Project Dependencies Using Mvn'
            shell: pwsh
            run: |
              pushd './${{ env.PACKAGE_DIRECTORY }}'
              mvn clean package
              popd
          
          - name: 'Run Azure Functions Action'
            uses: Azure/functions-action@v1
            id: fa
            with:
              app-name: 'javafn21'
              slot-name: 'staging'
              package: '${{ env.PACKAGE_DIRECTORY }}'
              respect-pom-xml: true
              publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_E2E70DE88E9D4XXX4C0792E53D2 }}
    

    Initially, I have added a sample dependency guava with version 30.1.jre in POM.XML.

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
    </dependencies>
    

    Deployed the function to Azure using GitHub actions.

    enter image description here

    Deployed files in KUDU under site/wwwroot/lib folder.

    enter image description here

    Updated the version of guava to 31.0-jre in POM.XML:

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0-jre</version> 
        </dependency>
    </dependencies>
    

    Deployed the updated function to Azure and the updated versions of the dependencies overridden the existing ones as highlighted below:

    enter image description here