PR trigger for Azure Pipeline building the base PR branch - azure

I have a github repository called apprepo . In that there is a branch dev2 which i use it as the base branch.
I created another branch out of that dev3 made some changes to it. In the azure-pr-pipeline.yml the contents are as follows:
pr:
branches:
include:
- dev2 // include when PR is raised for merging to dev2 branch
stages:
- stage: PR
displayName: prBuild
jobs:
- job: DowndSecureFile
displayName: Build
steps:
- task: NodeTool#0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- script: |
echo "workingdirectory " $(System.DefaultWorkingDirectory)
npm install
npm ci
npm audit fix
npm run build
Now when i make changes in dev3 and raise PR for merging to dev2,build gets triggered but the dev2 branch (base) is getting built instead of the dev3 branch where the changes are committed.
Can anyone please tell me, how to checkout the dev3 branch of the apprepo and only build that?

The PR build should run for the PR merge branch (refs/pull/{PR Number}/merge), neither the target branch (dev2) nor the source branch (dev3).
If there is build triggered for the branch which is not the PR merge branch, ensure you have disabled the CI trigger on this YAML pipeline.
For example:
trigger: none // Disable the CI trigger.
pr:
branches:
include:
- dev2
By this way, the pipeline will be only triggered by PR, not by the CI trigger from the target branch and the source branch.

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.

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.

Automatic code merging using gitlab-ci.yml pipeline

My goal is to create Gitlab yml file in order to automatically merge code from release to master branch which will be run on a schedule (once a week)
Here is my .gitlab-ci.yml:
image: "python:3.8"
stages:
- build
- test
- release
Build:
stage: build
script:
- <some code here>
Test:
stage: test
script:
- <some code here>
job:on-schedule:
stage: release
only:
- schedules
script:
- git config --global user.email myEmail#gmail.com
- git config --global user.name "myUserName"
- git checkout release
- git checkout master
- git merge release
But I'm getting the following error:
error: pathspec 'release' did not match any file(s) known to git
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1
. I'd appreciate it if you have any ideas on how to fix that.
By default, gitlab-ci does a shallow clone, and so the error you're seeing of:
error: pathspec 'release' did not match any file(s) known to git
is accurate because the release branch does not exist locally on the runner.
To resolve this, you can just update the lines to:
- git checkout origin/release
- git checkout origin/master
which will pull the latest branches from the repository directly instead.

Azure pipeline will not trigger from a branch with a single azure-pipelines.yml file

I've recently set up an Azure pipeline using the following azure-pipelines.yml file in the master branch:
trigger:
- master
- feature/*
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
I've also tried these two syntax examples for the trigger to no avail as well as removing the trigger completely.
trigger:
branches:
include:
- master
- feature/*
trigger:
branches:
include: ['*']
I've linked this to a GitHub repo where I created a branch called feature/cool_feature.
According to all of the docs I've read, I should be able to place the YAML file in the master branch which should start a build for both my branches. But all I can get this to do is start a build when a commit is made in the master branch.
The only want I could get the build to trigger is if I put another azure-pipelines.yml file inside of that branch.
Is this no longer the case or am I doing something wrong?
Apparently, you must have another copy of the master's azure-pipelines.yml file in the branch for this to work.

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:
- ...

Resources