Skip to content

Instantly share code, notes, and snippets.

@kholisrag
Created August 11, 2020 19:41
Show Gist options
  • Save kholisrag/71dbe245aff05b9b4009ff79075138e2 to your computer and use it in GitHub Desktop.
Save kholisrag/71dbe245aff05b9b4009ff79075138e2 to your computer and use it in GitHub Desktop.
Bash Script forRestore CI/CD Variables of a project, from json file
#!/bin/bash
init() {
PROJECT_ID=
GITLAB_PRIVATE_TOKEN=
}
restore_project_variables() {
while read project_var_key <&3 && \
read project_var_value <&4 && \
read project_var_variable_type <&5 && \
read project_var_protected <&6 && \
read project_var_masked <&7
do
echo -e "\n \e[1m\e[92m Restore ${project_var_key} = ${project_var_value} \e[0m \n"
curl --silent --request POST --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" \
"https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables" \
--form "key=${project_var_key}" \
--form "value=${project_var_value}" \
--form "variable_type=${project_var_variable_type}" \
--form "protected=${project_var_protected}" \
--form "masked=${project_var_masked}" \
| jq -r "."
done 3< <(cat "${1}" | jq -r ".[].key") \
4< <(cat "${1}" | jq -c ".[].value") \
5< <(cat "${1}" | jq -r ".[].variable_type") \
6< <(cat "${1}" | jq -r ".[].protected") \
7< <(cat "${1}" | jq -r ".[].masked")
}
main() {
init
restore_project_variables ${1}
}
main "$@"
@kholisrag
Copy link
Author

Please fill requirement in init function :

  PROJECT_ID=
  GITLAB_PRIVATE_TOKEN=

before running the script

associated with with https://gist.github.com/petrukngantuk/cf4dca0349756bc235c57346b9309d45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment