Implement CI/CD with GitHub – Deploy Azure Functions



Overview

In todays tutorial we are going to check out implementing CI/CD with GitHub by utilizing GitHub Actions to automate Azure Operate deployment.

Bringing and sustaining our Azure Capabilities right into a Git repository additionally brings all the advantages of model management, supply code administration and automatic construct and deployment of our features into Azure although CI/CD.



Pre-requisites

To get began you may want a number of issues, firstly:

  • An Azure Subscription
  • A GitHub Account and Git repository

Additionally, you will want to put in a number of native pre-requirements on the machine you’ll be engaged on. In my case I’ll put together my machine for creating C# Capabilities, however you possibly can check out among the different code stacks right here: Run Local Requirements

  • Azure Capabilities Core Instruments
  • VSCode
  • Azure Capabilities for Visible Studio Code
  • C# for Visible Studio Code



Create an Azure Operate App

Lets begin by making a useful resource group and a home windows dotnet operate app in our Azure subscription. For this step I’ve written a PowerShell script utilizing Azure CLI. It’s also possible to discover ths script on my GitHub repository.

#Log into Azure
az login

# Setup Variables.
$randomInt = Get-Random -Most 9999
$subscriptionId = $(az account present --query id -o tsv)
$resourceGroupName = "GitHub-Managed-Operate-Demo"
$storageName = "demofuncsa$randomInt"
$functionAppName = "demofunc$randomInt"
$area = "uksouth"

# Create a useful resource resourceGroupName
az group create --name "$resourceGroupName" --location "$area"

# Create an azure storage account for operate app
az storage account create `
    --name "$storageName" `
    --location "$area" `
    --resource-group "$resourceGroupName" `
    --sku "Standard_LRS" `
    --kind "StorageV2" `
    --https-only true `
    --min-tls-version "TLS1_2"

# Create a Operate App
az functionapp create `
    --name "$functionAppName" `
    --storage-account "$storageName" `
    --consumption-plan-location "$area" `
    --resource-group "$resourceGroupName" `
    --os-type "Home windows" `
    --runtime "dotnet" `
    --runtime-version "6" `
    --functions-version "4" `
    --assign-identity
Enter fullscreen mode

Exit fullscreen mode

The above script created a useful resource group containing the operate app, operate app storage and insights in addition to the consumption app service plan.

NOTE: We now have solely created our Operate App at this stage, we don’t have any Capabilities but. We’ll create our first operate afterward on this tutorial with GitHub.

image.png



Create a GitHub repository

Subsequent, head over to your GitHub account and create a brand new repository. We’ll use this repository to hyperlink our operate app/s supply code later.

I’ve known as my repository Demo-Azure-Capabilities

image.png



Put together Native Necessities

As talked about at the start of this put up we are going to now set up and run a number of pre-requirements on the machine we will likely be working and creating our operate code on.

Set up the next instruments:



Clone GitHub Operate repository

With all our instruments now put in we will now clone our GitHub operate repository to our native machine:

  1. Copy the clone URL

    image.png

  2. Open VSCode and navigate to the Command Palette image.png

  3. Within the command palette sort clone and click on on Git:clone image.png

  4. Paste within the copied clone URL and choose a folder you need to clone the repository to. (Notice: The repo will likely be cloned to a sub folder within the folder you chose, the title of this sub folder will match the repo title and can comprise all of your repo recordsdata.) image.png



Hyperlink Azure Operate App with GitHub Repository

Subsequent we are going to create an empty folder inside our regionally cloned repository. This folder will characterize our Operate App:

image.png

NOTE: I’ve known as my folder in my repo the identical title because the title I’ve given to my Azure Operate App we created earlier; demofunc6144.

Now we are going to create our first operate inside the folder utilizing the Azure Capabilities extension for Visible Studio Code we put in earlier.

In VSCode you will note the extension put in on the left facet of the display screen. Click on on the extension and choose Create New Mission:

image.png

This can then open the Command Palette once more, browse to and choose the empty folder we created representing our Operate App:

image.png

The Command Palette will now current you with some choices, choose the next:

  1. Choose a language: C# image.png

  2. Choose a .NET runtime: .NET 6 image.png

  3. Choose a template to your undertaking’s first operate: HTTP set off image.png

  4. Give the operate a reputation: MyFirstDotnetFunction image.png

  5. Present a namespace: My.Operate image.png

  6. Choose acceptable entry rights: Nameless/Operate/Admin image.png

As soon as the above course of has accomplished, discover that now now we have a C# operate template in our folder demofunc6144 we will immediately begin engaged on. As a result of that is additionally in our native git repository we will be certain that our code is all the time managed by way of supply management.

image.png

Save and commit the the modifications, then push the brand new operate to the distant GitHub repository:

image.png

image.png



Deploy Operate App

Now now we have a completely built-in workspace we will use to create and develop Capabilities. However now we have not arrange any CI/CD but.

This brings us to the final step, automating the deployment of our Capabilities with CI/CD utilizing GitHub Actions

  1. Navigate again to the Operate App hosted in Azure that we created earlier and go to Deployment Middle: image.png

  2. Choose Supply GitHub and set the Org, Repo and Department we created and hit Save. (NOTE: You can be requested to hyperlink your GitHub account if you’re performing this step for the very first time): image.png

NOTE: It’s also possible to handle the Publish Profile from the above step.

If you save the configuration above, you’ll discover that on the GitHub repository there’s a new automation workflow that’s robotically arrange in addition to a brand new repository secret.

The workflow will likely be in a particular folder known as .github/workflows that’s robotically created by Azure:

image.png

In my case the workflow is known as master_decomfunc6144.yml:

image.png

Let’s take a better have a look at this workflow:

title: Construct and deploy dotnet core app to Azure Operate App - demofunc6144

on:
  push:
    branches:
      - grasp
    paths:
      - 'demofunc6144/**'
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: 'demofunc6144' # set this to the trail to your internet app undertaking, defaults to the repository root
  DOTNET_VERSION: '6.0.x' # set this to the dotnet model to make use of

jobs:
  build-and-deploy:
    runs-on: windows-latest
    steps:
      - title: 'Checkout GitHub Motion'
        makes use of: actions/checkout@v2

      - title: Setup DotNet ${{ env.DOTNET_VERSION }} Atmosphere
        makes use of: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - title: 'Resolve Mission Dependencies Utilizing Dotnet'
        shell: pwsh
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          dotnet construct --configuration Launch --output ./output
          popd

      - title: 'Run Azure Capabilities Motion'
        makes use of: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'demofunc6144'
          slot-name: 'Manufacturing'
          bundle: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
          publish-profile: ${{ secrets and techniques.AZUREAPPSERVICE_PUBLISHPROFILE_AD8BBBC377A040C480EB918BC04CE61C }}
Enter fullscreen mode

Exit fullscreen mode

NOTE: I’ve added the workflow set off to solely set off on my grasp department and for any modifications made underneath the folder/repo path demofunc6144. The workflow_dispatch: set off permits us to moreover set off and run the automation workflow manually.

#Set off
on:
  push:
    branches:
      - grasp
    paths:
      - 'demofunc6144/**'
  workflow_dispatch:
Enter fullscreen mode

Exit fullscreen mode

Additionally notice that I’ve modified the setting variables for the operate app bundle path to demofunc6144

#Atmosphere variables
env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: 'demofunc6144' # set this to the trail to your internet app undertaking, defaults to the repository root
  DOTNET_VERSION: '6.0.x' # set this to the dotnet model to make use of
Enter fullscreen mode

Exit fullscreen mode

Let’s check out what this automation workflow will do when it’s triggered:

jobs:
  build-and-deploy:
    runs-on: windows-latest
    steps:
      - title: 'Checkout GitHub Motion'
        makes use of: actions/checkout@v2

      - title: Setup DotNet ${{ env.DOTNET_VERSION }} Atmosphere
        makes use of: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - title: 'Resolve Mission Dependencies Utilizing Dotnet'
        shell: pwsh
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          dotnet construct --configuration Launch --output ./output
          popd

      - title: 'Run Azure Capabilities Motion'
        makes use of: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'demofunc6144'
          slot-name: 'Manufacturing'
          bundle: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
          publish-profile: ${{ secrets and techniques.AZUREAPPSERVICE_PUBLISHPROFILE_AD8BBBC377A040C480EB918BC04CE61C }}
Enter fullscreen mode

Exit fullscreen mode

The above job mainly has 4 steps:

  1. The code is checked out onto the GitHub runner.
  2. The model of .NET we specified within the setting variables will likely be put in on the GitHub runner.
  3. The Azure Operate is constructed and packaged
  4. The Operate is deployed to the Azure Operate App, demofunc6144 utilizing the publish-profile of the Operate App:
publish-profile: ${{ secrets and techniques.AZUREAPPSERVICE_PUBLISHPROFILE_AD8BBBC377A040C480EB918BC04CE61C }}
Enter fullscreen mode

Exit fullscreen mode

Notice that the Publish Profile is definitely saved as a GitHub Motion Secret, this was additionally robotically created by Azure as a part of the workflow YAML file:

image.png

NOTE: This Actions Secret is mainly the contents of the Operate Apps Publish Profile File which may be downloaded and re-added if ever wanted manually:

image.png

Let’s set off this workflow manually and deploy our operate into the Azure Operate App. In GitHub navigate to Actions, choose the workflow after which Run Workflow:

image.png

After the workflow as ran, we will now see our Operate within the Operate App on Azure.

image.png



Conclusion

That is all there may be to it, now now we have efficiently built-in our operate app improvement lifecycle utilizing supply management with Git and GitHub and have the flexibility to robotically deploy our Capabilities utilizing CI/CD workflows with GitHub Actions.

We are able to merely create extra folders for any new Operate Apps together with a corresponding YAML workflow linked to deploy features created for the related Operate Apps in Azure.

I hope you could have loved this put up and have discovered one thing new. It’s also possible to discover the code samples used on this weblog put up on my revealed Github Action web page. ❤️



Writer

Like, share, observe me on: 🐙 GitHub | 🐧 Twitter | 👾 LinkedIn

pwd9000 image



Add a Comment

Your email address will not be published. Required fields are marked *