forked from lifechurch/k8s-deploy-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push
executable file
·73 lines (61 loc) · 3.09 KB
/
push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
export DEPLOY_ROOT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
source "$DEPLOY_ROOT_DIR/src/common.bash"
if [[ -n "$CI_REGISTRY_USER" ]]; then
echo "Logging to GitLab Container Registry with CI credentials..."
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
echo ""
fi
echo "Tagging application as latest..."
if [ "x$1" != "x" ]; then
export SHORT=$(echo "$1" | cut -d'-' -f2-)
echo "Tagging $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG as $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-latest"
docker pull $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG
echo "Pushing to GitLab Container Registry..."
if [ "x$CI_COMMIT_TAG" != "x" ]; then
docker tag $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:$SHORT
docker push $PIPELINE_CI_REGISTRY_IMAGE:$SHORT
docker rmi $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:$SHORT
else
docker tag $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-latest
docker push $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-latest
docker rmi $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-latest
fi
else
echo "Tagging $PIPELINE_CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG as $PIPELINE_CI_REGISTRY_IMAGE:latest"
docker pull $PIPELINE_CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG
docker tag $PIPELINE_CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:latest
echo "Pushing to GitLab Container Registry..."
if [ "x$CI_COMMIT_TAG" != "x" ]; then
docker tag $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
docker push $PIPELINE_CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
docker rmi $PIPELINE_CI_REGISTRY_IMAGE:$SHORT-$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
else
docker tag $PIPELINE_CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:latest
docker push $PIPELINE_CI_REGISTRY_IMAGE:latest
docker rmi $PIPELINE_CI_REGISTRY_IMAGE:$CI_REGISTRY_TAG $PIPELINE_CI_REGISTRY_IMAGE:latest
fi
fi
if [ "$REGISTRY_CLEANUP" == "true" ] && [[ "$REGISTRY_TOKEN" ]] && [ "x$CI_COMMIT_TAG" == "x" ]; then
if [ "x$CI_REGISTRY_IMAGE_SUFFIX" != "x" ]; then
REGISTRY_PATH="$CI_PROJECT_PATH/$CI_REGISTRY_IMAGE_SUFFIX"
else
REGISTRY_PATH="$CI_PROJECT_PATH"
fi
REGISTRY_ID=$(curl -s \
--header "PRIVATE-TOKEN: $REGISTRY_TOKEN" \
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories" | jq ".[] | select(.path==\"$REGISTRY_PATH\") | .id")
curl -s \
--request DELETE \
--data "name_regex=*[0-9a-z]{40}" \
--data 'keep_n=5' \
--data 'older_than=2d' \
--header "PRIVATE-TOKEN: $REGISTRY_TOKEN" \
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/registry/repositories/$REGISTRY_ID/tags"
fi