Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Last active April 22, 2020 19:05
Show Gist options
  • Save LeZuse/401e3295cdbcdc835c0216d9d44ac722 to your computer and use it in GitHub Desktop.
Save LeZuse/401e3295cdbcdc835c0216d9d44ac722 to your computer and use it in GitHub Desktop.
Copy ENV vars from one GitLab project to another
GL_TOKEN=xxxxxxx-xxxxxxx
ID_FROM=00001
ID_DESTINATION=00002
PREFIX=
[ "$1" == "--dry-run" ] && PREFIX=echo
download_page () {
DATA=`curl --header "PRIVATE-TOKEN: $GL_TOKEN" "https://gitlab.com/api/v4/projects/$ID_FROM/variables?per_page=100&page=$c"`
for row in $(echo "$DATA" | jq -r '.[] | @base64'); do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
echo "Copying $(_jq '.key') for $(_jq '.environment_scope') ..."
$PREFIX curl --request POST \
--header "PRIVATE-TOKEN: $GL_TOKEN" \
"https://gitlab.com/api/v4/projects/$ID_DESTINATION/variables" \
--form "key=$(_jq '.key')" \
--form "value=$(_jq '.value')" \
--form "protected=$(_jq .'protected')" \
--form "masked=$(_jq '.masked')" \
--form "environment_scope=$(_jq '.environment_scope')" \
--form "variable_type=$(_jq '.variable_type')"
done
}
START=1
PAGES=`curl -I --header "PRIVATE-TOKEN: $GL_TOKEN" "https://gitlab.com/api/v4/projects/$ID_FROM/variables?per_page=100" | grep 'x-total-pages' | cut -d' ' -f2`
echo $PAGES
# handles 1000 env vars max
for c in {1..10}
do
# extra pages will just skim through empty response
download_page $c
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment