Created
May 13, 2022 08:41
-
-
Save mRoca/96333782cbda0f5686cd4036668ad95f to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env sh | |
set -e | |
# Usage: | |
# export CI_API_TOKEN=<YOUR_TOKEN with api scope from https://gitlab.com/profile/personal_access_tokens> | |
# export CI_API_V4_URL=https://gitlab.com/api/v4 | |
# export CI_PROJECT_ID=37 | |
# | |
# ./deployment/bin/stop-all-dev-environments | |
# Stop all running or available dev envs | |
NEXT_ENV_COMMAND="curl -s --header 'PRIVATE-TOKEN: ${CI_API_TOKEN}' '${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/environments?search=dev/&states=available&per_page=1'" | |
next_env=$(eval "${NEXT_ENV_COMMAND}") | |
until [ $(echo "$next_env" | jq '. | length') -eq 0 ] | |
do | |
echo "Stopping $(echo "$next_env" | jq '.[0].name')" | |
curl -s --request POST --header "PRIVATE-TOKEN: ${CI_API_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/environments/$(echo "$next_env" | jq '.[0].id')/stop" 1>/dev/null | |
next_env=$(eval "${NEXT_ENV_COMMAND}") | |
done | |
# Delete all stopped dev envs | |
NEXT_STOPPED_ENV_COMMAND="curl -s --header 'PRIVATE-TOKEN: ${CI_API_TOKEN}' '${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/environments?search=dev/&states=stopped&per_page=1'" | |
next_stopped_env=$(eval "${NEXT_STOPPED_ENV_COMMAND}") | |
until [ $(echo "$next_stopped_env" | jq '. | length') -eq 0 ] | |
do | |
echo "Removing $(echo "$next_stopped_env" | jq '.[0].name')" | |
curl -s --request DELETE --header "PRIVATE-TOKEN: ${CI_API_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/environments/$(echo "$next_stopped_env" | jq '.[0].id')" 1>/dev/null | |
next_stopped_env=$(eval "${NEXT_STOPPED_ENV_COMMAND}") | |
done | |
echo "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment