Azure DevOps: CI Pipeline executing after merge - azure

I have implemented a pipeline that will build and test the application once a Pull Request have been created. It works fine but it will run again once the PR is merged, and I only want it to run in the creation of a PR.
The Yaml fine goes like:
trigger:
- development
pool:
vmImage: ubuntu-latest
variables:
buildConfiguration: 'Release'
steps:
- task: UseDotNet#2
displayName: 'Use .NET Core sdk 3.1.000'
inputs:
version: 3.1.x
- task: DotNetCoreCLI#2
displayName: 'Build the application'
inputs:
command: 'build'
projects: 'example.csproj'
configuration: 'Release'
- task: DotNetCoreCLI#2
displayName: 'Run unit tests'
inputs:
command: 'test'
projects: 'example.csproj'
configuration: 'Release'

kraego is right.
You will have to remove the trigger section within your pipeline and configure a build validation branch policy for that build.
See here.

Related

Azure FunctionApp without functions after successful CI/CD over Azure DevOps

I've created an Azure Function App (Linux) running on an App Service Plan Y1 and have my sources in Azure DevOps Git. The functions are written in C# on DOTNET 6.
Below you can see my YAML definitions for the CI and a separated CD pipeline. When I execute the pipeline everything works well (both are green). After the deployment however this is how my Azure Portal looks in the Functions blade:
Using the VS Code Azure Extension and looking into the files of the function app I get:
When I look in the artifact of the CI pipeline everything looks good (explorer view of the downloaded zip):
The bin-folder is populated there.
Some key points:
I have no slots in the function app.
The deployments are visible in the associated App Insights.
My reference point is Microsoft Learn
So is it normal to get the 404 when searching in VS code and did somebody experience something similar or even know the solution?
Sidenote:
I used to deploy my function using VS Code with extensions install. Today I now get a weired error message after the deployment:
CI YAML
pool:
vmImage: 'ubuntu-latest'
trigger: none
variables:
- name: 'Solution'
value: '**/MyProject.sln'
- name: 'ProjectFilter'
value: '**/*.csproj'
- name: 'BuildConfiguration'
value: 'Release'
steps:
- task: UseDotNet#2
displayName: Use DotNet 6
inputs:
version: '6.0.x'
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: restore
projects: '$(ProjectFilter)'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(ProjectFilter)'
arguments: '--no-restore --configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Publish Image Converter
inputs:
command: publish
projects: src/Functions/**/MyProject.csproj
publishWebProjects: false
arguments: '--no-restore --no-build --configuration $(BuildConfiguration) --output $(Build.DefaultWorkingDirectory)/function_publish_output'
zipAfterPublish: false
- task: ArchiveFiles#2
displayName: Archive Image Converter
inputs:
rootFolderOrFile: '$(Build.DefaultWorkingDirectory)/function_publish_output'
includeRootFolder: false
archiveFile: '$(Build.ArtifactStagingDirectory)/MyProject.zip'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
condition: succeededOrFailed()
CD YAML
variables:
- name: 'vmImageName'
value: 'ubuntu-latest'
- name: 'serviceConnectionName'
value: 'MYCONN'
- name: 'project'
value: 'MYPROJECT'
resources:
pipelines:
- pipeline: ci
source: 'NAMEOFCI'
trigger: true
pool:
vmImage: $(vmImageName)
trigger: none
stages:
- stage: Production
displayName: Production
jobs:
- deployment: Deploy
displayName: 'Deploy'
environment: 'Production'
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- download: ci
displayName: 'Download Artifact'
artifact: drop
- task: AzureFunctionApp#1
displayName: 'Deploy Image Converter Function'
inputs:
azureSubscription: '$(serviceConnectionName)'
appType: functionAppLinux
appName: 'fapp-**********-prod'
package: '$(Pipeline.Workspace)/ci/drop/MyProject.zip'
runtimeStack: 'DOTNET|6.0'
Can you check what exactly you see on the file system? You can use console or Advanced tools:

CI Triggers on Pipelines in Azure

Im having real issues with a pipeline everytime someone commits or pushes something to a branch on our repo, the pipeline triggers, in following the Microsoft Doc: https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=yaml#ci-triggers
Putting in Exclude features on every branch that we have the pipeline will still run when someone does a commit to a local branch even if I have wild carded the branch.
Has anyone been able to get this to work, that the pipeline should only run when there is a commit to Master only and nothing else.
Here is my Code:
trigger:
branches:
include:
- master
exclude:
- CICV/*
- An/*
- Prod/*
- Test/*
- Dev/*
- dev/*
- IN/*
- id/*
- St/*
- tr/*
pool:
vmImage: 'windows-latest'
demands: npm
variables:
System.Debug: false
azureSubscription: 'RunPipelinesInProd'
RG: 'VALUE'
Location: UK South
containername: 'private'
appconnectionname: 'RunPipelinesInProd'
jobs:
- job: job1
displayName: Create And Publish Artifact
pool:
vmImage: vs2017-win2016
steps:
- task: UseDotNet#2
displayName: Use .Net Core 3.1.x SDK
inputs:
packageType: 'sdk'
version: '3.1.x'
- task: DotNetCoreCLI#2
displayName: dotnet restore
inputs:
command: restore
projects: 'Website.csproj'
- task: Npm#1
displayName: 'npm install'
inputs:
workingDir: ClientApp
verbose: false
- task: Npm#1
displayName: 'npm run build'
inputs:
command: 'custom'
workingDir: ClientApp
customCommand: 'build'
- task: DotNetCoreCLI#2
displayName: dotnet build
inputs:
projects: 'Website.csproj'
arguments: '--configuration Release'
- task: DotNetCoreCLI#2
displayName: dotnet Test
inputs:
command: test
projects: 'UnitTests/UnitTests.csproj'
arguments: '--configuration Release'
- task: DotNetCoreCLI#2
displayName: dotnet publish
inputs:
command: publish
projects: 'Website.csproj'
arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: true
modifyOutputPath: false
- task: PublishPipelineArtifact#1
displayName: Publish Pipeline Artifact
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifact: 'Website'
publishLocation: 'pipeline'
- job: job2
displayName: Create Web App
dependsOn: job1
steps:
# Download Artifact File
- download: none
- task: DownloadPipelineArtifact#2
displayName: 'Download Build Artifacts'
inputs:
patterns: '**/*.zip'
path: '$(Build.ArtifactStagingDirectory)'
# deploy to Azure Web App
- task: AzureWebApp#1
displayName: 'Azure Web App Deploy: nsclassroom-dgyn27h2dfoyo'
inputs:
package: $(Build.ArtifactStagingDirectory)/**/*.zip
azureSubscription: $(azureSubscription)
ConnectedServiceName: $(appconnectionname)
appName: 'VALUE'
ResourceGroupName: $(RG)
You don't need a complex trigger like the one you outlined to trigger the pipeline on pushes to master. The following simple trigger configuration should work:
trigger:
- master
If there's anything in the include section, then only pushes to these branches trigger the build. If you specify both include and exclude sections, then it will try to exclude some subset from the include set - just like in the sample from the docs:
# specific branch build
trigger:
branches:
include:
- master
- releases/*
exclude:
- releases/old*
If the pipeline is still triggered by the push to some other branch, then it must be something else that triggers it.
As mentioned by #yan-sklyraneko in this answer your trigger configuration should be as simple as
trigger:
- master
However the trigger in your YAML file can be overridden in the GUI. Navigate to your pipeline and click Edit then click the ellipses as shown below and select Triggers
On that screen check that the Override the YAML continuous integration trigger from here box is unticked
I solved this in the end, I ended up down the route of managing through the Azure Dev Ops Portal.
It seems that if you try to use YAML to manage this it just doesn't work, but if you do it through the web interface as outlined in Answer 2, then the behaviour is as expected. I think that the Microsoft YAML part is broken for this but I already have three issues open with Microsoft I don't wish to add another one to follow and tag.

How can I set the EnvironmentName for XML transformation during a deployment job?

Alright folks, giving Azure multi-stage-pipelines feature a go and not having much luck getting xml transformations using a deployment job to work.
Update: This is not using the Classic Deploy/Release UI in Azure DevOps
What I've done so far:
remove transformations from the build process. Attempting to build once and deploy everywhere
verified that the web.{stage}.config files are included in the webdeploy package by removing node in csproj
Set up stage with name of 'Development'
pipeline yaml
trigger:
batch: false # If batch set to true, when a pipeline is running, the system waits until the run is completed,
branches:
include:
- staging-devops
paths:
include:
- myProject/*
- API/*
variables:
BuildConfiguration: 'Release'
BuildPlatform: 'Any CPU'
System.Debug: true
stages:
- stage: Build
displayName: 'Build project'
jobs:
- job:
displayName: 'Build and package client'
pool:
vmImage: 'vs2017-win2016'
demands:
- msbuild
- visualstudio
steps:
- task: VSBuild#1
displayName: 'Visual Studio build'
inputs:
solution: 'myProject/myProject.csproj'
vsVersion: '15.0'
msbuildArgs: '/p:DeployOnBuild=true /p:AutoParameterizationWebConfigConnectionStrings=False /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
platform: 'AnyCPU'
configuration: 'Release'
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(build.artifactstagingdirectory)'
artifactName: 'myProject-web-client'
- job:
displayName: 'Build and package API'
pool:
vmImage: 'vs2017-win2016'
demands:
- msbuild
- visualstudio
steps:
# add caching of nuget packages
- task: NuGetToolInstaller#0
displayName: 'Use NuGet 4.4.1'
inputs:
versionSpec: 4.4.1
- task: NuGetCommand#2
displayName: 'NuGet restore'
inputs:
restoreSolution: 'API/myAPI.sln'
- task: VSBuild#1
displayName: 'Visual Studio build'
inputs:
solution: 'API/myAPI.sln'
vsVersion: '15.0'
msbuildArgs: '/p:DeployOnBuild=true /p:AutoParameterizationWebConfigConnectionStrings=False /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
platform: '$(BuildPlatform)'
# configuration: '$(BuildConfiguration)'
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(build.artifactstagingdirectory)'
artifactName: 'myProject-api'
- stage: Development
displayName: 'Development'
dependsOn: Build
condition: succeeded('Build') #add check if artifact is available
jobs:
- deployment: DeploymyProjectClient
displayName: 'Deploy web client'
timeoutInMinutes: 30
pool:
vmImage: "windows-latest"
environment:
name: Staging
resourceType: VirtualMachine
tags: web
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppDeploymentOnMachineGroup#0
displayName: 'Deploy web application (myProject)'
inputs:
webSiteName: myProjectDev
package: '$(Pipeline.Workspace)/myProject-web-client/**/*.zip'
removeAdditionalFilesFlag: true
- deployment: Development
displayName: 'Development'
timeoutInMinutes: 30
pool:
vmImage: "windows-latest"
environment:
name: Staging
resourceType: VirtualMachine
tags: web
strategy:
runOnce:
deploy:
steps:
- task: IISWebAppDeploymentOnMachineGroup#0
displayName: Development
inputs:
webSiteName: 'WebAPI-Test'
package: '$(Pipeline.Workspace)/myProject-api/**/*.zip'
xmlTransformation: true
I've tried variations of the stage name, displayname, deployment name, etc still can't get the transformation for the Development stage to fire.
I do get the web.config and web.release.config to work but that is it.
2020-05-02T05:26:04.9272125Z ##[debug]adjustedPattern: 'C:\azagent\A2\_work\_temp\temp_web_package_8799433796999105\**/*.config'
2020-05-02T05:26:04.9343033Z ##[debug]9 matches
2020-05-02T05:26:04.9345300Z ##[debug]9 final results
2020-05-02T05:26:04.9351908Z ##[debug]Applying XDT Transformation : C:\azagent\A2\_work\_temp\temp_web_package_8799433796999105\Content\D_C\a\1\s\API\obj\Debug\Package\PackageTmp\Web.Release.config -> C:\azagent\A2\_work\_temp\temp_web_package_8799433796999105\Content\D_C\a\1\s\API\obj\Debug\Package\PackageTmp\Web.config
When reviewing the log file for deployment I do see the following
##[debug]Release.EnvironmentName=undefined
How can I set the stage name properly in order for the transformations to be applied during deployment??
How can I set the EnvironmentName for XML transformation during a deployment job?
This is known issue that has already been reported to product team.
As workaround, you could try to set the variable Release.EnvironmentName on the stage-level and on the job-level:
- stage: Development
displayName: 'Development'
dependsOn: Build
condition: succeeded('Build') #add check if artifact is available
variables:
Release.EnvironmentName: Development
jobs:
Then, the environment-specific config transformation was triggered.
Hope this helps.
Please try $(System.StageName).

Azure Devops Build Pipeline for solution with multiple project types

I have a solution that contains the following
Several Asp.net Projects (Microservices and Gateway)
.Net Core + Angular 8 (Front End)
When I hit the build button in Visual Studio every project is built. I have created a repo and uploaded the solution. Now I'm trying to figure out how to setup the pipeline to build each microservice and deploy them to individual Azure Web Apps.
I have the following Pipeline for the Angular Project. Should I define separate tasks like this? Is there a way to replicate the Visual Studio build here?
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: Npm#1
inputs:
command: install
workingDir: 'd:\a\1\s\Ok.Web\ClientApp'
- task: Npm#1
inputs:
command: custom
workingDir: 'd:\a\1\s\Ok.Web\ClientApp'
customCommand: run build
- task: CopyFiles#2
inputs:
targetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts#1
You may apply one of two approaches here:
One pipeline for whole repo
One pipeline for project
In both cases you may use templates to avoid repeating yourself; so you will define common tasks for building a .NET project and then use them in pipelines. I recently made a blog post about this, but please take a look at the documentation too.
To achieve this you need to first define a YAML file with common steps. For instance:
parameters:
- name: buildConfiguration # name of the parameter; required
default: 'Release'
- name: projectFolder
default: ' '
steps:
- task: DotNetCoreCLI#2
displayName: Restore nuget packages
inputs:
command: restore
projects: '*.csproj'
workingDirectory: '${{ parameters.projectFolder}}'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '${{ parameters.projectFolder}}'
arguments: '--configuration ${{ parameters.buildConfiguration }}'
# You just added coverlet.collector to use 'XPlat Code Coverage'
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
projects: '*Tests/*.csproj'
arguments: '--configuration ${{ parameters.buildConfiguration }} --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
workingDirectory: '${{ parameters.projectFolder}}'
- task: DotNetCoreCLI#2
inputs:
command: custom
custom: tool
arguments: install --tool-path . dotnet-reportgenerator-globaltool
displayName: Install ReportGenerator tool
- script: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:${{ parameters.projectFolder}}/coverlet/reports -reporttypes:"Cobertura"
displayName: Create reports
- task: PublishCodeCoverageResults#1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: ${{ parameters.projectFolder}}/coverlet/reports/Cobertura.xml
And then invoke this file from you main build file:
variables:
buildConfiguration: 'Release'
projectFolder: 'path to your project'
steps:
- template: build-and-test.yaml
parameters:
buildConfiguration: $(buildConfiguration)
- script: echo Some steps to create artifacts!
displayName: 'Run a one-line script'
In approach number 1 you will build all projects even if you change just one project, so I would recommend you use approach number 2. For this you may define multiple pipelines (one per project) and limit triggers to changes in specific folder. Please take a look here.
Here you have an example of how you can limit triggers to specific folder for master branch:
trigger:
branches:
include:
- master
paths:
include:
- gated-checkin-with-template/*
exclude:
- gated-checkin-with-template/azure-pipelines-gc.yml

Deploying and running .exe files using Azure Pipelines

I´m struggling to make my MultiStage pipelines to run a .exe file in a hosted agent running in a Azure VM.
My .yaml file is:
trigger:
- develop
stages:
- stage: build
displayName: Build
jobs:
- job: buildJob
pool:
vmImage: 'ubuntu-16.04'
variables:
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
inputs:
versionSpec: '5.5.0'
- task: DotNetCoreCLI#2
displayName: 'Dotnet Build $(buildConfiguration)'
inputs:
command: 'build'
arguments: '--configuration $(buildConfiguration)'
projects: '**/TestProj.csproj'
- task: DotNetCoreCLI#2
displayName: "Publish"
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/TestProj.csproj'
arguments: '--no-restore --configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: false
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: Container
- stage: Release
displayName: Release
dependsOn: build
jobs:
- deployment: AzureVMDeploy
displayName: agentDeploy
environment:
name: AzureDeploy
resourceName: vmName
resourceType: VirtualMachine
tags: develop
This VM is on the azure pipelines Environment. After I run this pipeline, the folder is downloaded into the VM, but I cannot find how to automate the execution of the output .exe file in this folder.
I think the way is to create a job with a task to do it, but I cannot figure out how to set the agent installed on the VM to run this task.
How can I do that?
If I understood you correctly, you want to execute your artifact file which was deployed to VM.
I think that PowerShell on Target Machines task should do the job for you. Yoy can write simple inline script to exeute your file. However, you need to have remoting confogured on VM. This article may help you with this.
You could specify tasks in strategy: Deployment job For example:
YAML
stages:
- stage: build
jobs:
- job: buildJob
pool:
vmImage: 'Ubuntu-16.04'
steps:
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Pipeline.Workspace)'
publishLocation: 'pipeline'
- stage: deploy
dependsOn: build
jobs:
- deployment: DeployWeb
displayName: deploy Web App
environment:
name: vm1
resourceType: virtualmachine
strategy:
runOnce:
deploy:
steps:
- script: echo my first deployment
- task: CmdLine#2
inputs:
script: 'more README.md'
workingDirectory: '$(Pipeline.Workspace)/build.buildJob/s'
For this YAML pipeline, I publish all files in pipeline workspace to artifact in build stage, then this artifact will be download to target virtual machine of vm1 environment in deploy stage (folder name will be {stage name}.{job name}), then run command line task to get a file's content. (The script and command line tasks will be run on that virtual machine)

Resources