0

The latest answer I could find that was on a path I was looking for is 5 years old and no longer applies.

Is there a way to publish a function app in c# to other environments via REST, Azure.ResourceManager or some other way that is "not" the standard ways in Azure or Visual Studio?

I need to be able to completely automate the deployment as well as publishing. So far all ways have been leading to a dead end.

If github is the answer, how can it be automated with zero interaction into other environments?

user2455808
  • 95
  • 1
  • 1
  • 9

1 Answers1

0
  • I have used below steps to deploy function app using ARM template to azure portal and Git-Hub to deploy HTTP triggers

  • Open Azure portal and Search for Custom Deployment.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
        "_generator": {
            "name": "bicep",
      "version": "0.5.6.12127",
      "templateHash": "10848576439099634716"
        }
    },
  "parameters": {
        "appName": {
            "type": "string",
      "defaultValue": "[format('fnapp{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
                "description": "function app that you want to create."
      }
        },
    "storageAccountType": {
            "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ],
      "metadata": {
                "description": "Storage Account type"
      }
        },
    "location": {
            "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
                "description": "Location for all resources."
      }
        },
    "appInsightsLocation": {
            "type": "string",
      "metadata": {
                "description": "Location for Application Insights"
      }
        },
    "runtime": {
            "type": "string",
      "defaultValue": "node",
      "allowedValues": [
        "node",
        "dotnet",
        "java"
      ],
      "metadata": {
                "description": "runtime to load in the function app."
      }
        }
    },
  "variables": {
        "functionAppName": "[parameters('appName')]",
    "hostingPlanName": "[parameters('appName')]",
    "applicationInsightsName": "[parameters('appName')]",
    "storageAccountName": "[format('{0}azfunctions', uniqueString(resourceGroup().id))]",
    "functionWorkerRuntime": "[parameters('runtime')]"
  },
  "resources": [
    {
        "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-08-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
            "name": "[parameters('storageAccountType')]"
      },
      "kind": "Storage"
    },
    {
        "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-03-01",
      "name": "[variables('hostingPlanName')]",
      "location": "[parameters('location')]",
      "sku": {
            "name": "Y1",
        "tier": "Dynamic"
      },
      "properties": { }
    },
    {
        "type": "Microsoft.Web/sites",
      "apiVersion": "2021-03-01",
      "name": "[variables('functionAppName')]",
      "location": "[parameters('location')]",
      "kind": "functionapp",
      "identity": {
            "type": "SystemAssigned"
      },
      "properties": {
            "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "siteConfig": {
                "appSettings": [
                  {
                    "name": "AzureWebJobsStorage",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2021-08-01').keys[0].value)]"
                  },
            {
                    "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2021-08-01').keys[0].value)]"
            },
            {
                    "name": "WEBSITE_CONTENTSHARE",
              "value": "[toLower(variables('functionAppName'))]"
            },
            {
                    "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "~4"
            },
            {
                    "name": "WEBSITE_NODE_DEFAULT_VERSION",
              "value": "~10"
            },
            {
                    "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('Microsoft.Insights/components', variables('applicationInsightsName'))).InstrumentationKey]"
            },
            {
                    "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[variables('functionWorkerRuntime')]"
            }
          ],
          "ftpsState": "FtpsOnly",
          "minTlsVersion": "1.2"
        },
        "httpsOnly": true
      },
      "dependsOn": [
        "[resourceId('Microsoft.Insights/components', variables('applicationInsightsName'))]",
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    },
    {
        "type": "Microsoft.Insights/components",
      "apiVersion": "2020-02-02",
      "name": "[variables('applicationInsightsName')]",
      "location": "[parameters('appInsightsLocation')]",
      "kind": "web",
      "properties": {
            "Application_Type": "web",
        "Request_Source": "rest"
      }
    }
  ]
}
  • After deploying to azure portal you will see as below

enter image description here

  • Go to Git-Hub -> create an Empty Repository
  • create a New folder in local system
  • Goto Visual Studio Code -> Files -> Open Folder -> Select folder name and click on Open and follow as below

enter image description here

  • Click on Browse and select the folder that you have created as below

enter image description here

  • Select C#

enter image description here

  • Select .NET 6.0

enter image description here

  • Select HTTP trigger

enter image description here

  • Enter the HTTP Trigger name click enter

enter image description here

  • Enter the name you want and click on enter

enter image description here

  • Select Anonymous

enter image description here

  • Push the code to the Git-Hub Repository that you have created.

enter image description here

  • After pushing the project to repository Goto Azure portal -> Function App that you want to add HTTP trigger -> Select Deployment center -> connect your Git-Hub account and details of the repository that you want to add as below

enter image description here

  • After Connecting the Repository, the azure will create a workflow folder in git repository as below

enter image description here

  • Now Goto Actions and check deployment is successful as below

enter image description here

enter image description here

enter image description here

  • After successfully deploy Goto Azure portal -> Function app -> Functions as below

enter image description here

  • Below is the yml file I have used to deploy Http Trigger to Azure function in azure portal

  • Please check the DOTNET_VERSION format in you yml file below

Update

# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy dotnet core app to Azure Function App - function app name

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '6.0.x' # set this to the dotnet version to use

jobs:
  build-and-deploy:
    runs-on: windows-latest
    steps:
      - name: 'Checkout GitHub Action'
        uses: actions/checkout@v2

      - name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: 'Resolve Project Dependencies Using Dotnet'
        shell: pwsh
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          dotnet build --configuration Release --output ./output
          popd

      - name: 'Run Azure Functions Action'
        uses: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'Function app name'
          slot-name: 'Production'
          package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
Tarun Krishna
  • 366
  • 2
  • 6
  • The github credentials would have to be imported programmatically to deployment. Zero user input in Azure. – user2455808 Dec 09 '22 at 14:27
  • When you added the git action to azure functions automatically a trigger will be send to that repository and will automatically deploy in to azure functions – Tarun Krishna Dec 09 '22 at 14:44
  • I have added below picture to show where we can see the trigger deploying updates are shown in git hub – Tarun Krishna Dec 09 '22 at 14:49
  • so how would a new function app from another environment with a different name use the same .yml file with 0 human interaction? – user2455808 Dec 09 '22 at 17:06
  • We have to give some information to get the yml file from github we have to fallow some steps .please Goto deployment center on your azure function and give the GitHub account name ,repository and node name and click on save – Tarun Krishna Dec 09 '22 at 23:11
  • this won't work, can't have any interaction. – user2455808 Dec 12 '22 at 15:12
  • In the Azure portal Function app menu -> Deployments ->Here we have to connect GitHub with our login credentials and click save – Tarun Krishna Dec 14 '22 at 12:02