Whenever I have to setup private.yml
when working setting up a grove cluster repository locally, it’s annoying to have to manually copy CI/CD variables into the file. I decided to automate it a bit.
First of all, we can get variables using GitLab’s API (gitlab docs):
curl --header "PRIVATE-TOKEN: <your access token with api access>" "https://gitlab.com/api/v4/projects/<project_id>/variables"
Then you would want to parse JSON. jq
is a great command for that, and you can get all the variables in the form <name>: <value>
you can do the following (assumes you’ve saved the output of the previous command to ci-cd-variables.json
):
jq '.[] | .key + ": " + .value' ci-cd-variables.json --raw-output
We can go even further by using sed
to automatically put values into the private.yml
.
This will make changes to your
private.yml
.
jq '.[] | .key + ": .*" + "," + .key + ": " + .value' ci-cd-variables.json --raw-output | xargs -i sed -i -e 's,{},' private.yml
You can double check that the change are correct using:
diff private.yml private.yml.example