gitlabgitlab-cihttp-status-code-401gitlab-api

Getting 401 from gitlab ci trying to use gitlab api


I am running into a frustrating issue where I am failing to auth to the gitlab api from within a gitlab ci job (to add a label to a MR based on if a file is touched) using a group token.

I run the cmd locally using the same token value and it works as expected, but within the gitlab ci job it returns a 401 un auth error. I tested across multiple different endpoints and they all have the same outcome.

I have tried both via headers and via query to no avail based on a recent gitlab issue I came across.

curl --fail-with-body -X PUT $CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID?add_labels=$LABEL&private_token=$TOKEN

curl --fail-with-body -H "PRIVATE-TOKEN:$TOKEN" -X PUT $CI_API_V4_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID?add_labels=$LABEL

Solution

  • ensure the $TOKEN parameter is set in the injected ci variables. You can do it in either your project or a parent directory of the project to apply the variable more globally.

    Project > Settings > CI/CD > "Variables"