azureazure-devopsazure-functionsazure-pipelinesazure-powershell

Azure Function App - Azure DevOps pipeline deployment


I am testing Azure Powershell Function Apps. I used Azure DEvOps pipeline to upload Function files into Azure Function App, pipeline completed successfully, I can see both files, run.ps1 and function.json i my Function App main blade Function/App Files. My files below:

run.ps1

# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"

function.json

{
    "bindings": [
      {
        "name": "Timer",
        "type": "timerTrigger",
        "direction": "in",
        "schedule": "* * * * * *"
      }
    ]
  }

I am not sure how to Create a Function based on these files, When I go to Overview I don't see any functions in Functions tab. There is no option to create a function using option Develop in Portal. I didn't find any useful info in MS documentation. Earlier when I was using Develop in the Portal option and I was able to create Function Apps manually from the portal. Now when I am using Azure DevOps pipelines to upload Function files (run.ps1, function.json) to Azure Function App, I am not sure what is recommended way to create Functions.


Solution

    1. When we create the Azure Function App, it shows the option to create and develop in Azure Portal:

    enter image description here

    1. Created PowerShell Core Timer Trigger Azure Function App in VS Code and pushed the code to the Azure DevOps Repository using git commands:

    enter image description here

    1. Click on “Set up build
    2. It takes you to pipeline Job step where repos and branch is automatically selected. Then Select the option shown below:

    enter image description here

    It will ask you to select Subscription, Function App created on Azure Portal.

    Below is the YAML Code of the pipeline created automatically :

    trigger:
    - main
    
    variables:
    azureSubscription: '<subscription_id>'
    
    functionAppName: 'dkazfnapp-ci-101'
    
    vmImageName: 'windows-2019'
    
    workingDirectory: '$(System.DefaultWorkingDirectory)/'
    
    stages:
    - stage: Build
    displayName: Build stage 
    
    jobs:
    - job: Build
    displayName: Build
    pool:
    vmImage: $(vmImageName)
    
    steps:
    - powershell: |
    if (Test-Path "extensions.csproj") {
    dotnet build extensions.csproj --output ./$(workingDirectory)/bin
    }
    displayName: 'Build extensions' 
    
    - task: ArchiveFiles@2
    displayName: 'Archive files'
    
    inputs:
    rootFolderOrFile: $(workingDirectory)
    includeRootFolder: false
    archiveType: zip
    archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
    replaceExistingArchive: true
    
    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
    artifact: drop
    
    - stage: Deploy
    displayName: Deploy stage
    dependsOn: Build
    condition: succeeded()
    
    jobs:
    - deployment: Deploy
    displayName: Deploy
    environment: $(functionAppName)
    pool:
    vmImage: $(vmImageName)
    
    strategy:
    runOnce:
    deploy:
    
    steps:
    - task: AzureFunctionApp@1
    displayName: 'Azure functions app deploy'
    inputs:
    azureSubscription: '$(azureSubscription)'
    appType: functionApp
    appName: $(functionAppName)
    package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
    

    For more information on pipeline customization, refer to this MS Document.

    Save and Run the pipeline.

    1. Build Succeeded and asking to grant the permission for deploy:
      enter image description here

    Click on “View” and “Permit” for deploy the function app.
    6. Deploy Succeeded:

    enter image description here

    Result:

    enter image description here

    enter image description here