Skip to content

Instantly share code, notes, and snippets.

@kholisrag
Last active August 11, 2020 19:41
Show Gist options
  • Save kholisrag/cf4dca0349756bc235c57346b9309d45 to your computer and use it in GitHub Desktop.
Save kholisrag/cf4dca0349756bc235c57346b9309d45 to your computer and use it in GitHub Desktop.
Bash Script for Backup and Delete CI/CD Variables of a project, store it in json and conf file
#!/bin/bash
init() {
PROJECT_ID=
GITLAB_PRIVATE_TOKEN=
touch ./backup_envar_${PROJECT_ID}.conf
touch ./backup_envar_${PROJECT_ID}.json
}
backup_and_delete_project_variables() {
curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables?per_page=200" | jq -r "." >> ./backup_envar_${PROJECT_ID}.json
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 "${project_var_key}=${project_var_value}" >> ./backup_envar_${PROJECT_ID}.conf
echo -e "\e[1m\e[31m Deleting ${project_var_key} \e[0m \n"
curl --silent --request DELETE --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables/${project_var_key}"
done 3< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables?per_page=200" | jq -r ".[].key") \
4< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables?per_page=200" | jq -c ".[].value") \
5< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables?per_page=200" | jq -r ".[].variable_type") \
6< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables?per_page=200" | jq -r ".[].protected") \
7< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/variables?per_page=200" | jq -r ".[].masked")
echo -e "\n" >> ./backup_envar_${PROJECT_ID}.conf
}
main() {
init
backup_and_delete_project_variables
}
main "$@"
@kholisrag
Copy link
Author

Please fill requirement in init function :

  PROJECT_ID=
  GITLAB_PRIVATE_TOKEN=

before running the script

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