CI-CD Azure devops for function app deployment - azure

My Function app Name:
my_func_dev_app
Functions I have in my project
func_dev
func_prd
Push func_dev to my_func_dev_app
Push func_prd to my_func_prd_app
But It is pushing both func_dev and func_prd to my_func_de_app
My app folder structure
(folder)myapp_function
(folder)func_dev --__init__.py
-- function.json
(folder)func_prd --__init__.py
-- function.json
task.py
Multi stage Pipeline:
#pipelines1
trigger:
- '*'
stages:
- stage: 'Build'
displayName: 'Build the web application'
jobs:
- job: 'Build'
displayName: 'Build job'
pool:
name: 'onprem_agent'
steps:
- task: CopyFiles#2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: |
**/*
!.git/**
OverWrite: true
CleanTargetFolder: true
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: ArchiveFiles#2
displayName: "Archive files"
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/myapp_function'
includeRootFolder: false
archiveFile: '$(Build.ArtifactStagingDirectory)/build$(Build.BuildId).zip'
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'myapp_function'
- stage: 'DEV_AZURE'
displayName: 'Az DEV Deploy'
dependsOn: Build
condition: |
and
(
succeeded(),
eq(variables['Build.SourceBranchName'], variables['releaseBranchName'])
)
jobs:
- deployment: Deploy
environment: dev
variables:
- group: Release
strategy:
runOnce:
deploy:
steps:
- download: current
artifact: myapp_function
displayName: Downloading artifacts
- task: AzureFunctionApp#1
inputs:
azureSubscription: CI-CD
appType: functionAppLinux
appName: my_func_dev_app
package: '$(Pipeline.Workspace)/myapp_function/*.zip'
deployToSlotOrASE: true
resourceGroupName: my-rg
slotName: PRODUCTION
displayName: 'Deploying dev'
How to fix? Push func_dev to my_func_dev_app and ignore func_prd

How to fix? Push func_dev to my_func_dev_app and ignore func_prd
To solve this issue, you can package your two projects separately into zip.
In your ArchiveFiles task, you need to specify a specific project.
For example:
For func_dev project: $(Build.ArtifactStagingDirectory)/myapp_function/func_dev
- task: ArchiveFiles#2
displayName: "Archive files"
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/myapp_function/func_dev'
includeRootFolder: false
archiveFile: '$(Build.ArtifactStagingDirectory)/build$(Build.BuildId)-func_dev.zip'
Then you can only package the project corresponding to the function app name.
If you want to deploy two projects to the corresponding function app in one pipeline at the same time, you could use the same method to separately package the two projects as zip.

Related

How to deploy two zipped Functions to the same Azure Function App?

I trying to deploy two zipped Functions to the same Azure Function App called http-trigger-api-test and the result is that the first one gets deployed but then it is replaced by the second function.
I have the tasks in the following yaml file:
- stage: Build
displayName: Build stage for Azure Function
jobs:
- job: Build
steps:
- task: ArchiveFiles#2
displayName: 'Archive HttpTriggerApi Azure Function'
inputs:
rootFolderOrFile: 'azure/azure_functions/HttpTriggerApi/function_code'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- task: ArchiveFiles#2
displayName: 'Archive HttpTriggerApiWeb Azure Function'
inputs:
rootFolderOrFile: 'azure/azure_functions/HttpTriggerApi/function_code_web'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId)2.zip
replaceExistingArchive: true
- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
displayName: 'Upload Azure Function Full Stack'
artifact: full_stack_deploy
- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId)2.zip
displayName: 'Upload Azure Function Web Stack'
artifact: web_deploy
- stage: TEST
displayName: Deploy stage Test
variables:
- group: optimizelyeventconnector-test-group
jobs:
- deployment: Deploy
displayName: Deploy
environment: optimizelyeventconnector-test
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI#1
displayName: 'Deploy Azure Function Full Stack'
inputs:
azureSubscription: 'test-group-SPN'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: |
az functionapp deployment source config-zip -g test-group -n http-trigger-api-test --src $(Pipeline.Workspace)/full_stack_deploy/$(Build.BuildId).zip
- task: AzureCLI#1
displayName: 'Deploy Azure Function Web Stack'
inputs:
azureSubscription: 'test-group-SPN'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: |
az functionapp deployment source config-zip -g test-group -n http-trigger-api-test --src $(Pipeline.Workspace)/web_deploy/$(Build.BuildId)2.zip
I have read some questions where it is suggested not to pack several functions into one function app unless they are related. In my case, both functions are related.
Any suggestion, comment or improvement to this questions is welcome.
After some research I found the solution.
I had to structure my function app in the following way:
- main_function_app
- functions
- Full_Stack
- configAPI.json
- configAPITest.json
- function.json
- index.js
- Web
- configAPI.json
- configAPITest.json
- function.json
- index.js
- host.json
- local.settings.json
- package-lock.json
- package.json
- proxies.json
- utils.js
When deploying to azure, you need to install node, dependencies, zip the files and publish them.
- stage: Build
displayName: Build stage for Azure Function
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: NodeTool#0
inputs:
versionSpec: '14.x'
displayName: 'Install Node.js'
- task: Bash#3
inputs:
targetType: 'inline'
script: 'npm install --prefix azure/azure_functions/HttpTriggerApi/main_function_app/functions'
- task: ArchiveFiles#2
displayName: 'Build Azure Functions and Dependencies'
inputs:
rootFolderOrFile: 'azure/azure_functions/HttpTriggerApi/main_function_app/functions'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
displayName: 'Publish Azure Functions Artifact'
artifact: functions

Azure Release Pipeline IISVirtualDirectory YAML

I'm having this Release created via Azure and its working perfectly. I have create the pipeline using YAML and i want to put the release in the same file. I'm using the "View YAML" approach for this to copy the YAML template and use it. But somehow its failing with this message:
ERROR ( message:Cannot find APP object with identifier "TEST01/". )
##[error]Process 'appcmd.exe' exited with code '1168'.
I guess im missing some other parameters or i really can't figure out how to use the same approach using YAML.
This is the code:
trigger:
- develop
stages:
- stage:
jobs:
- job: Build
displayName: Agent job 1
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
- task: ArchiveFiles#2
displayName: Archive $(System.DefaultWorkingDirectory)
inputs:
rootFolderOrFile: "$(System.DefaultWorkingDirectory)"
includeRootFolder: false
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: drop'
- stage:
jobs:
- job: Deploy
displayName: Deploy to IIS Dev
pool:
vmImage: windows-2019
steps:
- task: IISWebAppManagementOnMachineGroup#0
displayName: 'IIS Web App Manage'
inputs:
IISDeploymentType: iISVirtualDirectory
ParentWebsiteNameForVD: TEST01
VirtualPathForVD: /admin/test
PhysicalPathForVD: '%SystemDrive%\inetpub\_phpapps\test-center'
- task: IISWebAppDeploymentOnMachineGroup#0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: "TEST01"
VirtualApplication: "/admin/test"
TakeAppOfflineFlag: True
XmlVariableSubstitution: True
After some research and testing seams like there is a new approach regarding deployment/releases.
Instead of using "Pipelines > Release" and create a release, if you are using the Yaml approach (defining stages etc.) you need to use it with "Environments". Create the group or environment and use it.
Two steps here are missing, one is the package and the other is env, where should be deployed.
So the solution for the above code is the following:
- stage: 'DeployTest'
displayName: 'DeployTest'
dependsOn: 'Build'
condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'develop'))
jobs:
- deployment: Test
displayName: Test
environment:
name: 'ENV-TEST-GROUP'
resourceType: VirtualMachine
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppManagementOnMachineGroup#0
displayName: 'IIS Web App Manage'
inputs:
IISDeploymentType: iISVirtualDirectory
ParentWebsiteNameForVD: TEST01
VirtualPathForVD: /admin/test
PhysicalPathForVD: '%SystemDrive%\inetpub\_phpapps\test-center'
- task: IISWebAppDeploymentOnMachineGroup#0
displayName: 'IIS Web App Deploy'
inputs:
WebSiteName: "TEST01"
VirtualApplication: "/admin/test"
Package: $(Pipeline.Workspace)\drop\build_test.zip
TakeAppOfflineFlag: True
XmlVariableSubstitution: True

Azure DevOps - Deployment problems

I am trying to deploy a new code in an existing function on Azure but for some reason I am getting a Green/Pass pipeline but when I request the URL I got error 404.
What I have done:
Setup the function manually
Run a Pipeline with the stages:
a) mvn package
b) zip content of azure functions in the target
c) Deploy artifact from agent to the pipeline
d) Deploy artifact into a function using snipped code from microsoft.
The pipeline gets a green state and the function has been deployed:
Starting: AzureFunctionApp
==============================================================================
Task : Azure Functions
Description : Update a function app with .NET, Python, JavaScript, PowerShell, Java based web applications
Version : 1.195.0
Author : Microsoft Corporation
Help : https://aka.ms/azurefunctiontroubleshooting
==============================================================================
Got service connection details for Azure App Service:'test'
Trying to update App Service Application settings. Data: {"WEBSITE_RUN_FROM_PACKAGE":"https://teststorage.blob.core.windows.net/azure-pipelines-deploy/package_1639741028399.zip?***"}
Updated App Service Application settings.
Updated WEBSITE_RUN_FROM_PACKAGE Application setting to https://teststorage.blob.core.windows.net/azure-pipelines-deploy/package_1639743928399.zip?***
Syncing triggers for function app
Sync triggers for function app completed successfully
Successfully added release annotation to the Application Insight :test
App Service Application URL: http://test.azurewebsites.net
Finishing: AzureFunctionApp
but when I request the URL it fails, also I check the functions section in the portal , and the function that was there (deployed manually) got removed.
Note:
The code is fine because I can deploy manually the same code and it is working fine, via pipeline is not working.
Pipeline code:
pool:
vmImage: ubuntu-latest
variables:
serviceName: test
jdkVersion: "1.11"
stages:
- stage:
displayName: Build
jobs:
- job: "Deployment_draft"
steps:
- task: MavenAuthenticate#0
displayName: "Maven Authenticate"
inputs:
artifactsFeeds: test-artifactory
- task: ArchiveFiles#2
inputs:
rootFolderOrFile: $(Build.SourcesDirectory)/${{ variables.serviceName }}/target/azure-functions/${{ variables.serviceName }}
includeRootFolder: true
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: '${{ variables.serviceName }}'
publishLocation: 'Container'
- task: AzureFunctionApp#1
inputs:
azureSubscription: 'SubscriptionTest(Subscription ID)'
appType: 'functionAppLinux'
appName: 'test'
deploymentMethod: zipDeploy
package: '$(Build.ArtifactStagingDirectory)/**/*.zip'
rootFolderorFile - Enter the root folder or file path to add to the archive. If a folder, everything under the folder will be added to the resulting archive. Default value: $(Build.BinariesDirectory)
I have slightly modified your pipeline.
pool:
vmImage: ubuntu-latest
variables:
serviceName: test
jdkVersion: "1.11"
stages:
- stage:
displayName: Build
jobs:
- job: "Deployment_draft"
steps:
- task: MavenAuthenticate#0
displayName: "Maven Authenticate"
inputs:
artifactsFeeds: test-artifactory
- stage : deploy
jobs:
- job:
displayName : Function App update
steps:
- task: ArchiveFiles#2
inputs:
rootFolderOrFile: '$(Build.SourcesDirectory)'
includeRootFolder: true
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
ArtifactName: '${{ variables.serviceName }}'
publishLocation: 'Container'
- task: AzureFunctionApp#1
inputs:
azureSubscription: 'SubscriptionTest(Subscription ID)'
appType: 'functionAppLinux'
appName: 'test'
deploymentMethod: zipDeploy
package: '$(Build.ArtifactStagingDirectory)/**/*.zip'
Refer here

Excluding Certain Files in Azure CI Pipeline (YAML)

I have a CI pipeline (YAML) that builds a repo that will deploy into an existing Azure Function. The CI pipeline is doing it is job. However, after it is done, and I go to Function App -> App files -> I can see the azure-pipeline.yml is included in there (or i think it was included in the build process). I have tried using paths and exclude but they dont work. My question is, how do I exclude only that azure-pipeline.yml so that after the pipeline is done building, the azure-pipeline.yml is not in App files in Function App. Below is my YAML
trigger:
branches:
include:
- master
paths:
exclude:
- README.md
- azure-pipelines.yml
variables:
# Azure Resource Manager connection created during pipeline creation
azureSubscription: 'DevOps-Test'
# Function app name
functionAppName: 'test'
# Agent VM image name
vmImageName: 'vs2017-win2016'
# Working Directory
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
- task: DownloadBuildArtifacts#0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
- task: AzureFunctionApp#1
displayName: 'Azure functions app deploy'
inputs:
azureSubscription: '$(azureSubscription)'
appType: functionApp
appName: $(functionAppName)
package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
The following syntax means file README.md or azure-pipelines.yml won't trigger the build. It doesn't mean file README.md or azure-pipelines.yml are excluded in the working directory.
trigger:
branches:
include:
- master
paths:
exclude:
- README.md
- azure-pipelines.yml
I've noticed you tried archiving folder $(workingDirectory), and workingDirectory defined in variable which was actually $(System.DefaultWorkingDirectory)/. System.DefaultWorkingDirectory is the local path on the agent where your source code files are downloaded.
Obviously, file README.md and azure-pipelines.yml are in the source code, so they are archived too. You could add a CopyFiles task before ArchiveFiles task to copy files you need from a source folder to a target folder using match patterns, then archive the target folder. For example:
- task: CopyFiles#2
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory) '
inputs:
SourceFolder: '$(workingDirectory)'
Contents: |
**/*
!*.md
!*.yml
TargetFolder: '$(Build.ArtifactStagingDirectory) '
- task: ArchiveFiles#2
displayName: 'Archive files '
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory) '
Take a look here
- 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
You produce your ouptut here ./$(workingDirectory)/bin but you zipped rootFolderOrFile: $(workingDirectory). Please change it to rootFolderOrFile: $(workingDirectory)/bin.
EDIT
Please add this before calling archive
- script: |
rm README.md
rm azure-pipelines.yml
workingDirectory: $(workingDirectory)
So you will remove them from folder which you later archive.

Azure Pipeline (YAML) deploys an empty solution to my FunctionApp

I'm building a CI-CD azure pipeline with YAML to deploy an azure function app.
Everything works fine, I can even set some properties for the function app, but my function app remains empty.
I downloaded the zip artifact and it looks just fine.
trigger:
- master
variables:
variables:
buildConfiguration: 'Release'
Parameters.RestoreBuildProjects: '**/TestFunction.csproj'
stages:
- stage: Build
jobs:
- job:
pool:
vmImage: 'vs2017-win2016'
continueOnError: false
steps:
- task: DotNetCoreCLI#2
displayName: 'Restore'
inputs:
command: 'restore'
projects: '$(Parameters.RestoreBuildProjects)'
feedsToUse: 'select'
vstsFeed: '/0856b234-f3a6-4052-b5a6-ed9f6ec9c635'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(Parameters.RestoreBuildProjects)'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: 'Publish Build'
inputs:
command: publish
arguments: '--configuration $(BuildConfiguration)'
projects: '$(Parameters.RestoreBuildProjects)'
publishWebProjects: false
modifyOutputPath: true
zipAfterPublish: false
- task: ArchiveFiles#2
displayName: "Archive Files"
inputs:
rootFolderOrFile: "$(System.DefaultWorkingDirectory)"
includeRootFolder: false
archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip'
ArtifactName: 'drop'
- stage: Deploy
jobs:
# track deployments on the environment
- deployment:
pool:
vmImage: 'vs2017-win2016'
# creates an environment if it doesn’t exist
environment: 'dev'
strategy:
# default deployment strategy
runOnce:
deploy:
steps:
- task: DownloadBuildArtifacts#0
displayName: 'Download Artifacts'
inputs:
buildType: 'current'
downloadType: 'specific'
downloadPath: '$(System.ArtifactsDirectory)'
- task: AzureFunctionApp#1
displayName: 'Azure Function App Deploy: ngproductionfetcherfuncref'
inputs:
azureSubscription: 'Agder Energi Sandbox (1aa9cf92-9d42-49a6-8d31-876ac2dff562)'
appType: functionApp
appName: myFunctionApp
package: '$(System.ArtifactsDirectory)/**/*.zip'
appSettings: '-name0 value0 -name1 value1'
The pipeline is all green:
Got service connection details for Azure App Service:'myFunctionApp'
Updating App Service Application settings. Data: {"WEBSITE_RUN_FROM_PACKAGE":"1"} {"WEBSITE_RUN_FROM_ZIP":{"value":""}}
Updated App Service Application settings and Kudu Application settings.
Package deployment using ZIP Deploy initiated.
Successfully deployed web package to App Service.
Updating App Service Application settings. Data: {"name0":"value0","name1":"value1"} undefined
Updated App Service Application settings and Kudu Application settings.

Resources