Implement CI/CD with GitHub – Deploy Azure Functions steps

Implement CI/CD with GitHub – Deploy Azure Functions – DEV Community

1 Create an Azure Function App
rg, storage account, function app, not deployment is set up yet.
To set up deployment, webapipython | Deployment Center, we do that later

2 Create a GitHub repository
test-webapipython

git@github.com:spawnmarvel/test-webapipython.git

3 Clone GitHub Function repository

/c/giti2023
$ git clone git@github.com:spawnmarvel/test-webapipython.git

4 Link Azure Function App with GitHub Repository

init project, name is same as function app name in azure-automation
PS C:\giti2023\test-webapipython> func init webapipython

cd .\webapipython\
Create a function
C:\giti2023\test-webapipython\webapipython> func new –name testpy1 –template “HTTP trigger” –authlevel “anonymous”

spawnmarvel/test-webapipython: Test simple web api in python with az ci/cd and git actions (github.com)

Test the function
func start

http://localhost:7071/api/testpy1

Save and commit the changes
https://github.com/spawnmarvel/test-webapipython

5 Deploy Function App
This brings us to the last step, automating the deployment of our Functions with CI/CD using GitHub Actions

webapipython | Deployment Center

Choose workflow

Add the repos (I think I added organization previous)

When you save on the configuration above, you will notice that on the GitHub repository there is a new automation workflow that is automatically created as well as a new repository secret.

The workflow will be located in a special folder called .github/workflows that is automatically created by Azure:

main_webapipython.yml

Note that the Publish Profile is actually stored as a GitHub Action Secret, this was also automatically created by Azure as part of the workflow YAML file:

Actions secrets (github.com)

Let’s trigger this workflow manually and deploy our function into the Azure Function App. In GitHub navigate to Actions, select the workflow and then Run Workflow:

Run it

Working

Hm, error

Node.js 12 actions are deprecated. Please update the following actions to use Node.js 16: actions/checkout@v2, actions/setup-python@v1. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.

GitHub Actions: All Actions will begin running on Node16 instead of Node12 | GitHub Changelog

$ git pull origin main

# edit from 2 to 3
 - name: Checkout repository
        uses: actions/checkout@v3

Git add, commit and push.

Run actions again

Error

ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirements.txt’

Hm, moved requirements.txt from

C:\giti2023\test-webapipython\webapipython

to

C:\giti2023\test-webapipython\

It started deploying at once

Done

I have called the function URL, but it takes time…to build…

Last step I did was to edit the yaml file, did not read enough from the previous post

on:
  push:
    branches:
      - main
    paths: # elo. Added this
      - 'webapipython/**' # elo. Added this
  workflow_dispatch:

env:
  # elo AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  AZURE_FUNCTIONAPP_PACKAGE_PATH: 'webapipython' # elo set this to the path to your web app project, defaults to the repository root

And now it works

Git actions

App in portal

App on URL

-https://webapipython.azurewebsites.net/api/testpy1

-https://webapipython.azurewebsites.net/api/testpy1?name=john

Lets make a small change to the code.

return func.HttpResponse(
             "Github Actions: This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",

Push to git and deploy automatic from actions

Visit URL

When updating yaml file, the Github actions does not trigger, put a comment in a file with code and it will trigger, all changes.

For now we will lock the actions

Security

quickguides/security at main · spawnmarvel/quickguides · GitHub

Update 03.11.2023

Set allow actions for test

And we have the new tab with actions

Do some code changes

git add., git commit -m “03.11.2023 wf”

git push origin main

and it is done

Now visit URL -https://webapipython.azurewebsites.net/api/testpy1?name=john

set back