|
| 1 | +--- |
| 2 | +title: Use GitHub Actions to make code updates in Azure Functions |
| 3 | +description: Learn how to use GitHub Actions to define a workflow to build and deploy Azure Functions projects in GitHub. |
| 4 | +author: ahmedelnably |
| 5 | +manager: gwallace |
| 6 | +ms.service: azure-functions |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 09/16/2019 |
| 9 | +ms.author: aelnably |
| 10 | +--- |
| 11 | + |
| 12 | +# Continuous delivery by using GitHub Action |
| 13 | + |
| 14 | +[GitHub Actions](https://github.com/features/actions) lets you define a workflow to automatically build and deploy your functions code to function app in Azure. |
| 15 | + |
| 16 | +> [!IMPORTANT] |
| 17 | +> GitHub Actions is currently in beta. You must first [sign-up to join the preview](https://github.com/features/actions) using your GitHub account. |
| 18 | +
|
| 19 | +In GitHub Actions, a [workflow](https://help.github.com/articles/about-github-actions#workflow) is an automated process that you define in your GitHub repository. This process tells GitHub how to build and deploy your functions app project on GitHub. |
| 20 | + |
| 21 | +A workflow is defined by a YAML (.yml) file in the `/.github/workflows/` path in your repository. This definition contains the various steps and parameters that make up the workflow. |
| 22 | + |
| 23 | +For an Azure Functions workflow, the file has three sections: |
| 24 | + |
| 25 | +| Section | Tasks | |
| 26 | +| ------- | ----- | |
| 27 | +| **Authentication** | <ol><li>Define a service principal.</li><li>Create a GitHub secret.</li></ol>| |
| 28 | +| **Build** | <ol><li>Set up the environment.</li><li>Build the function app.</li></ol> | |
| 29 | +| **Deploy** | <ol><li>Deploy the function app.</li></ol>| |
| 30 | + |
| 31 | +## Create a service principal |
| 32 | + |
| 33 | +You can create a [service principal](../active-directory/develop/app-objects-and-service-principals.md#service-principal-object) by using the [az ad sp create-for-rbac](/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) command in the [Azure CLI](/cli/azure/). You can run this command using [Azure Cloud Shell](https://shell.azure.com) in the Azure portal or by selecting the **Try it** button. |
| 34 | + |
| 35 | +```azurecli-interactive |
| 36 | +az ad sp create-for-rbac --name "myApp" --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>/providers/Microsoft.Web/sites/<APP_NAME> --sdk-auth |
| 37 | +``` |
| 38 | + |
| 39 | +In this example, replace the placeholders in the resource with your subscription ID, resource group, and function app name. The output is the role assignment credentials that provides access to your function app. Copy this JSON object, which you can use to authenticate from GitHub. |
| 40 | + |
| 41 | +> [!IMPORTANT] |
| 42 | +> It is always a good practice to grant minimum access. This is why the scope in the previous example is limited to the specific function app and not the entire resource group. |
| 43 | +
|
| 44 | +## Configure the GitHub secret |
| 45 | + |
| 46 | +1. In [GitHub](https://github/com), browse your repository, select **Settings** > **Secrets** > **Add a new secret**. |
| 47 | + |
| 48 | +  |
| 49 | + |
| 50 | +1. Use `AZURE_CREDENTIALS` for the **Name** and the copied command output for **Value**, then select **Add secret**. |
| 51 | + |
| 52 | +GitHub can now authenticate to your function app in Azure. |
| 53 | + |
| 54 | +## Set up the environment |
| 55 | + |
| 56 | +Setting up the environment can be done using one of the publish setup actions. |
| 57 | + |
| 58 | +|Language | Setup Action | |
| 59 | +|---------|---------| |
| 60 | +|**.NET** | `actions/setup-dotnet` | |
| 61 | +|**Java** | `actions/setup-java` | |
| 62 | +|**JavaScript** | `actions/setup-node` | |
| 63 | +|**Python** | `actions/setup-python` | |
| 64 | + |
| 65 | +The following examples show the part of the workflow that sets up the environment for the various supported languages: |
| 66 | + |
| 67 | +**JavaScript** |
| 68 | + |
| 69 | +```yaml |
| 70 | + - name: 'Login via Azure CLI' |
| 71 | + uses: Azure/actions/login@master |
| 72 | + with: |
| 73 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 74 | + - name: Setup Node 10.x |
| 75 | + uses: actions/setup-node@v1 |
| 76 | + with: |
| 77 | + node-version: '10.x' |
| 78 | +``` |
| 79 | +
|
| 80 | +**Python** |
| 81 | +
|
| 82 | +```yaml |
| 83 | + - name: 'Login via Azure CLI' |
| 84 | + uses: Azure/actions/login@master |
| 85 | + with: |
| 86 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 87 | + - name: Setup Python 3.6 |
| 88 | + uses: actions/setup-python@v1 |
| 89 | + with: |
| 90 | + python-version: 3.6 |
| 91 | +``` |
| 92 | +
|
| 93 | +**.NET** |
| 94 | +
|
| 95 | +```yaml |
| 96 | + - name: 'Login via Azure CLI' |
| 97 | + uses: Azure/actions/login@master |
| 98 | + with: |
| 99 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 100 | + - name: Setup Dotnet 2.2.300 |
| 101 | + uses: actions/setup-dotnet@v1 |
| 102 | + with: |
| 103 | + dotnet-version: '2.2.300' |
| 104 | +``` |
| 105 | +
|
| 106 | +**Java** |
| 107 | +
|
| 108 | +```yaml |
| 109 | + - name: 'Login via Azure CLI' |
| 110 | + uses: Azure/actions/login@master |
| 111 | + with: |
| 112 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 113 | + - name: Setup Java 1.8.x |
| 114 | + uses: actions/setup-java@v1 |
| 115 | + with: |
| 116 | + # If your pom.xml <maven.compiler.source> version is not in 1.8.x |
| 117 | + # Please change the Java version to match the version in pom.xml <maven.compiler.source> |
| 118 | + java-version: '1.8.x' |
| 119 | +``` |
| 120 | +
|
| 121 | +## Build the function app |
| 122 | +
|
| 123 | +This depends on the language and for languages supported by Azure Functions, this section should be the standard build steps of each language. |
| 124 | +
|
| 125 | +The following examples show the part of the workflow that builds the function app, in the various supported languages.: |
| 126 | +
|
| 127 | +**JavaScript** |
| 128 | +
|
| 129 | +```yaml |
| 130 | + - name: 'Run npm' |
| 131 | + shell: bash |
| 132 | + run: | |
| 133 | + # If your function app project is not located in your repository's root |
| 134 | + # Please change your directory for npm in pushd |
| 135 | + pushd . |
| 136 | + npm install |
| 137 | + npm run build --if-present |
| 138 | + npm run test --if-present |
| 139 | + popd |
| 140 | +``` |
| 141 | +
|
| 142 | +**Python** |
| 143 | +
|
| 144 | +```yaml |
| 145 | + - name: 'Run pip' |
| 146 | + shell: bash |
| 147 | + run: | |
| 148 | + # If your function app project is not located in your repository's root |
| 149 | + # Please change your directory for pip in pushd |
| 150 | + pushd . |
| 151 | + python -m pip install --upgrade pip |
| 152 | + pip install -r requirements.txt --target=".python_packages/lib/python3.6/site-packages" |
| 153 | + popd |
| 154 | +``` |
| 155 | +
|
| 156 | +**.NET** |
| 157 | +
|
| 158 | +```yaml |
| 159 | + - name: 'Run dotnet build' |
| 160 | + shell: bash |
| 161 | + run: | |
| 162 | + # If your function app project is not located in your repository's root |
| 163 | + # Please consider using pushd to change your path |
| 164 | + pushd . |
| 165 | + dotnet build --configuration Release --output ./output |
| 166 | + popd |
| 167 | +``` |
| 168 | +
|
| 169 | +**Java** |
| 170 | +
|
| 171 | +```yaml |
| 172 | + - name: 'Run mvn' |
| 173 | + shell: bash |
| 174 | + run: | |
| 175 | + # If your function app project is not located in your repository's root |
| 176 | + # Please change your directory for maven build in pushd |
| 177 | + pushd . ./POM_ARTIFACT_ID |
| 178 | + mvn clean package |
| 179 | + mvn azure-functions:package |
| 180 | + popd |
| 181 | +``` |
| 182 | +
|
| 183 | +## Deploy the function app |
| 184 | +
|
| 185 | +To deploy your code to a function app, you will need to use the `Azure/functions-action` action. This action has two parameters: |
| 186 | + |
| 187 | +|Parameter |Explanation | |
| 188 | +|---------|---------| |
| 189 | +|**_app-name_** | (Mandatory) The name of your function app. | |
| 190 | +|_**slot-name**_ | (Optional) The name of the [deployment slot](functions-deployment-slots.md) you want to deploy to. The slot must already be defined in your function app. | |
| 191 | + |
| 192 | + |
| 193 | +The following example uses version 1 of the `functions-action`: |
| 194 | + |
| 195 | +```yaml |
| 196 | + - name: 'Run Azure Functions Action' |
| 197 | + uses: Azure/functions-action@v1 |
| 198 | + id: fa |
| 199 | + with: |
| 200 | + app-name: PLEASE_REPLACE_THIS_WITH_YOUR_FUNCTION_APP_NAME |
| 201 | +``` |
| 202 | + |
| 203 | +## Next steps |
| 204 | + |
| 205 | +To view a complete workflow .yaml, see one of the files in the [Azure GitHub Actions workflow samples repo](https://github.com/Azure/actions-workflow-samples) that have `functionapp` in the name. You can use these samples a starting point for your workflow. |
| 206 | + |
| 207 | +> [!div class="nextstepaction"] |
| 208 | +> [Learn more about GitHub Actions](https://help.github.com/en/articles/about-github-actions) |
0 commit comments