Reputation: 403
I want to retrieve and use the project information using the predefined variables in GitLab such as CI_COMMIT_AUTHOR.
I want to use that variable outside of the .gitlab-ci.yml file.
I am writing a template so when it's used the file should repopulate with the correct information.
For example, the template includes a .yaml file
apiVersion: sample.io/v1alpha1
kind: Component
metadata:
name: #said team app"my_awesome_app"
description: #description of app
annotations:
backstage.io/techdocs-ref: dir:.
spec:
type: documentation
lifecycle: experimental
owner: $CI_COMMIT_AUTHOR
Upvotes: 1
Views: 1318
Reputation: 4400
If you want to use Gitlab variables outside the .gitlab-ci.yml, the way to achieve this is to query them through the Gitlab API
But unfortunately, for the Gitlab predefined
variables it seems that an API to retrieve them doesn't exist
My proposal would be to create a file with all the variables you require, in your gitlab-ci.yml. Store it the artifacts and retrieve the artifacts through Gitlab API
To achieve this firstly add a job to your gitlab yml, that stores the predefined variables in an artifact E.g
store_predefined_variables:
...
script:
- touch variables.txt
- echo "CI_COMMIT_AUTHOR=$CI_COMMIT_AUTHOR" >> variables.txt
artifacts:
paths:
- ./variables.txt
Next retrieve the id of the job with name store_predefined_variables
https://docs.gitlab.com/ee/api/jobs.html#list-project-jobs
GET /projects/:id/jobs
Finally, use the job id
to retrieve the artifacts that contain all the needed variables https://docs.gitlab.com/ee/api/job_artifacts.html#get-job-artifacts
GET /projects/:id/jobs/:job_id/artifacts
Combining the two API calls and utilizing the jq
command, we can get a one liner that retrieves the artifacts of the most recent job with name store_predefined_variables
. The artifacts are download as artifacts.zip
wget -U "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.17 (KHTML,like Gecko) Ubuntu/11.04 Chromium/11.0.654.0 Chrome/11.0.654.0 Safari/534.17" --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/jobs/$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/jobs" | jq '[.[] | select(.name=="store_predefined_variables")] | .[0].id')/artifacts" -O artifacts.zip
Upvotes: 1