Updated on 2024-06-06 GMT+08:00

Pipeline Contexts

Contexts are a way to access information about pipeline runs, sources, variables, and jobs. Each context is an object that contains various attributes.

  • Reference format
    ${{ <context>.<attribute_name> }}
  • Pipeline contexts

    Context Name

    Type

    Description

    pipeline

    object

    Information about the pipeline run.

    sources

    object

    Information about the pipeline sources in each pipeline run.

    env

    object

    Information about the custom parameters in each pipeline run.

    jobs

    object

    Information about jobs that have reached the final states in each pipeline run.

Scenarios

Most contexts can be used in any job and step of a pipeline.

  • You can use contexts to specify the execution condition of a job.
    The following example shows that a job runs only when the running branch of the specified code source is master.
    ${{ sources.my_repo.target_branch == 'master' }}
  • You can use contexts when configuring parameters to query information.

    The following expression shows how to obtain all pipeline run information.
    ${{ pipeline }}
    The following expression shows how to obtain the triggering mode of a pipeline.
    ${{ pipeline.trigger_type }}

pipeline Context

The pipeline context contains pipeline run information.

Name

Type

Description

pipeline

object

Information about the pipeline run. This object contains the following attributes: project_id, pipeline_id, run_number, timestamp, trigger_type, and run_id.

pipeline.project_id

string

ID of the project to which the current pipeline belongs. This string is the same as the predefined parameter PROJECT_ID.

pipeline.pipeline_id

string

Current pipeline ID. This string is the same as the predefined parameter PIPELINE_ID.

pipeline.run_number

string

Pipeline execution number. This string is the same as the predefined parameter PIPELINE_NUMBER.

pipeline.timestamp

string

Pipeline execution timestamp. This string is the same as the predefined parameter TIMESTAMP. For example, 20211222124301.

pipeline.trigger_type

string

Pipeline triggering type. This string is the same as the predefined parameter PIPELINE_TRIGGER_TYPE.

pipeline.run_id

string

Pipeline execution ID. This string is the same as the predefined parameter PIPELINE_RUN_ID.

  • Content example

    The following example shows the pipeline context information contained in a manually executed pipeline.

    {
        "project_id": "6428c2e2b4b64affa14ec80896695c49",
        "pipeline_id": "f9981060660249a3856f46c2c402f244",
        "run_number": "168",
        "timestamp": "20231016000004",
        "trigger_type": "Manual",
        "run_id": "c2f507f93510459190b543e47f6c9bec"
    }
  • Usage example

    To obtain the triggering mode of the current pipeline, you can use the following syntax:

    ${{ pipeline.trigger_type }}

sources Context

The sources context contains information about the pipeline sources.

Name

Type

Description

sources

object

Information about the pipeline sources in each pipeline run. This object contains the following attributes: alias, repo_name, commit_id, commit_id_short, commit_message, repo_url, repo_type, repo_name, ssh_repo_url, tag, merge_id, source_branch, and target_branch.

sources.<alias>

object

Information about the pipeline source which has an alias.

sources.<repo_name>

object

Information about the pipeline source which does not have an alias but only a repository name. It contains the same information as that in sources.<alias>

sources.<alias>.commit_id

string

The last commit ID before execution. This string is the same as the predefined parameter COMMIT_ID.

sources.<alias>.commit_id_short

string

The last commit short ID before execution. This string is the same as the predefined parameter COMMIT_ID_SHORT.

sources.<alias>.commit_message

string

The last commit message before execution.

sources.<alias>.repo_url

string

Code repository address (HTTPS). This string is the same as the predefined parameter REPO_URL.

sources.<alias>.repo_type

string

Type of the code repository.

sources.<alias>.repo_name

string

Name of the code repository.

sources.<alias>.ssh_repo_url

string

Code repository address (SSH).

sources.<alias>.tag

string

Tag name when the tag is triggered.

sources.<alias>.merge_id

string

Merge request ID when the merge request is triggered.

sources.<alias>.source_branch

string

Source branch name when the merge request is triggered.

sources.<alias>.target_branch

string

If the merge request is triggered, this string indicates the name of the target branch. Otherwise, this string indicates the name of the running branch.

  • Content example

    The following example shows the sources context information contained in a manually executed pipeline with a single code source. The alias of pipeline source is my_repo.

    {
        "my_repo": {
            "commit_id": "dedb73bb9abfdaab7d810f2616bae9d2b6632ecc",
            "commit_id_short": "dedb73bb",
            "commit_message": "maven0529 update pipeline0615.yml",
            "repo_url": "https://example.com/clsyz00001/maven0529.git",
            "repo_type": "codehub",
            "repo_name": "maven0529",
            "ssh_repo_url": "git@example.com:clsyz00001/maven0529.git",
            "target_branch": "master"
        }
    }
  • Usage example

    To obtain the running branch of the pipeline, you can use the following syntax:

    ${{ sources.my_repo.target_branch }}

env Context

The env context contains information about the custom parameters in each pipeline run.

Name

Type

Description

name

string

Name of a custom parameter.

value

string

Value of a custom parameter.

  • Content example

    The following example shows the env context information in a run, which includes two custom parameters.

    {
        "var_1": "val1",
        "var_2": "val2"
    }
  • Usage example

    To obtain the value of the custom parameter var_1, you can use the following syntax:

    ${{ env.var_1 }}

jobs Context

The jobs context contains information about jobs that have reached the final states in each pipeline run.

Name

Type

Description

jobs

object

Information about jobs in a pipeline. This object contains the following attributes: job_id, status, outputs, output_name, metrics, and metric_name.

jobs.<job_id>

object

Information about the job with a specified ID.

jobs.<job_id>.status

string

Execution result of a job. The value can be INIT, QUEUED, RUNNING, CANCELED, COMPLETED, FAILED, PAUSED, IGNORED, or UNSELECTED.

jobs.<job_id>.outputs

object

General output of an executed job.

jobs.<job_id>.outputs.<output_name>

string

The value of output_name of an executed job.

jobs.<job_id>.metrics

object

The running metric of a job.

jobs.<job_id>.metrics.<metric_name>

string

The value of metric_name of an executed job.

  • Content example

    The following example shows the jobs context information in a run. There are two successfully executed jobs. The output of the check_job job is two metrics, and the output of the demo_job job is two general outputs.

    {
        "check_job": {
            "status": "COMPLETED",
            "metrics": {
                "critical": "0",
                "major": "0"
            }
        },
        "demo_job": {
            "status": "COMPLETED",
            "outputs": {
                "output1": "val1",
                "output2": "val2"
            }
        }
    }
  • Usage example

    To obtain the value of output1 of demo_job, you can use the following syntax:

    ${{ jobs.demo_job.outputs.output1 }}