Skip to content

Commit ca55099

Browse files
renukamanavalanyxieca
authored andcommitted
Update dockers with platform & SONiC version as part of name/tag. (#4337)
* Include platform info in name. Get SONiC Version as parameter and use Make additional tag as optional. Avoid repetitions by using function. * Per review comments, make SONIC_VERSION optional and added some comments. * 1) Added additional params are optional 2) Handle DOCKER_IMAGE_TAG only if given 3) Use BUILD_NUMBER only if SONIC_VERSION not given 4) Tag with SONIC_VERSION if given. Current behavior is not changed, unless SONIC_VERSION is given. * Update per review comments 1) Added new args with options 2) Handle PORT possible being empty 3) Exhibit new behavior only if both version & platform are given. * Drop redundant quotes
1 parent fd2ca95 commit ca55099

1 file changed

Lines changed: 64 additions & 27 deletions

File tree

push_docker.sh

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,79 @@
1+
#! /bin/bash
2+
3+
sonic_version=""
4+
sonic_platform=""
5+
6+
while getopts ":v:p:" opt
7+
do
8+
case ${opt} in
9+
v ) # SONiC image version
10+
sonic_version=${OPTARG}
11+
;;
12+
p ) # Platform info
13+
sonic_platform=${OPTARG}
14+
;;
15+
\? ) echo "\
16+
Usage: [-v <version> ] [ -p <platform> ] <DOCKER_IMAGE_FILE> <REGISTRY_SERVER> <REGISTRY_PORT> \
17+
<REGISTRY_USERNAME> <REGISTRY_PASSWD> [<DOCKER_IMAGE_TAG>]"
18+
;;
19+
esac
20+
done
21+
22+
shift $((OPTIND -1))
23+
124
DOCKER_IMAGE_FILE=$1
225
REGISTRY_SERVER=$2
326
REGISTRY_PORT=$3
427
REGISTRY_USERNAME=$4
528
REGISTRY_PASSWD=$5
629
DOCKER_IMAGE_TAG=$6
30+
REGISTRY_SERVER_WITH_PORT=${REGISTRY_SERVER}${REGISTRY_PORT:+:$REGISTRY_PORT}
731

8-
set -e
9-
docker load < $DOCKER_IMAGE_FILE
32+
push_it() {
33+
# $1 - Given image name
34+
# $2 - Remote image name
1035

11-
## Fetch the Jenkins build number if inside it
12-
[ ${BUILD_NUMBER} ] || {
13-
echo "No BUILD_NUMBER found, setting to 0."
14-
BUILD_NUMBER="0"
36+
docker tag $1 $2
37+
echo "Pushing $2"
38+
image_sha=$(docker push $2 | sed -n "s/.*: digest: sha256:\([0-9a-f]*\).*/\\1/p")
39+
echo "Remove $2"
40+
docker rmi $2 || true
41+
echo "Image sha256: $image_sha"
1542
}
1643

17-
## Prepare tag
18-
docker_image_name=$(basename $DOCKER_IMAGE_FILE | cut -d. -f1)
19-
remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$DOCKER_IMAGE_TAG
20-
timestamp="$(date -u +%Y%m%d)"
21-
build_version="${timestamp}.bld-${BUILD_NUMBER}"
22-
build_remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$build_version
44+
set -e
2345

24-
## Add registry information as tag, so will push as latest
25-
## Add additional tag with build information
26-
docker tag $docker_image_name $remote_image_name
27-
docker tag $docker_image_name $build_remote_image_name
46+
echo "Loading image ${DOCKER_IMAGE_FILE}"
47+
docker load < ${DOCKER_IMAGE_FILE}
2848

2949
## Login the docker image registry server
3050
## Note: user name and password are passed from command line
31-
docker login -u $REGISTRY_USERNAME -p "$REGISTRY_PASSWD" $REGISTRY_SERVER:$REGISTRY_PORT
32-
33-
## Push image to registry server
34-
## And get the image digest SHA256
35-
echo "Pushing $remote_image_name"
36-
image_sha=$(docker push $remote_image_name | sed -n "s/.*: digest: sha256:\([0-9a-f]*\).*/\\1/p")
37-
docker rmi $remote_image_name || true
38-
echo "Image sha256: $image_sha"
39-
echo "Pushing $build_remote_image_name"
40-
docker push $build_remote_image_name
41-
docker rmi $build_remote_image_name || true
51+
docker login -u ${REGISTRY_USERNAME} -p "${REGISTRY_PASSWD}" ${REGISTRY_SERVER_WITH_PORT}
52+
53+
## Get Docker image name
54+
docker_image_name=$(basename ${DOCKER_IMAGE_FILE} | cut -d. -f1)
55+
remote_image_name=${REGISTRY_SERVER_WITH_PORT}/${docker_image_name}
56+
57+
[ -z "${DOCKER_IMAGE_TAG}" ] || {
58+
push_it ${docker_image_name} ${remote_image_name}:${DOCKER_IMAGE_TAG}
59+
}
60+
61+
if [ -n "${sonic_version}" ] && [ -n "${sonic_platform}" ]
62+
then
63+
remote_image_name=${REGISTRY_SERVER_WITH_PORT}/sonic-dockers/${sonic_platform}/${docker_image_name}:${sonic_version}
64+
push_it ${docker_image_name} ${remote_image_name}
65+
else
66+
## Fetch the Jenkins build number if inside it
67+
[ ${BUILD_NUMBER} ] || {
68+
echo "No BUILD_NUMBER found, setting to 0."
69+
BUILD_NUMBER="0"
70+
}
71+
72+
timestamp="$(date -u +%Y%m%d)"
73+
build_version="${timestamp}.bld-${BUILD_NUMBER}"
74+
push_it ${docker_image_name} ${remote_image_name}:${build_version}
75+
fi
76+
4277
docker rmi $docker_image_name || true
78+
echo "Job completed"
79+

0 commit comments

Comments
 (0)