Gitlab Pipeline Run Python Script

Project pipelines¶

You can trigger a pipeline using token authentication instead of user authentication. To do so create an anonymous Gitlab instance and use lazy objects to get the associated project: gl = gitlab.Gitlab(URL) # no authentication project = gl.projects.get(projectid, lazy=True) # no API call project.triggerpipeline('master', triggertoken) Reference: https://docs.gitlab.com/ee/ci/triggers/#trigger-token. Sep 27, 2021 With this gitlab-ci.yml file, without any rule, all the jobs are displayed and run. Image: 'python:3.8' stages: - testwithoutonlypolicy - testwithonlypolicy testwithoutonlypolicy: stage: testwithoutonlypolicy when: manual script: - echo 'Yay, I am in the pipeline' testwithonlypolicy: stage: testwithonlypolicy script: - echo 'I. Jan 19, 2019 Writing a deploy script with Python and fabric. Jan 19, 2019 3 min read. Some weeks ago I tried to set up a continuous deployment pipeline for a python project with Gitlab CI and I. Here is my./ci-cdscripts/getversion.py: import os refName = os.environ.get('CICOMMITREFNAME') piplineID = os.environ.get('CIPIPELINEID') relVersion = refName + '.0.' + piplineID version = relVersion.replace('rel.' , ') print('current version is', version) python output in pipeline log.

A pipeline is a group of jobs executed by GitLab CI.

Reference¶

  • v4 API:

    • gitlab.v4.objects.Project.pipelines

  • GitLab API: https://docs.gitlab.com/ce/api/pipelines.html

Gitlab Pipeline Run Python Script Tutorial

Examples¶

List pipelines for a project:

Get a pipeline for a project:

Get variables of a pipeline:

Create a pipeline for a particular reference with custom variables:

Retry the failed builds for a pipeline:

Cancel builds in a pipeline:

Delete a pipeline:

Triggers¶

Triggers provide a way to interact with the GitLab CI. Using a trigger a useror an application can run a new build/job for a specific commit.

Reference¶

  • v4 API:

    • gitlab.v4.objects.Project.triggers

  • GitLab API: https://docs.gitlab.com/ce/api/pipeline_triggers.html

Examples¶

List triggers:

Gitlab Pipeline Run Python Script

Get a trigger:

Create a trigger:

Remove a trigger:

Full example with wait for finish:

You can trigger a pipeline using token authentication instead of userauthentication. To do so create an anonymous Gitlab instance and use lazyobjects to get the associated project:

Reference: https://docs.gitlab.com/ee/ci/triggers/#trigger-token

Pipeline schedule¶

You can schedule pipeline runs using a cron-like syntax. Variables can beassociated with the scheduled pipelines.

Reference¶

  • v4 API

    • gitlab.v4.objects.Project.pipelineschedules

    • gitlab.v4.objects.Project.pipelineschedules

  • GitLab API: https://docs.gitlab.com/ce/api/pipeline_schedules.html

Examples¶

List pipeline schedules:

Get a single schedule:

Create a new schedule:

Update a schedule:

Trigger a pipeline schedule immediately:

Delete a schedule:

List schedule variables:

Create a schedule variable:

Edit a schedule variable:

Delete a schedule variable:

Jobs¶

Jobs are associated to projects, pipelines and commits. They provideinformation on the jobs that have been run, and methods to manipulatethem.

Reference¶

  • v4 API

    • gitlab.v4.objects.Project.jobs

  • GitLab API: https://docs.gitlab.com/ce/api/jobs.html

Examples¶

Jobs are usually automatically triggered, but you can explicitly trigger a newjob:

List jobs for the project:

Get a single job:

List the jobs of a pipeline:

Note

Job methods (play, cancel, and so on) are not available onProjectPipelineJob objects. To use these methods create a ProjectJobobject:

Get the artifacts of a job:

Get the artifacts of a job by its name from the latest successful pipeline ofa branch or tag:

project.artifacts(ref_name=’master’, job=’build’)

Warning

Artifacts are entirely stored in memory in this example.

You can download artifacts as a stream. Provide a callable to handle thestream:

Gitlab pipeline variable

You can also directly stream the output into a file, and unzip it afterwards:

Get a single artifact file:

Get a single artifact file by branch and job:

Mark a job artifact as kept when expiration is set:

Delete the artifacts of a job:

Get a job trace:

Warning

Traces are entirely stored in memory unless you use the streaming feature.See the artifacts example.

Cancel/retry a job:

Play (trigger) a job:

Erase a job (artifacts and trace):

Pipeline bridges¶

Get a list of bridge jobs (including child pipelines) for a pipeline.

Reference¶

  • v4 API

    • gitlab.v4.objects.ProjectPipeline.bridges

  • GitLab API: https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-bridges

Building A CI/CD Pipeline

Examples¶

List bridges for the pipeline:

Pipeline test report¶

Get a pipeline’s complete test report.

Reference¶

  • v4 API

    • gitlab.v4.objects.ProjectPipeline.test_report

  • GitLab API: https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report

Examples¶

Get the test report for a pipeline:

Hello everyone,

my name is Roman and I’m trying to set up CI pipeline. This pipeline should consist of 4 stages. First 3 stages (build, test and package) should be run on each push to the git and the last one (post_merge) should be run only when project maintainer is merged merge request into the master branch.

On the “package” stage I create a docker-image with my application and push this image into the local docker-registry. This docker image has tag equal to the name of current branch in the git. Testers can deploy this image into the testing environment and test this feature manually. When this branch is merged into the master this docker-image is not necessary anymore and I want to remove it on the “post_merge” stage.

It’s possible to remove tag by using Gitlab API something like this:

But I can’t find the way when to make this API call.

To implement this approach I created following config in .gitlab-ci.yml (I skipped some not important parts, such as variables definition):

First 3 steps are working as I want, but the last one is not working. In this step variable $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME is not available and it’s empty. I also tried to replace it with $CI_COMMIT_REF_NAME, but in this context this variable has value “master”, but I need to have the name of source branch here. Also I tried to replace cleanup job with something like this:

But in this case this stage starts as independent pipeline (which is consist of the only one job) and this stage doesn’t have access to artifacts of build stage. Also this pipeline runs not only on the merge request merged, but on merge request is created too.

Does any one know how to set up .gitlab-ci.yml to run stage only when some branch is merged into the master and get the name of source branch?