Force skiping job in after_script

Hello

First I would like to thank you all because I found a lot of information in this forum and in the documentation :slight_smile: .

I have just a question about jobs definition on CI pipeline , here is my .gitlab-ci.yaml file :

job1:
  stage: jobs
  script:
    - echo "done"

job2:
  stage: jobs
  script:
    - echo "done"

job3:
  stage: jobs
  script:
    - echo "done"

I would like to skip two other jobs when one is run, to be sure that the user can run ONLY one job (ANY of the three) of the stage.

Do you know how I can achieve this ?
I though of using some directives in the after_script section, but didn’t find solution like this except canceling the whole pipeline (cancel its own ci job · Wiki · morph027 / gitlab-ci-helpers · GitLab), but this is not really what i would like .

I’m using GitLab Enterprise Edition [13.12.15-ee]

Thank you :slight_smile:

Hi @donuts_dealer

This is interesting… Is this pipeline going to run automatically, or will the user start it from the GitLab? If the latter, I would use variables for this:

stages:
  - jobs

job1:
  stage: jobs
  rules:
    - if: CI_PIPELINE_SOURCE == "web" && JOB1_ONLY
      when: always
    - when: never
  script:
    - echo "done"

job2:
  stage: jobs
  rules:
    - if: CI_PIPELINE_SOURCE == "web" && JOB2_ONLY
      when: always
    - when: never
  script:
    - echo "done"

job3:
  stage: jobs
  rules:
    - if: CI_PIPELINE_SOURCE == "web" && JOB3_ONLY
      when: always
    - when: never
  script:
    - echo "done"

If you are not using the web app, and your pipelines would be triggered by a Git push, or similar, you probably still want to use variables and rules, but you might need to think a bit about how those variables will be set.

HTH!

thanks a lot @snim2 !

I implemented a different way to solve my issue, here it is:
I created a bash script check.sh :

#!/bin/bash

# this script is used to check if another job for this pipeline has already been done

PIPELINE_ID=$1
PROJECT_ID=$2
JOB_ID=$3

if [[ ${PIPELINE_ID} =~ ^[0-9]+$ ]]; then 
	echo "PIPELINE_ID ok"
else
	echo "pattern not matched on PIPELINE_ID !"
	exit 1
fi
if [[ ${PROJECT_ID} =~ ^[0-9]+$ ]]; then 
	echo "PROJECT_ID ok"
else
	echo "pattern not matched on PROJECT_ID !"
	exit 1
fi
if [[ ${JOB_ID} =~ ^[0-9]+$ ]]; then 
	echo "JOB_ID ok"
else
	echo "pattern not matched on JOB_ID !"
	exit 1
fi
PIPELINE_JOBS_URL="https://gitlab.example.com/api/v4/projects/${PROJECT_ID}/pipelines/${PIPELINE_ID}/jobs?scope[]=success"

curl -s -o pipelineJobs.json -k --header "Private-Token: ${MY_GITLAB_TOKEN}" "${PIPELINE_JOBS_URL}"

if [[ -f pipelineJobs.json ]]; then
	CHECK_JOBS_DONE=$(cat pipelineJobs.json)
	if [[ "${CHECK_RELEASE_JOBS_DONE}" == "[]" ]]; then
		echo "no other jobs have been done"
		exit 0
	else
		echo "ERROR : another job has already been done"
		exit 1
	fi
else
	echo "ERROR on checking PIPELINE_JOBS_URL !"
	exit 1
fi

PIPELINE_JOBS_URL="https://gitlab.example.com/api/v4/projects/${PROJECT_ID}/pipelines/${PIPELINE_ID}/jobs?scope[]=running"

curl -s -o pipelineJobs.json -k --header "Private-Token: ${MY_GITLAB_TOKEN}" "${PIPELINE_JOBS_URL}"

if [[ -f pipelineJobs.json ]]; then
	CHECK_RELEASE_JOBS_DONE=$(cat pipelineJobs.json)
	if [[ "${CHECK_RELEASE_JOBS_DONE}" == "[]" ]]; then
		echo "no other job is running"
		exit 0
	else
		JOBS_COUNT=$(jq '.|length' pipelineJobs.json)
		if [[ "${JOBS_COUNT}" == "1" ]]; then
			CURRENT_JOB=$(jq -r '.[0] | .id' pipelineJobs.json)
			if [[ "${CURRENT_JOB}" == "${JOB_ID}" ]]
				echo "first job for this pipeline"
				exit 0
			else
				echo "ERROR : another job job is running on this pipeline"
				exit 1
			fi
		else
			echo "ERROR : more than one job is running on this pipeline"
			exit 1
		fi
		echo "ERROR : another job is running"
		exit 1
	fi
else
	echo "ERROR on checking PIPELINE_JOBS_URL !"
	exit 1
fi

then in my .gitlab-ci.yml file :

  before_script:
    - chmod +x ./ci/*
    - ./ci/check.sh "$CI_PIPELINE_ID" "$CI_PROJECT_ID" "${CI_JOB_ID}"

If you have any suggestion or recommendation to make it better, feel free to share :slight_smile:

1 Like