You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Trigger pipeline and download artifacts from triggered pipeline #1021
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.
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
project.trigger_pipelinedoes work for me with CI_JOB_TOKEN so the downstream job triggering works fine
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()
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.
importgitlabimporttimegl=gitlab.Gitlab('https://mygitlab.com')
project=gl.projects.get(1, lazy=True) # no API callpipeline=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 :)whilepipeline.finished_atisNone:
pipeline.refresh() # gitlab.exceptions.GitlabGetError: 404: 404 Project Not Foundtime.sleep(1)
print('Artifact ready!')
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.
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.
Activity
max-wittig commentedon Feb 19, 2020
Where do you get
targetprojecttokenfrom?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)
Letme commentedon Feb 19, 2020
targetprojecttokenis token of the trigger, not of the user. But my main expectation is thatCI_JOB_TOKENshould be enough?matthewcosgrove commentedon Jan 2, 2021
Also have been trying to get the documented example referenced above working with mixed results.
Related observations using GitLab Enterprise Edition 13.5.5-ee
project.trigger_pipelinedoes work for me withCI_JOB_TOKENso the downstream job triggering works finepipeline.refresh()returnsgitlab.exceptions.GitlabGetError: 404: 404 Project Not FoundSo it looks like the documented approach wont fully work with
CI_JOB_TOKENas I'm assuming the scope of its privileges don't cover the underlying API call needed forpipeline.refresh()Letme commentedon Jan 2, 2021
@matthewcosgrove That is also what I would assume, but I could not find any reference anywhere in the documentation about this.
matthewcosgrove commentedon Jan 2, 2021
Found a very good summary here:
Seems there are a ton of related issues too but I'm not seeing much sign of the chosen direction by GitLab yet.
matthewcosgrove commentedon Jan 2, 2021
More observations using the
CI_JOB_TOKENinstead of the "trigger token"Another alternative I will be investigating is a "bridge" job which removes the need for any API calls at all.
erakis commentedon Jul 9, 2021
Same problem on my side
nejch commentedon Aug 6, 2022
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.