I am working on an Azure pipeline for a dotnet core project that runs on Windows Self hosted agent.
I want my pipeline to use Git commands to check out the release branch and merge the develop branch into it. Next steps will be to build the release branch and deploy to intranet servers
I don’t know Git wording very good, I was using TFS for years. I use the commands below and got the logs here:
- task: CmdLine#2
displayName: Checkout Release branch
inputs:
script: |
#echo off
git checkout release
git pull develop
git status
From the logs, I understand:
It downloads the content of the develop branch because it is the default branch in GitHub, I’d rather want the release branch but I believe Azure is like that
I manage to switch to release but I have these errors that I don’t understand:
##[error]Previous HEAD position was bc94bd2 Update Staging Build Pipeline.yml
##[error]Switched to branch 'release'
I understood that pull can be used with local or remote branch so I use it to fetch and merge the develop branch to the release branch but I get: [error]fatal: 'develop' does not appear to be a git repository
Do I have to specify credentials on every calls to git?
On the last step, it fetches again the code from the develop branch and I understand why
If you could help me improve my script, that would be great,
Many thanks.
You can use git merge commands to merge branches. To merge develop branch into release branch you can use git merge origin/develop. Check the document for more information. See below example:
steps:
- checkout: self
persistCredentials: true
- task: cmdLine#2
inputs:
script: |
#echo off
git checkout release
git merge origin/develop
git status
However, it is not recommended to deploy release branch in above way. You can change the default branch of your azure pipeline to release branch and enabled the Continuous Integration trigger for release branch.
So that you can create a pull request to merge develop into release from the github UI or by using commands. After develop is merged into release, the azure pipeline will be automatically triggered to deploy from release branch. Note: the azure pipeline yaml file must exist in release branch too. See below steps:
1, To change azure pipeline branch from develop to release:
On your azure devops pipeline Edit page, Click the 3dots and click Triggers
Go to YAML tab--> Get Sources-->Click the 3dots to change the default branch.
2, Set CI trigger for release branch
In the azure pipeline yaml file, set the trigger to include release branch(You can also set PR trigger):
(Actually you do not need to follow above steps to change the default branch. You just need to include the azure pipeline yaml file in release branch and set the CI trigger to include release branch as below)
trigger:
branches:
include:
- release
exclude:
- develop #if you want disable CI trigger for develop branch
By adding the CI trigger to include the release branch in the azure pipeline yaml file. Azure pipeline will automatically be triggered on release branch when merging from develop into release branch.
Related
I have code stored inside various branches an Azure Dev Ops git repository.
Is it possible that a developer can somehow push to a branch, passing some sort of command line flag, that will stop any pipelines executing for that branch?
I have a number of pipelines that are set to run when changes are detected to my branches.
Workflow:
Developer pulls down branch (e.g. features/abc)
Developer pushes changes to that branch
My pipeline.yml file is then set up as follows:
trigger:
batch: true
branches:
include:
- features/*
When the developer pushes to the feature/abc that will start the pipeline.
I am wondering if there is some sort of flag I can set on the command line when I enter git push that will stop the pipeline from executing.
Yeah this is possible, one such way is to include [skip ci] in the commit message.
A full list of ways can be found in the documentation under the section header "Skipping CI for individual pushes"
I have a YAML build pipeline which resides on the repo PipelineRepo, branch master.
It contains a reference to a second repo AppRepo with a trigger on branch dev.
When a commit is made on AppRepo/dev, the build triggers and produces an artifact. The Build.SourceBranch predefined variable available during the build is of course dev as this was the triggering branch on AppRepo. This is as per the docs here:
https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/multi-repo-checkout?view=azure-devops#triggers
The specific part of the docs is this:
When an update to one of the repositories triggers a pipeline, then
the following variables are set based on triggering repository:
Build.Repository.ID
Build.Repository.Name
Build.Repository.Provider
Build.Repository.Uri
Build.SourceBranch
Build.SourceBranchName
Build.SourceVersion
Build.SourceVersionMessage
I consume the artifact in a Release pipeline, where I have a "Dev" stage with an artifact filter for branch dev:
I have a continuous deployment trigger on the artifact for branch dev:
Now we come to the problem
Every time a new build artifact is produced from the AppRepo/dev branch, the continuous deployment trigger doesn't fire because it thinks the build branch of the artifact was PipelineRepo/master. This is also true of the artifact filter on the stage - I have tested it by changing the continuous deployment trigger to master.
Note: you can see on the screenshot the build name contains the word "dev". This is because I use the Build.SourceBranch var in my custom build name. This proves the artifact is definitely produced by the AppRepo/dev triggering branch.
How can I get the DevOps Release Pipeline to pick up the triggering branch?
Based on your description, I could reproduce the similar issue in my organization.
When the pipeline is triggered by another repo branch, it still show the master branch in Release artifacts.
At the same time, the display of the trigger branch is also inconsistent in Pipelines.
For example:
Overview:
Detail view:
I suggest that you could create a feedback ticket in Our feedback Site.
For a workaround:
You could add a build tag in Pipeline to distinguish artifacts.
Yaml sample:
resources:
repositories:
- repository: test
type: git
name: 123/ARM
trigger:
- test
steps:
- checkout: test
- script: echo "##vso[build.addbuildtag]$(Build.SourceBranch)"
Release Pipeline:
You could set the master branch and add the build tag.
I am setting up CI/CD at work and there is one step I’m not sure how to do and furthermore, if it is a right thing do.
For background, I am used to develop in C# with Visual Studio, source code in TFS and deploying with basic script that copies files on the intranet.
Now, I’m requested to setup Build and Release pipelines on Dot Net Core projects in GitHub.
I have three branches on this project: DEV, RELEASE and MASTER
I created one pipeline that triggers on DEV’s commits, creates an artefact and deploy to DEV server.
Those are the pipelines that deploy all developers work to a DEV server where they run their own tests.
Next step, when we want to deploy to staging servers, we click a button in Azure, this merge the DEV branch to the RELEASE branch but I know close to nothing in GitHub, not even sure those are the appropriate words.
When the merge is done, this will trigger a build pipeline that will create a different artefact, when this artefact is updated, deploy to staging server.
Once this release is validated on Staging and Quality, we would merge RELEASE to MASTER and do the same until PROD servers. It is all on intranet and self-hosted agents.
Is that a good way of doing things? Can it be done this way? I need a PowerShell task or is there something that exists?
If you are using Azure DevOps pipeline, the pipeline should select GitHub for repository type, then we can configure the CI trigger.
a. Configure CI trigger:
Classic steps:
1.Open project setting->Service connections->select GitHub-> create a new GitHub service connection
2.Create a new build pipeline via classic editor-> Select GitHub as the source.
3.Open pipeline->select the tab Triggers-> enable the option Enable continuous integration and configure the Branch filters
b.YAML steps:
1.Open project setting->Service connections->select GitHub-> create a new GitHub service connection
2.Create a new build pipeline and select GitHub(YAML)
c.The sample of Check out GitHub repositories in your pipeline
resources:
repositories:
- repository: MyGitHubRepo # The name used to reference this repository in the checkout step
type: github
endpoint: MyGitHubServiceConnection
name: MyGitHubOrgOrUser/MyGitHubRepo
trigger:
- {branch name}
Configure CD trigger:
Please refer to this doc to configure the release trigger.
If you are using GitHub action.
Please select the correct workflow to configure the CI/CD, Please refer this doc for more details
CI sample:
on:
push:
branches:
- ' DEV'
Update1
When Dev branch is updated, it is built and deployed to Dev server by pipeline.
Create build A and release B, configure the CI build trigger, when the branch Dev is updated, it triggers the build pipeline A, and when build pipeline A is completed, it triggers the release pipeline B.
click a button to synchronize Dev branch to Release branch.
We cannot see the button, as a workaround, we can add task powershell and call the API to create pull request and complete the pull request. We also can add task cmd and publish the code via git cmd
I'm hoping to somehow replicate the functionality of PR triggers which, according to the docs, are currently only supported for GitHub and Bitbucket Cloud repos. I'd like my CI pipelines to not trigger if the change is incoming from certain branches.
I've mostly tried to solve this problem with GitVersion, which is the part of my pipeline that makes it problematic to trigger builds when I'm merging back from a release build or master back onto develop. So far I've had no luck, so now I'm hoping I've overlooked a feature of Azure Pipelines which will help.
My current pipeline trigger:
trigger:
batch: true
branches:
include:
- develop
paths:
exclude:
- ReadMe.md
- development-pipeline.yml
- release-pipeline.yml
- GitVersion.yml
I'd like a pull request which originated in a release branch (can be identified with the regex pattern [Rr]eleases?[\/-]) or master to not trigger my pipeline. In reality, any change to the develop branch triggers the build.
If you just want the develop branch not to trigger ci build, then you can check "Enable continuous integration" option in the Continuous integration of builds Triggers and set exclude develop branch in the branch filters.
If you want some source branches to trigger the CI build of the develop branch, some can't. I am afraid that this feature you want is not achievable. Once your deveop branch
Enable continuous integration, then the deveop branch will trigger the build pipeline once it changes.
If you want to merge the commits on the release or master branch into the develop branch, and create pr does not trigger the CI build, you could enable the build policy in the Build validation in the branch policy.In this way, only after PR is completed will CI build be triggered.But pr build is unavoidable.
I have my git repository hosted on Azure DevOps. I created a new yaml based build pipeline in the master branch and have set the trigger section to two existing branches. Other branches don't have a azure-pipeline.yml file nor any kind of branch policies are set for this DevOps project.
trigger:
batch: 'true'
branches:
include:
- master
- develop
The trigger gets invoked for every change in the master branch as expected. But is ignoring any pushes to the develop branch.
If I configure a build pipeline with the visual editor and define the exact two branches there, for every push a build will be triggered.
Any idea how Azure Pipeline respects the build definition also for other branches without copy and pasting the whole definition for every possible branch?
Build trigger is not invoked on push for Azure Git repositories
I have created a sample with the syntax:
trigger:
batch: 'true'
branches:
include:
- master
- Dev
And it works fine on my side. Then I check the new project you provided, but I found that the .yml file is incomplete and does not contain a trigger: node.
So, to resolve this issue, we need to double check the .yml file you modified in under the master branch, and you build .yml file is you modified.
Besides, when we edit the build pipeline, there is a extended button, we could select the option Triggers to set the build trigger with visual editor:
If above not help you, you can try to create a new build pipeline, set the trigger only with Develop branch, check if it works fine, then return to the previous with master
and develop branch.
If all of the above methods are not worked, you may need share a detailed sample and some steps, the reason for this problem may be hidden in the corner we ignore.
Hope this helps.