Build pipeline not triggered on PR to master - azure

On my master branch I have azure-pipelines.yml file with:
trigger:
- master
I've created a new PR into the master branch:
But nothing is happening in my build pipeline.
If I complete the PR a the master branch is build though.
Continuous integration (CI) triggers cause a build to run whenever a push is made to the specified branches or a specified tag is pushed.
Am I misreading this? How do I trigger a build on a PR with the Master branch, but without completing the PR?

If you want a build to run during a PR, you should set up a branch policy that specifies that build as a validation build.

for trigger on PR you need to use PR trigger:
pr:
autoCancel: boolean # indicates whether additional pushes to a PR should cancel in-progress runs for the same PR. Defaults to true
branches:
include: [ string ] # branch names which will trigger a build
exclude: [ string ] # branch names which will not
paths:
include: [ string ] # file paths which must match to trigger a build
exclude: [ string ] # file paths which will not trigger a build
https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#pr-trigger

Related

Azure Devops build pipeline: CI triggers not working on PR merge to a branch when there is no work item is attached with the PR

I need to trigger the pipeline(CI) when any change is made (directly to the branch or by merging a PR) to my_branch, my yml trigger configuration is like this -
trigger:
batch: true
branches:
include:
- my_branch
paths:
include:
- path/of/the/directory
This works fine if a work item is attached with a PR and the PR is merged with the my_branch.
But, when there is no work item attached to a PR - CI is not triggering after merging the PR.
Am I missing anything?
I tried to reproduce the same in my environment and got the results successfully like below:
 Step 1: Created a sample repository. (No branch policies are applied at this stage) 
Step 2: Create a basic yaml build pipeline as below. 
trigger:
branches:
batch: true
include:
- main
pool:
vmImage: ubuntu-latest
steps:
- script: echo Hello, world!
displayName: 'Sample script'
Step 3: Create a new branch from the main branch. 
  
Step 4: Modify or add some code to the newly created branch and create a Pull Request to the main branch and verify build pipeline.
     
Note: As I did not set any pull request policies on the main branch, the build pipeline will get triggered only after the feature branch merged to the main branch.

GitLab CI/CD yml - How to add into pipeline job if new branch created. And exclude for further pushes?

In our git process we have hothix process. The process is to create a hotfix brunch from master and make changes and then merge to master back.
The issue that I'm faсing now is how to add job to pipelne if branch just created and exclude from pipeline for any further push?
When branch is created we need to set up custom environment. Create a sandbox, push code and push some test data. This is need only when branch is created. In case of changes and pushes, environment is available and no need in building it again. Just build code and push a release tag
I'm trying something like this:
job-name:
stage: build
script:
- myScript
rules:
- if: '$CI_COMMIT_BRANCH =~ /^hotfix.*/'
changes:
- force-app/**/*
when: never
- if: '$CI_COMMIT_BRANCH =~ /^hotfix.*/'
allow_failure: false
when: manual
And for sure it's not working as changes: not part of if policy:
Rules are evaluated in order until a match is found. If a match is found, the attributes are checked to see if the job should be added to the pipeline.

Gitlab - how to allow multi project pipelines to create a branch in trigger repo

I have a repo A(upstream) and a repo B(downstream). After some jobs complete in repo A; I want to trigger some jobs in repo B. I was able to achieve that.
trigger_repo_B:
stage: trigger_repo_B
trigger:
project: test/repo_B
What I havent been able to figure out is - how do I go about triggering repo B jobs for a non-existent branch in repo B. For example I can trigger jobs in repo B for a specific branch C if C exists but if C does not exist the pipeline is in a pending state. I want to be able to create a branch in B and then run the jobs in B if the branch C does not exist.
trigger_repo_B:
stage: trigger_repo_B
trigger:
project: test/repo_B
branch: C
Any ideas? The only way I could think of it working is to do a before_script where I clone the repo and create a branch before triggering the pipeline in B
Instead of listing the branch name in the trigger config, instead pass a variable where the value is the branch you want to create. Then in your downstream pipeline, you could add a stage and job to run before everything else that checks to see if the variable exists. If not, just exit 0 and let the pipeline continue as usual. But if it is set, create the branch, then continue.
That could look something like this:
#repo_A .gitlab-ci.yml
stages:
- trigger_repo_B
trigger:
stage: trigger_repo_B
variables:
BRANCH_TO_CREATE: branch_name
trigger:
- project: test/repo_B
#repo_B .gitlab-ci.yml
stages:
- prep_work
- build
Create Branch:
stage: prep_work
script:
- if [ -z ${BRANCH_TO_CREATE+x} ]; then git checkout -b $BRANCH_TO_CREATE; fi
# see https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash to explain the conditional above
Build:
stage: build
script:
- ...

Azure Devops build pipeline: CI triggers not working on PR merge to master when PR set to none

Need to trigger CI build job when a PR is merged to master (only when change is inside ./rel/* path) but not have CI build triggered when the Pull Request (PR) is created. So I have the trigger config as below.
trigger:
branches:
include:
- master
paths:
include:
- ./rel/*
pr: none # will disable PR builds (but not CI builds)
But it fails to trigger CI build when pr: none is added. If pr: none is removed, The Job is getting triggered for both PR and a merge to master. I would only need the job/CI build to run on a merge to master.
The paths filter in the YAML is looking at paths in your repository file structure, not the branch path. To have it only trigger on rel branch, replace the master under the include branches with ./rel/* (or the correct value).
We have a more defined pipeline that runs unit tests on PR and then only packages for release on merge into the master branch. We do this by having our trigger set to the master branch and using conditions for each stage in the multi-stage pipeline. Check those out as well.
Solved! This works now.
trigger:
branches:
include:
- master
paths:
include:
- ./rel/*
pr: none # will disable PR builds (but not CI builds)
Also you can configure / override using the classic azure devops UI from Triggers.
Ref : https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/azure-repos-git?view=azure-devops&tabs=classic#ci-triggers

How to exclude one branch in Azure DevOps build pipeline

In Azure DevOps its possible to define build pipelines which by default is executed when a code is pushed to a branch.
I want the pipeline to run for all branches except one. The configuration
trigger:
branches:
include:
- branchToInclude
exclude:
- branchToExclude
works when I push to branchToInclude.
But if I take away the include part the pipeline is not executed when I push to branchToInclude.
trigger:
branches:
exclude:
- branchToExclude
A * instead of branchToInclude is not accepted when I try to save the file. Is there a way to configure a pipeline to execute for all branches except one?
I suppose something like this should work
trigger:
branches:
include:
- '*'
exclude:
- branchToExclude

Resources