Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicated code in .gitlab-ci.yml's script section

Tags:

docker

gitlab

Is there a way within gitlab ci to create something like a function? I would like to don't have the duplicated code in the stages or should I use a external script for such a task?

I had a look into the gitlab template but with a template I can only use a job in the template once.

stages:
  - build-artefakts
  - build-image

variables:
  CI_REGISTRY_NAMESPACE: 'main'
  CI_BASE_IMAGE: '...'

  1_GROUP_ID: '...'
  1_ARTIFACT_ID: '...'
  1_CLASSIFIER: '...'
  1_JAR_NAME: '...'
  1_IMAGE_NAME: '...'

  2_GROUP_ID: '...'
  2_ARTIFACT_ID: '...'
  2_CLASSIFIER: '...'
  2_JAR_NAME: '...'
  2_IMAGE_NAME: '...'


deploy-maven:
  image: maven:3.6-jdk-11
  stage: build-artefakts
  script:
    - 'mvn deploy -f pom.xml -s ~config/maven/ci_settings.xml -P prod'
  only:
    - master
  artifacts:
    paths:
      - ./target
    expire_in: 1 weeks

deploy-docker-1:
  image: $CI_BASE_IMAGE
  stage: build-image
  variables:
    ARTIFACT_ID: $1_ARTIFACT_ID
    JAR_NAME: $1_JAR_NAME
    IMAGE_NAME: $1_IMAGE_NAME

  before_script:
    - 'docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN  $CI_REGISTRY'
  script:
    - 'cd docker'
    - 'mv ../target/$ARTIFACT_ID/*.jar $JAR_NAME'
    - 'docker build . -t $IMAGE_NAME:$PROJECT_VERSION --build-arg JAR_FILE=$JAR_NAME'
    - 'docker tag $IMAGE_NAME:$PROJECT_VERSION $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$IMAGE_NAME:$PROJECT_VERSION'
    - 'docker push $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$IMAGE_NAME:$PROJECT_VERSION'

deploy-docker-2:
  image: $CI_BASE_IMAGE
  stage: build-image
  variables:
    ARTIFACT_ID: $2_ARTIFACT_ID
    JAR_NAME: $2_JAR_NAME
    IMAGE_NAME: $2_IMAGE_NAME

  before_script:
    - 'docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN  $CI_REGISTRY'
  script:
    - 'cd docker'
    - 'mv ../target/$ARTIFACT_ID/*.jar $JAR_NAME'
    - 'docker build . -t $IMAGE_NAME:$PROJECT_VERSION --build-arg JAR_FILE=$JAR_NAME'
    - 'docker tag $IMAGE_NAME:$PROJECT_VERSION $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$IMAGE_NAME:$PROJECT_VERSION'
    - 'docker push $CI_REGISTRY/$CI_REGISTRY_NAMESPACE/$IMAGE_NAME:$PROJECT_VERSION'

like image 264
Meeresgott Avatar asked Apr 27 '26 05:04

Meeresgott


1 Answers

You can use YAML-Anchors in "script"-sections:

.some-script: &some-script
  - echo "executed script"

.some-other-script: &some-other-script
  - echo "executed other script"

first_job:
  script:
    - *some-script

second_job:
  script:
    - *some-other-script

Since GitLab 13.9 you can also use the custom YAML tag !reference:

.some-script:
  script:
    - echo "executed script"

.some-other-script:
  script:
    - echo "executed other script"

first_job:
  script:
    - !reference [.some-script, script]

second_job:
  script:
    - !reference [.some-other-script, script]

The difference between the two approaches is that YAML anchors cannot be used across included YAML files. Referenced tags, on the other hand, can be used across included YAML files.

like image 192
Jakob Liskow Avatar answered Apr 29 '26 00:04

Jakob Liskow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!