Skip to content

Instantly share code, notes, and snippets.

@kholisrag
Last active August 11, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kholisrag/2225c5871dad072593e76f90b58ac1b4 to your computer and use it in GitHub Desktop.
Save kholisrag/2225c5871dad072593e76f90b58ac1b4 to your computer and use it in GitHub Desktop.
Bash Script for Import CI/CD Variables from one group to another group
#!/bin/bash
init() {
SOURCE_GROUP_ID=
TARGET_GROUP_ID=
GITLAB_PRIVATE_TOKEN=
}
import_group_variables() {
while read group_var_key <&3 && \
read group_var_value <&4 && \
read group_var_variable_type <&5 && \
read group_var_protected <&6 && \
read group_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/groups/${TARGET_GROUP_ID}/variables" \
--form "key=${group_var_key}" \
--form "value=${group_var_value}" \
--form "variable_type=${group_var_variable_type}" \
--form "protected=${group_var_protected}" \
--form "masked=${group_var_masked}" \
| jq -r "."
done 3< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/groups/${SOURCE_GROUP_ID}/variables?per_page=200" | jq -r ".[].key") \
4< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/groups/${SOURCE_GROUP_ID}/variables?per_page=200" | jq -c ".[].value") \
5< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/groups/${SOURCE_GROUP_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/groups/${SOURCE_GROUP_ID}/variables?per_page=200" | jq -r ".[].protected") \
7< <(curl --silent --request GET --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/groups/${SOURCE_GROUP_ID}/variables?per_page=200" | jq -r ".[].masked")
}
main() {
init
import_group_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