Deploy your Azure Functions as a package from GitHub Actions

May 3, 2021

For Squarl, I started to implement a couple of improvements for the back-end running on Azure Functions. I created these functions with TypeScript. In the past, I used webpack and later Azure Funcpack to bundle my functions to one file per function. That way, the host does not have to do all those read actions for retrieving the dependencies.

As I wanted to do the same thing, I read using Azure Funcpack is not supported/maintained anymore. Instead, you should use the run from package functionality.

What is the run from package option?

Instead of bundling, you install your dependencies, build your solution, and compress the directory as a ZIP file.

No significant changes, but now comes the cool part. The ZIP package gets used by the Azure Functions to run the function’s logic. Instead of pushing your files to the wwwroot directory, the app service loads the ZIP package as a virtual directory.

This option improves the load times, deployment times, and more.

If you want to use this functionality, you will have to set the WEBSITE_RUN_FROM_PACKAGE setting on your function app.

The value for this can refer to where the function app can find the ZIP package, or it can just be 1. This option is the recommended approach when running apps on Windows.

When using 1 as the value, the function app will run the data from a local package. The ZIP package needs to be deployed to d:\home\data\SitePackages as this improves the cold-start.

Info You can read more at: run your Azure Functions from a package file

How do you use it on GitHub Actions?

Important Make sure to first set the WEBSITE_RUN_FROM_PACKAGE to 1 in the function app configuration.

The steps which you need to perform to deploy your code to the functions app are the following:

  • Check out your code
  • Specify the Node.js version to use
  • Install your dependencies
  • Build your project
  • Create the ZIP
  • Perform the deployment with the webapps-deploy action

My GitHub Actions workflow looks as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
deploy_az:
  runs-on: windows-latest
  name: "Deploy back-end"
  if: ${{ !contains(github.event.head_commit.message, '#frontend') }}

  steps:
    - uses: actions/[email protected]
    - uses: actions/[email protected]
      with:
        node-version: '12'
        registry-url: https://registry.npmjs.org/

    - name: "Install and build"
      run: |
        npm ci
        npm run build
        npm prune --production

    # You can also make use of a ZIP GitHub Action
    - name: "Create ZIP"
      run: |
        Get-ChildItem $pwd | Compress-Archive -Destination .\backend.zip
      shell: pwsh

    - name: 'Deploy the ZIP to the app'
      uses: azure/[email protected]
      with:
        app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE_DEV }}
        package: ./backend.zip

Once this workflow ran. Your function app will now run from the package you deployed. You can verify this if you take a look in the wwwroot directory. This directory should now contain the files from the package. Be aware; this directory is now read-only.

The packages pushed to your Azure Function
The packages pushed to your Azure Function

Info The packagename.txt file contains the name of the ZIP package which needs to be used.

Comments

comments powered by Disqus