Skip to content

Trigger pipeline and download artifacts from triggered pipeline #1021

Closed

Description

@Letme

It should be a simple exercise, but in fact because of all the tokens it is not. There are few issues and I tried few variations, but to no avail. I am trying to trigger pipline and download artifacts of project A to pipeline that is running in project B and then use those artifacts to compile documentation.

Example I assembled based on documentation:

gl = gitlab.Gitlab('https://gitlab.melexis.com', job_token=os.environ['CI_JOB_TOKEN'])
project = gl.projects.get(project_id, lazy=True)
pipeline = project.trigger_pipeline('branchname', 'targetprojecttoken', variables={"VAR1": "True"})

# Start pipeline and wait for completion
while pipeline.finished_at is None:
    pipeline.refresh()
    time.sleep(1)

So, first problem was that I want to use CI_JOB_TOKEN to trigger the pipeline (not targetprojecttoken). This is something that can be done with curl, but here it does not work (tried with replacing targetprojecttoken with os.environ['CI_JOB_TOKEN'] but I get gitlab.exceptions.GitlabCreateError: 404: 404 Not Found).

Now for second more pressing issue here is that pipeline.refresh() returns gitlab.exceptions.GitlabGetError: 404: 404 Project Not Found although I confirmed that trigger_pipeline did trigger the pipeline.

Expected Behavior

Actual Behavior

  • trigger_pipeline works with targetprojecttoken instead of job_token
  • pipeline.refresh() returns gitlab.exceptions.GitlabGetError: 404: 404 Project Not Found

Specifications

  • python-gitlab version: 2.0.1
  • API version you are using (v3/v4): v4
  • Gitlab server version (or gitlab.com): private

Activity

  1. max-wittig commented on Feb 19, 2020

    @max-wittig
    Member

    Where do you get targetprojecttoken from?

    It really looks like the user is not able to access the project. Could you try to call pipeline refresh with curl? (Get pipeline status)

  2. Letme commented on Feb 19, 2020

    @Letme
    Author

    targetprojecttoken is token of the trigger, not of the user. But my main expectation is that CI_JOB_TOKEN should be enough?

  3. matthewcosgrove commented on Jan 2, 2021

    @matthewcosgrove

    Also have been trying to get the documented example referenced above working with mixed results.

    TOKEN=os.environ.get("CI_JOB_TOKEN")
    gl = gitlab.Gitlab(URL, job_token=TOKEN)
    project_id=1
    project = gl.projects.get(project_id, lazy=True)  # no API call
    pipeline = project.trigger_pipeline('master', TOKEN, variables={"TEST_METADATA": TEST_METADATA})
    while pipeline.finished_at is None:
      pipeline.refresh()
      time.sleep(1)
    

    Related observations using GitLab Enterprise Edition 13.5.5-ee

    1. project.trigger_pipeline does work for me with CI_JOB_TOKEN so the downstream job triggering works fine
    2. pipeline.refresh() returns gitlab.exceptions.GitlabGetError: 404: 404 Project Not Found

    So it looks like the documented approach wont fully work with CI_JOB_TOKEN as I'm assuming the scope of its privileges don't cover the underlying API call needed for pipeline.refresh()

  4. Letme commented on Jan 2, 2021

    @Letme
    Author

    @matthewcosgrove That is also what I would assume, but I could not find any reference anywhere in the documentation about this.

  5. matthewcosgrove commented on Jan 2, 2021

    @matthewcosgrove

    Found a very good summary here:

    1. https://gitlab.com/gitlab-org/gitlab/-/issues/35067#note_355141732
    2. https://gitlab.com/gitlab-org/gitlab/-/issues/35067#note_385512474.

    Seems there are a ton of related issues too but I'm not seeing much sign of the chosen direction by GitLab yet.

  6. matthewcosgrove commented on Jan 2, 2021

    @matthewcosgrove

    More observations using the CI_JOB_TOKEN instead of the "trigger token"

    • It enables the pipeline graph for multi-project pipelines
    • You dont get the label "triggered" like you do with the trigger token, and you dont get the trigger variables visible alongside the job logs for the triggered job in the UI

    Another alternative I will be investigating is a "bridge" job which removes the need for any API calls at all.

    Before GitLab 11.8, it was necessary to implement a pipeline job that was responsible for making the API request to trigger a pipeline in a different project. In GitLab 11.8, GitLab provides a new CI/CD configuration syntax to make this task easier, and avoid needing GitLab Runner for triggering cross-project pipelines.

  7. erakis commented on Jul 9, 2021

    @erakis

    Same problem on my side

    import gitlab
    import time
    
    gl = gitlab.Gitlab('https://mygitlab.com')
    project = gl.projects.get(1, lazy=True)  # no API call
    pipeline = project.trigger_pipeline('master', '**********', variables={"FOO": bar'})
    
    # So far all is well, I can confirm from the web site that hte the
    # pipeline has started and the buid is in progress :)
    
    while pipeline.finished_at is None:
        pipeline.refresh()  # gitlab.exceptions.GitlabGetError: 404: 404 Project Not Found
        time.sleep(1)
    
    print('Artifact ready!')
  8. nejch commented on Aug 6, 2022

    @nejch
    Member

    Hey folks, I just re-read this issue. This is basically expected upstream behavior, because refresh() calls an API endpoint that job tokens cannot access (you can only trigger the pipeline using it).

    In the last comment above mine, it's even more limited as the Gitlab instance was initialized without a token, and only one API call was made with a trigger token. This will of course not work for other endpoints.

    We also describe the job token limitation now in https://python-gitlab.readthedocs.io/en/stable/api-usage.html by linking to the upstream docs.

    I think this is probably the best we can do, because the job token permissions change frequrently in GitLab's API, and it'd be hard to track/maintain that continuously in our own client via either docs or user warnings for all the relevant endpoints. So I think we can close this now and you can track the upstream issues linked above for more flexible job token permissions. But please reopen if you disagree.

  9. locked as resolved and limited conversation to collaborators on Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions