Skip to content

Instantly share code, notes, and snippets.

@crigertg
Last active December 13, 2022 14:43
Show Gist options
  • Save crigertg/7a58893e2bdc6bd6ba5e1891b2401839 to your computer and use it in GitHub Desktop.
Save crigertg/7a58893e2bdc6bd6ba5e1891b2401839 to your computer and use it in GitHub Desktop.
Create container expiration policy for all gitlab repos in a group
#!/bin/bash
TOKEN=<your_gitlab_token>
GITLAB_URL=https://gitlab.com
GROUP_ID=<your_group_id>
SETTINGS=$(cat <<EOF
{
"container_expiration_policy_attributes": {
"cadence": "1d",
"enabled": true,
"keep_n": 5,
"older_than": "14d",
"name_regex": ".*",
"name_regex_keep": "^(?:v.+|main|master|release|dev|stage|preprod|prod).*"
}
}
EOF
)
for repo_id in $(curl -s --header "PRIVATE-TOKEN: $TOKEN" $GITLAB_URL/api/v4/groups/$GROUP_ID | jq -r ".projects[].id"); do
echo "Setting container expiration policy for repo $repo_id."
CURLOUT=$(curl -s --request PUT --header 'Content-Type: application/json;charset=UTF-8' --header "PRIVATE-TOKEN: $TOKEN" \
--data-binary "$SETTINGS" \
"$GITLAB_URL/api/v4/projects/$repo_id")
if [ $? -ne "0" ] || [ "$(echo $CURLOUT | jq '.message')" != 'null' ]; then
echo "Something went wrong while trying to put config to repo $repo_id" >&2
echo "$CURLOUT" >&2
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment