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 - azure

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.

Related

How do I set up PR validations in Azure DevOps/GitHub?

We are migrating from Azure DevOps to GitHub and we have Build Validations set up where if you make a change in a specific folder, the respective CI pipeline will run when a PR is created.
I am trying to make use of the PR triggers in my YAML file, however when I open a PR it doesn't seem to work.
My pipeline is:
trigger: none
pr:
branches:
include:
- develop
- release/*
- ProductionSupport/*
paths:
include:
- cicd/pipelines/common/pre-commit-ci.yaml
- src
- cicd
pool:
vmImage: ubuntu-latest
variables:
PRE_COMMIT_HOME: $(Pipeline.Workspace)/pre-commit-cache
steps:
- bash: echo "##vso[task.setvariable variable=PY]`python -V`"
displayName: Get python version
- task: Cache#2
inputs:
key: pre-commit | .pre-commit-config.yaml | "$(PY)"
path: $(PRE_COMMIT_HOME)
- bash: |
pip install --quiet pre-commit
pre-commit run
displayName: 'Run pre-commit'
As a test to make sure my branches/paths were correct I updated the triggers section to:
trigger:
branches:
include:
- develop
- release/*
- ProductionSupport/*
paths:
include:
- cicd/pipelines/common/pre-commit-ci.yaml
- src
- cicd
Then when I made a change in one of the files in these folders, the pipeline was successfully triggered. Am I specifying my PR validation incorrectly?
Your yml definition seems correct.
Since you mentioned the CI trigger work fine and you mentioned We are migrating from Azure DevOps to GitHub.
This brings me a idea that a situation that exactly reproduces what you're experiencing and you might not expect:
PR Trigger Override
For example, if your pipeline is the same one as before(Just change the pipeline source), and you didn't delete the previous build validation(Or previous pipeline name is same as the current one), then the pr part in your github yml file will be override, only the build validation on DevOps side will work.
I suggest you investigate whether you have some build validation settings to the pipeline(If your project structure is complex, this maybe difficult to find) or you can simply create a totally new pipeline with the new YAML file.

Specify build branch based on branch used in triggering pipeline Azure Devops

Say I have two pipelines: PL1 and PL2 and that PL2 is triggered by PL1. Say I ran PL1 on the master branch, I want PL2 to also get triggered for the master branch. When PL1 is triggered on another branch, like releases/X.X.X I want PL2 to also be triggered on the releases/X.X.X branch. Can I do that? Right now PL2 is always using master when it gets triggered
I tried to replicate the scenario.
First pipeline:
trigger:
- none
stages:
- stage: first
jobs:
- job: firstJob
continueOnError: false
steps:
- bash: echo "first from test branch"
Second Pipeline:
resources:
pipelines:
- pipeline: firstPipeline
source: first-pipeline
trigger:
branches:
include:
- ${Build.SourceBranch}
stages:
- stage: second
jobs:
- job: secondJob
steps:
- bash: echo Test-branch
I tested these from two different branches and each time the source code of second pipeline was picked up based on the first pipeline's branch.
PS: Both of my pipeline YAML are in same repository

pr not triggered when opening github PR (Azure pipeline YAML)

The goal
I'm pretty new to Azure and pipelines, and I'm trying to trigger a pipeline from a pr in Azure. The repo lives in Github.
Here is the pipeline yaml: pipeline.yml
trigger: none # I turned this off for to stop push triggers (but they work fine)
pr:
branches:
include:
- "*" # This does not trigger the pipeline
stages:
- stage: EchoTriggerStage
displayName: Echoing trigger reason
jobs:
- job: A
steps:
- script: echo 'Build reason::::' $(Build.Reason)
displayName: The build reason
# ... some more stages here triggered by PullRequests....
# ... some more stages here triggered by push (CI)....
The pr on Github looks like this:
The problem
However, the pipeline is not triggered, when the push triggers work just fine.
I have read in the docs but I can't see why this does not work.
The pipeline is working perfectly fine when I am triggering it through git push. However, when I try to trigger it with PR's from Github, nothing happens. In the code above, I tried turning off push triggers, and allow for all pr's to trigger the pipeline. Still nothing.
I do not want to delete the pipeline yet and create a new one.
Update
I updated the yaml file to work as suggested underneath. Since the pipeline actually runs through a push command now, the rest of the details of the yaml file are not relevant and are left out.
Other things I have tried
Opening new PR on Github
Closing/Reopening PR on Github
Making change to existing PR on Github
-> Still no triggering of pipeline.
You have a mistake in your pipeline. It should be like this:
trigger: none # turned off for push
pr:
- feature/automated-testing
steps:
- script: echo "PIPELINE IS TRIGGERED FROM PR"
Please change this
- stage:
- script: echo "PIPELINE IS TRIGGERED FROM PR"
to
- stage:
jobs:
- job:
steps:
- script: echo "PIPELINE IS TRIGGERED FROM PR"
EDIT
I used your pipeline
trigger: none # I turned this off for to stop push triggers (but they work fine)
pr:
branches:
include:
- "*" # This does not trigger the pipeline
stages:
- stage: EchoTriggerStage
displayName: Echoing trigger reason
jobs:
- job: A
steps:
- script: echo 'Build reason::::' $(Build.Reason)
displayName: The build reason
# ... some more stages here triggered by PullRequests....
# ... some more stages here triggered by push (CI)....
and all seems to be working.
Here is PR and here build for it
I didn't do that but you can try to enforce this via branch policy. TO do that please go to repo settings and then as follow:
The solution was to go to Azure Pipelines -> Edit pipeline -> Triggers -> Enable PR validation.
You can follow below steps to troubleshooting your pipeline.
1, First you need to make sure a pipeline was created from the yaml file on azure devops Portal. See example in this tutorial.
2, Below part of your yaml file is incorrect. - script task should be under steps section.
Change:
stages:
- stage:
- script: echo "PIPELINE IS TRIGGERED FROM PR"
To:
stages:
- stage:
jobs:
- job:
step:
- script: echo "PIPELINE IS TRIGGERED FROM PR"
3, I saw you used template in your yaml file. Please make sure the template yaml files are in correct format. For example:
the dockerbuild-dashboard-client.yml template of yours is a step template. You need to make sure its contents is like below:
parameters:
...
steps:
- script: echo "task 1"
- script: echo "task 2"
And webapprelease-dashboard-dev-client.yml of yours is a job template. Its contents should be like below:
parameters:
name: ''
pool: ''
sign: false
jobs:
- job: ${{ parameters.name }}
pool: ${{ parameters.pool }}
steps:
- script: npm install
- script: npm test
- ${{ if eq(parameters.sign, 'true') }}:
- script: sign
4, After the pipeline was created on azure devops Portal. You can manually run this pipeline to make sure there is no error in the yaml file and the pipeline can be successfully executed.
5, If All above are checked, but the PR trigger still is not working. You can try deleting the pipeline(created on the first step) created on Azure devops portal and recreated a new pipeline from the yaml file.

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

Resources