Recreate Gitlab CI before_script / extends functionality to avoid duplicate code

  Kiến thức lập trình

With Gitlab CI, you can use before_script to run a script before every script command of a yaml file (cf. example 1 below).

A similar functionality can be achieved with extends and a hidden job, making it possible to also reuse other parameters (e.g. image, cf. example 2).

In this way, duplicate code can be avoided by defining content once and referencing it multiple times in different sections of the yaml file.

How can this be replicated with Azure Pipelines yaml files?
There seems to be no equivalent of before_script and extends does not feature the Gitlab CI yaml functionality.

Gitlab CI yaml file, example 1:

before_script:
    - echo "this runs before every script command"

stages:
    - test

test-code:
    stage: test
    # before_script will be run before this script command
    script:
        - echo "run script"

Gitlab CI yaml file, example 2:

.hidden_job
    image: python:3.12-bookworm
    - script:
        - echo "reference this to avoid duplicate yaml code"

stages:
    - test

test-code:
    stage: test
    extends: .hidden_job
    script:
        - echo "run script"

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT