RandomB
RandomB

Reputation: 3737

How to preform REST call to GitLab API from a running job?

I need to make a Gitlab API REST call (let's say "GET /projects/:id/jobs/:job_id") from a script running during CI pipelines' job execution. I need a token to pass through security check. Sure I can create such a token (PAT) and to use it and it works fine. But how can I save/pass this token to the pipeline? To hard-code it in the Gitlab CI YAML file? To hard-code it directly in the script? What is the correct solution here?

I read here that Gitlab itself provides such a token directly in the job. Previously it was stored in the CI_JOB_TOKEN environment variable. I tried it, but I get HTTP 401 error, and it seems its string value is "[MASKED]". How to achieve it - to do REST call with a token during job execution in convenient way? Any example, hints will be very helpful.

EDIT: I found this post on Reddit. It seems the way is PAT only (separate per project) ?

Upvotes: 0

Views: 3483

Answers (1)

makozaki
makozaki

Reputation: 4366

But how can I save/pass this token to the pipeline? To hard-code it in the Gitlab CI YAML file? To hard-code it directly in the script? What is the correct solution here?

Just inject it via environment variable: Settings -> CI/CD -> Variables

Upvotes: 1

Related Questions