Skip to content

Instantly share code, notes, and snippets.

@kholisrag
Created August 11, 2020 17:56
Show Gist options
  • Save kholisrag/3c6f9db029cca6f6237d6d15c8e81bd7 to your computer and use it in GitHub Desktop.
Save kholisrag/3c6f9db029cca6f6237d6d15c8e81bd7 to your computer and use it in GitHub Desktop.
Bash Script for Import CI/CD Variables from one project to another project
#!/bin/bash
init() {
SOURCE_PROJECT_ID=
TARGET_PROJECT_ID=
GITLAB_PRIVATE_TOKEN=
}
import_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 Adding ${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/${TARGET_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< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${SOURCE_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/${SOURCE_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/${SOURCE_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/${SOURCE_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/${SOURCE_PROJECT_ID}/variables?per_page=200" | jq -r ".[].masked")
}
main() {
init
import_project_variables
}
main "$@"
@kholisrag
Copy link
Author

Please fill requirement in init function :

  SOURCE_GROUP_ID=
  TARGET_GROUP_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