-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathpush
executable file
·149 lines (125 loc) · 4.29 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Copyright 2019-2020 The OpenEBS Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
if [ -z ${DIMAGE} ];
then
echo "Error: DIMAGE is not specified";
exit 1
fi
function pushBuildx() {
BUILD_TAG="latest"
TARGET_IMG=${DIMAGE}
# TODO Currently ci builds with commit tag will not be generated,
# since buildx does not support multiple repo
# if not a release build set the tag and ci image
if [ -z "${RELEASE_TAG}" ]; then
return
# BUILD_ID=$(git describe --tags --always)
# BUILD_TAG="${BRANCH}-${BUILD_ID}"
# TARGET_IMG="${DIMAGE}-ci"
fi
echo "Tagging and pushing ${DIMAGE}:${TAG} as ${TARGET_IMG}:${BUILD_TAG}"
docker buildx imagetools create "${DIMAGE}:${TAG}" -t "${TARGET_IMG}:${BUILD_TAG}"
}
# if the push is for a buildx build
if [[ ${BUILDX} ]]; then
pushBuildx
exit 0
fi
IMAGEID=$( sudo docker images -q ${DIMAGE}:ci )
echo "${DIMAGE}:ci -> $IMAGEID"
if [ -z ${IMAGEID} ];
then
echo "Error: unable to get IMAGEID for ${DIMAGE}:ci";
exit 1
fi
# Generate a unique tag based on the commit and tag
BUILD_ID=$(git describe --tags --always)
# Determine the current branch
CURRENT_BRANCH=""
if [ -z ${BRANCH} ];
then
CURRENT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2)
else
CURRENT_BRANCH=${BRANCH}
fi
#Depending on the branch where builds are generated,
# set the tag CI (fixed) and build tags.
BUILD_TAG="${CURRENT_BRANCH}-${BUILD_ID}"
CI_TAG="${CURRENT_BRANCH}-ci"
if [ ${CURRENT_BRANCH} = "develop" ]; then
CI_TAG="ci"
fi
echo "Set the fixed ci image tag as: ${CI_TAG}"
echo "Set the build/unique image tag as: ${BUILD_TAG}"
function TagAndPushImage() {
REPO="$1"
# Trim the `v` from the TAG if it exists
# Example: v1.10.0 maps to 1.10.0
# Example: 1.10.0 maps to 1.10.0
# Example: v1.10.0-custom maps to 1.10.0-custom
TAG="${2#v}"
#Add an option to specify a custom TAG_SUFFIX
#via environment variable. Default is no tag.
#Example suffix could be "-debug" of "-dev"
IMAGE_URI="${REPO}:${TAG}${TAG_SUFFIX}";
sudo docker tag ${IMAGEID} ${IMAGE_URI};
echo " push ${IMAGE_URI}";
sudo docker push ${IMAGE_URI};
}
if [ ! -z "${DNAME}" ] && [ ! -z "${DPASS}" ];
then
sudo docker login -u "${DNAME}" -p "${DPASS}";
# Push CI tagged image - :ci or :branch-ci
TagAndPushImage "${DIMAGE}" "${CI_TAG}"
# Push unique tagged image - :develop-<uuid> or :branch-<uuid>
# This unique/build image will be pushed to corresponding ci repo.
TagAndPushImage "${DIMAGE}-ci" "${BUILD_TAG}"
if [ ! -z "${RELEASE_TAG}" ] ;
then
# Push with different tags if tagged as a release
# When github is tagged with a release, then github action release workflow
# will set the release tag in env RELEASE_TAG
TagAndPushImage "${DIMAGE}" "${RELEASE_TAG}"
TagAndPushImage "${DIMAGE}" "latest"
fi;
else
echo "No docker credentials provided. Skip uploading ${DIMAGE} to docker hub";
fi;
# Push ci image to quay.io for security scanning
if [ ! -z "${QNAME}" ] && [ ! -z "${QPASS}" ];
then
sudo docker login -u "${QNAME}" -p "${QPASS}" quay.io;
# Push CI tagged image - :ci or :branch-ci
TagAndPushImage "quay.io/${DIMAGE}" "${CI_TAG}"
if [ ! -z "${RELEASE_TAG}" ] ;
then
# Push with different tags if tagged as a release
# When github is tagged with a release, then github action reelase workflow
# will set the release tag in env RELEASE_TAG
# Trim the `v` from the RELEASE_TAG if it exists
TagAndPushImage "quay.io/${DIMAGE}" "${RELEASE_TAG}"
TagAndPushImage "quay.io/${DIMAGE}" "latest"
fi;
else
echo "No docker credentials provided. Skip uploading ${DIMAGE} to quay";
fi;
#Push image to run openebs-e2e based on git commit
if [ ! -z "${COMMIT}" ];
then
sudo docker login -u "${GITLAB_DNAME}" -p "${GITLAB_DPASS}";
# Push COMMIT tagged image - :COMMIT
TagAndPushImage "${DIMAGE}" "${COMMIT}"
fi;