how to cancel gitlab previous pipeline when new pipeline job is triggered - gitlab

I'm looking for mechanism to cancel previous pipeline job if an existing pipeline job is active and running and upgrade in a particular environment in GitLab. Thanks
When two services are modified one after another in quick succession, the helm job fails because it doesn't run two upgrades simultaneously.

Auto-cancel redundant pipelines
You can set pending or running pipelines to cancel automatically when a new pipeline runs on the same branch. You can enable this in the project settings:
On the top bar, select Main menu > Projects and find your project.
On the left sidebar, select Settings > CI/CD.
Expand General Pipelines.
Select the Auto-cancel redundant pipelines checkbox.
Select Save changes.
Use the interruptible keyword to indicate if a running job can be cancelled before it completes.
https://docs.gitlab.com/ee/ci/pipelines/settings.html#auto-cancel-redundant-pipelines
stages:
- stage1
- stage2
- stage3
step-1:
stage: stage1
script:
- echo "Can be canceled."
interruptible: true
step-2:
stage: stage2
script:
- echo "Can not be canceled."
step-3:
stage: stage3
script:
- echo "Because step-2 can not be canceled, this step can never be canceled, even though it's set as interruptible."
interruptible: true

Related

Gitlab pipeline - run the script with a rule

I want to send a slack notification as soon as trigger the pipeline build manually.
Steps I am following:
Go to your project’s CI/CD > Pipelines and select Run pipeline.
Choose the branch you want to run the pipeline for.
Input the variable and its value in the UI.
Click on the Run pipeline button
From the documentation I see:
How the pipeline was triggered. Can be push, web, schedule, api, external, chat, webide, merge_request_event, external_pull_request_event, parent_pipeline, trigger, or pipeline. For a description of each value, see Common if clauses for rules, which uses this variable to control when jobs run.
In this init stage I want to execute a script only when I run the pipeline using the above steps
init:
stage: init
environment: $DEPLOY_ENVIRONMENT
script:
- bundle exec fastlane slack_build_start_summary
rules:
- if: $CI_PIPELINE_SOURCE == "trigger" // also tried with "pipeline"
when: always
But I can't see this stage in the Gitlab pipeline stages.
I want to know which CI_PIPELINE_SOURCE ("trigger" or "pipeline" or something else?) I need to check when I trigger the pipeline manually from Gitlab using the above steps.
I got the answer here from the documentation
web -> For pipelines created by using Run pipeline button in the GitLab UI, from the project’s CI/CD > Pipelines section.
So I need to use:
- if: $CI_PIPELINE_SOURCE == "web"

how to add manual intervention in build pipeline

i have a build pipeline i want to add mannual intervention in the pipeline and when that manual intervention is done the user can click on resume the pipeline but i want to do this in build pipeline.
i checked in this https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/manual-validation?view=azure-devops&tabs=yaml but it looks like we need to do using stage pipeline in release pipeline. but i want this to happen in build pipeline
We seem have not such task for build pipeline, especially classic build pipeline.
If you want to add manual intervention in a build pipeline, you can try to set up this pipeline with YAML and add this run the Manual Validation task in an agentless job.
However, it might not be possible if you want to pause a running agent job in the build pipeline. Maybe you can try run the pipeline on a self-hosted agent with interactive mode.
[UPDATE]
You should use the Manual Validation task in an agentless job in the YAML pipeline, not in the classic build pipeline.
To set up an agentless job in the YAML pipeline, you need to set the value of 'pool' key to ne 'server'.
jobs:
- job: agentless-job
pool: server
To view more details, you can see "Server jobs".
To add a manual validation task in pipeline without creating a agent-less job you will need to define pool: server on the top of your steps. as below.
I got this from by referring to canary deployments but have tested the same works for rolling deployment and probably other steps introduced in the pipeline.
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops
pool: server
steps:
- task: ManualValidation#0
timeoutInMinutes: 1440 # task times out in 1 day
inputs:
notifyUsers: |
youremailadress#hotmail.com
instructions: 'Please validate the build configuration and resume'
onTimeout: 'reject'

Will Gitlab trigger all jobs that listen on a branch change when I run pipeline manually

I have a pipeline that contains several jobs triggered by changes on a branch:
deployDev:
only:
- dev
script:
- ...
deployProd:
only:
- master
script:
- ...
If I now hit the "Run pipeline" button in the GitLab UI, will it trigger these jobs or will it only trigger the jobs that specify when:manual ?
And how can I make sure that deployDev and deployProd do not run when I run a manual deploy?
I have checked here: https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic, but I'm unsure.
It'll run all jobs corresponding to the branch and will wait for manual action for manual jobs.
So your deploydev will run against your development pipeline and other job will run only in production pipeline
I'm a bit confused what you are trying to achieve from your comment as well, however...
If I now hit the "Run pipeline" button in the GitLab UI, will it trigger these jobs or will it only trigger the jobs that specify when:manual ?
As #Prashanna has said, It'll run all jobs corresponding to the branch and will wait for manual action for manual jobs.
If you do not want deployDev and deployProd to appear in the Pipeline when pressing the Run Pipeline button, you can use:
only:
- master
except:
- web
from the link you showed https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic: web -
For pipelines created by using Run pipeline button in the GitLab UI, from the project’s CI/CD > Pipelines section.
The above job would then only appear in the Pipeline, when it is the master branch, and when it has not been triggered by the Run Pipeline button.

How to track what triggered my pipeline on Azure DevOps?

I am using YML file to configure my azure pipelines. My pipeline can either be triggered by new commits to development branch and/or scheduled cron. How can I track which triggered the pipeline? I wanted to do some task based on what triggered by pipeline, whether from a new commit or a cron. It's a small add-on task, so I am avoiding writing a new pipeline to separate the tasks. Thanks in advance!
Here is my code sample:
trigger:
- development
schedules:
- cron: '0 0 * * *' # will run midnight every day
displayName: 'Midnight tests' (UTC - 7:00)'
branches:
include:
- development
always: true
stages:
### do some work
We could add task bash and enter script printenv to print all pipeline env variable. Then we could see the variable BUILD_REASON, we can track this variable to check how the build pipeline is triggered.
Schedule is trigger from cron, IndividualCI is trigger from commit push. You could check the pic below.

Gitlab-CI : triggering a stage based on the Status of the previous stage

enter image description here
Hello - I'm using Gitlab 10.x Enterprise version.
This is in the context of a Production deployment to an environment having eight servers. The requirement is to deploy to one server first, and then after couple of days do a deployment to the remaining servers with the press of just one button.
Pipeline Stages:
Release-Tag-Creation -> Production-OneServer-Deployment -> OneButtonPush -> DeploytoAllServers
Question:
How can I tie the dependency between the stages "OneButtonPush" and "DeploytoAllServers"?. "DeploytoAllServers" stage should be kicked-off only when the Status of the job in the "OneButtonPush" stage is successful. "DeploytoAllServers" stage will have parallel jobs to deploy to each server.
I made few attempts based on the Gitlab CI documentation, but wasn't successful. Also, can the concept of Rolling deployment used in the context of GitLab-CI.
Thanks!
In Gitlab CI, the stages are run one after another. Each stage can have multiple jobs which run in parallel.
As per your use case, you'll need to have different stages each for Release-Tag-Creation, Production-OneServer-Deployment, OneButtonPush and DeploytoAllServers. You can have manual triggers for particular jobs (OneButtonPush in your case) by specifying when: manual in the job definition.
By default, if there is a job awaiting manual trigger, jobs from further stages will start executing considering the job with manual trigger as successful. To change this behavior one will need to use allow_failure: false. Mode details on allow_failure over here
If you want to run the Stage only after then specific one get successful you can use the needs
Example
image: maven:latest
stages:
- deploy_dev_lambda
- test_dev_lambda
deploy_dev_lambda:
stage: deploy_dev_lambda
image: maven:latest
script:
- ./deployLambda
environment: dev
variables:
TARGET_ENV: "dev"
TAG_NAME: main-$CI_COMMIT_SHORT_SHA
test_dev_lambda:
stage: test_dev_lambda
needs:
- deploy_dev_lambda
script:
- ./testLambda
So test_dev_lambda only run after the deploy_dev_lambda stage and if stage deploy_dev_lambda fails test_dev_lambda wont get executed.

Resources