Running gitlab pipelines before a merge request is published - gitlab

I have a project that runs a pipeline with a few jobs/stages in it. I have the project set up to create merge requests when I push to another branch. Those jobs kick off after the merge request is published. Is there any way to start the pipeline before the merge request is published?
EDIT
Here is what I ended up adding based on the accepted answer.
workflow:
rules:
- when: always

If you want to use the basic configuration, I'd probably take a look at workflow:rules in Gitlab's pipeline configuration reference:
The top-level workflow: keyword determines whether or not a pipeline is created. It accepts a single rules: keyword that is similar to rules: defined in jobs. Use it to define what can trigger a new pipeline.
Here you could use the merge_request_event-rule:
For pipelines created when a merge request is created or updated. Required to enable merge request pipelines, merged results pipelines, and merge trains.
Gitlab also offers Pipelines for merge requests, which could be a better fit for your specific use-case. Keep in mind that not every user can trigger a pipeline if the relevant branch is protected.

Related

Why bitbucket pipeline merges pull request before it runs?

In their documentation regarding pull request pipelines, bitbucket says:
Pull requests:
a special pipeline that only runs on pull requests initiated from within your repository. It merges the destination branch into your working branch before it runs. If the merge fails, the pipeline stops.
So I'm wondering, why merging before running the pipeline? Why not just running against the coming branch without merging?
Could the reason be detecting merge conflicts early on in the pipeline before the real merge?
If you want to run a pipeline against the coming branch, this is very doable by using Branch workflows. PR merge trigger is just a slightly different idea, as the result of a PR merge is not necessarily the same as the coming branch. For example, merge conflicts can be introduced, which will make your pipeline fail.
There's one thing that documentation is not quite clear about, so I'll clarify it: all this pre-pipeline merging only occurs inside your build environment. Git history of your repository is absolutely safe, and Bitbucket Pipelines won't introduce any changes to it on your behalf.
Finally, you can run a PR merge pipeline manually from the Pipelines UI, without actually merging a PR (see the same link). This way, you can make sure that the merge result build is passing without actually doing a merge.

Azure Devops pipeline triggering twice with Build Validation

I have created a pipeline in my repository which is used to validate code by executing unit tests for code that is being pushed to features/* branches. The same pipeline is used as Build validation pipeline set as Branch Policy on the develop branch to validate incoming PRs. This is the trigger of the pipeline.
# pipeline.yml
trigger:
batch: false
branches:
include:
- features/*
However we have come across the following condition: Given an open PR from refs/heads/features/azure-pipelines -> refs/heads/develop we push a commit on the features/azure-pipelines branch.
This causes the pipeline to trigger twice. To my understanding one of the runs is due to the trigger of the pipeline (The one marked as Individual CI on the screenshot) and the second run is due to branch policy trying to validate code being pushed onto the open PR to develop. (The PR Automated)
Is there any way to disable one of the executions since it's essentially a duplicate? I was maybe looking for a way to retrieve open PRs and abort execution of a pipeline for Individual CI if there is an open PR for a branch but I am not sure that's the best way around that and I am looking for options.
You can set
trigger: none
This way only the branch policy will trigger the pipeline.
Is there any way to disable one of the executions since it's essentially a duplicate?
As we know, we could not disable the Build validation pipeline set as Branch Policy on the develop branch to validate incoming PRs unless we cancel the Build validation.
For your situation, you could try to include [skip ci] in the commit message or description of the HEAD commit to make the Azure Pipelines skip running CI when you plan to merge the features branch to the develop branch.
You could check the document Skipping CI for individual commits for some more details.
Here it depends if they does the same. You can have conditional checks in the pipeline which does a different things for PR and CI runs. However, I'm pretty sure that this is not possible, because one is defined on the YAML and the second on the Azure DevOps portal. So even if you disnle PR trigger here in YAML, a branch policy still runs a PR. And you can specify antyhing in YAML to block branch policy.

Is there a way to trigger different commit builds on merge requests based on different file changes in GitlabCI?

We are migrating from tfs to gitlab. Ours is a huge repository with multiple technology stack. And each solution has its own unit tests associated. We are planning to allow merge requests to successfully complete only upon successful build of that particular solution with the changes associated with merge request. As we have multiple solutions, is there a way to tell Gitlab ci yaml to trigger only a particular job if the merge request is associated with specific file changes.
eg..
If I have changes from solution A in the merge request, the pipeline should trigger job A
Currently, we have yaml snippet similar to this. But its getting triggered on all the merge requests while we want it to trigger only for merge requests for files under /docs/UI
rules:
- if: $CI_MERGE_REQUEST_IID
- changes:
- ./docs/UI
Our Gitlab version: GitLab Enterprise Edition 12.6.6-ee
Any leads would be appreciated. Thanks!!
You are using the keyword rules. Rules are evaluated in order until the first match. When matched, the job is either included or excluded from the pipeline, depending on the configuration.
Use the keyword only instead. For example like this:
only:
refs:
- merge_requests
changes:
- docs/UI/**/*

Trigger Azure Pipeline on Merge

I have a use case where I have two pipelines configured. One for running tests which are triggered by a Pull Request and another pipeline that is handling deployment. I am using Bitbucket cloud in Azure.
Pipeline A - Run through tests on PR.
Pipeline B - Deploy upon successful merge (approved pull request).
My current setup is to trigger pipeline A when a pull request is submitted. This is no problem and works as intended. The issue I am running into is with Pipeline B. It is triggered regardless of whether the PR is approved or even merged. I know that expected behavior for the CI trigger is to fire off with any action on the branch. The PR trigger also doesn't fit my needs though as it would be redundant with Pipeline A.
What I would like to happen is since Pipeline A fires off on PR, a code reviewer will Approve and Merge the PR and only if the PR ends up getting merged into the Release branch is Pipeline B triggered. Is this possible?
Developer completes code.
Developer submits PR against a Release1.0 branch.
Pipeline A triggers. Tests pass or fail.
Code reviewer approves and merges PR into Release1.0 branch.
Pipeline B triggers and deploys.
UPDATE: For those who bump into the same issue it's actually quite a simple fix. Simply setting the trigger...
pr: none
in Pipeline B will do the trick.
To be clear, this solution is specific to Bitbucket Cloud and GitHub repositories only. The 'pr' syntax in YAML pipelines doesn't exist for Azure Repositories (use branch policy instead).

What is the difference between Continuous deployment trigger and Pull request trigger?

Azure DevOps Release pipeline artifact settings has 2 options:
Continuous deployment trigger - Enabling the trigger will create a new release every time a new build is available.
Pull request trigger - Enabling this will create a release every time a selected artifact is available as part of a pull request workflow
I am trying to understand what is the difference between these options with respect to the highlighted parts and whether the build validation policy causes release to trigger with both options, if so then why do we have pull request trigger?
In my opinion, these two triggers have different working scope.
Assuming we set one CI build as release artifact, according to my test:
1.Continuous deployment trigger:
Whenever we have a new version of the Build, it triggers the release. It means that no matter the build pipeline is triggered by manual run, CI trigger or build validation in branch policy, the release is triggered when there's one newer build.
2.Pull request trigger:
It has a smaller scope, it will be trigger by the build pipeline which is trigger by build validation in branch policy.
(Which is triggered by PR, so if we create new PR=>It triggers PR build=>It triggers PR release)
Feel free to correct me if I misunderstand anything.
Update1:
Here's one pic about my two tests:
Release-8 is triggered by my manual running Build pipeline with only CD triggered enabled. And Release-7 is triggered by PR build with only PR triggered enabled. (I only enable the Pull Request for deployment in Stage 1)
Apart from working scope, these two triggers also have a little difference here. The Pull Request Deployment in stage for now is only for PR trigger in Artifact.
This explains that very well (taken from documentation):
Pull requests (PRs) provide an effective way to have code reviewed
before it is merged to the codebase. However, certain issues can be
tricky to find until the code is built and deployed to an environment.
Before the introduction of pull request release triggers, when a PR
was raised, you could trigger a build, but not a deployment. Pull
request triggers enable you to create pull request releases that
deploy your PR code or PR builds to detect deployment issues before
the code changes are merged. You can use pull request triggers with
code hosted on Azure Repos or GitHub.
New build basically means that your pipeline was executed.
Creating pull request trigger you need to define an artifact which will be later deployed. For this kind of trigger Azure Devops runs your pipeline and produce artifact according to your pipeline/build definition and later use this artifact for deployment.
Both trigger are similar, the difference is when your code will be deployed before or after being merged into main branch.
PR trigger is part of CI - applies when your merge your branch to master
CD trigger - applies only to the master branch

Resources