Created
June 5, 2022 07:16
-
-
Save kyriakos/0885c618012a932420000e56ae5ae758 to your computer and use it in GitHub Desktop.
Clone Gitlab CI variables from one Gitlab Repository to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The following script will blindly clone all Gitlab CI variables from source repository to the target. Created this to move CI from my own repository to a client's own. | |
// Update the project_id/api_key (and url's if needed below), install the dependency "npm i node-fetch" and then run using node clonecivariables.mjs | |
import fetch from 'node-fetch'; // run in the same dir as the script: npm i node-fetch | |
const source_project_id = ''; // can be found on repository's home page under the repository name. | |
const source_api_key = '' // get one here https://gitlab.com/-/profile/personal_access_tokens (create a personal token with API scope) | |
const source_url = 'https://gitlab.com/'; // only need to change this if using self-hosted gitlab | |
const target_project_id = ''; | |
const target_api_key = ''; | |
const target_url = 'https://gitlab.com/'; // only need to change this if using self-hosted gitlab | |
const source = `${source_url}api/v4/projects/${source_project_id}/variables`; | |
const target = `${target_url}api/v4/projects/${target_project_id}/variables`; | |
(async () => { | |
const response = await fetch(source, { | |
headers: { | |
'Content-Type': 'application/json', | |
'PRIVATE-TOKEN': source_api_key | |
}, | |
}); | |
const variables = await response.json(); | |
for (const variable of variables) { | |
const response = await fetch(target, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'PRIVATE-TOKEN': source_api_key | |
}, | |
body: JSON.stringify(variable) | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment