Предварительно нужно создать Gitlab token в Web UI Gitlab (Edit profile -> Access Tokens -> Выдать все права).
В скрипт подставить значения для GITLAB_DOMAIN
, GITLAB_TOKEN
, PROJECT_ID_FROM
, PROJECT_ID_TO
.
#!/usr/bin/env bash
set -euo pipefail
GITLAB_DOMAIN="gitlab.some.domain"
GITLAB_TOKEN=""
PROJECT_ID_FROM="123"
PROJECT_ID_TO="321"
pages=$(curl -IL --silent --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://${GITLAB_DOMAIN}/api/v4/projects/${PROJECT_ID_FROM}/variables?per_page=1000" | grep "x-total-pages" | sed 's/x-total-pages: //g' | tr -d '\r' )
for page in $( seq 1 "${pages}" ); do
json_variables=$( curl --silent --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "https://${GITLAB_DOMAIN}/api/v4/projects/${PROJECT_ID_FROM}/variables?per_page=100&page=${page}" | jq -r '.' )
keys_in_page=$( echo "${json_variables}" | jq -r '.[].key' | wc -l )
for key in $( seq 0 "${keys_in_page}" ); do
json_data=$( echo "${json_variables}" | jq -r '.['$key']' )
echo "${json_variables}" | jq -r '.['$key'].key'
curl --request POST \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
--header "Content-type: application/json" \
--data "${json_data}" \
"https://${GITLAB_DOMAIN}/api/v4/projects/${PROJECT_ID_TO}/variables"
echo ""
done
done