2020-04-09 23:18:14 -05:00
|
|
|
#! /bin/bash
|
|
|
|
|
|
|
|
sonic_version=""
|
|
|
|
sonic_platform=""
|
|
|
|
|
|
|
|
while getopts ":v:p:" opt
|
|
|
|
do
|
|
|
|
case ${opt} in
|
|
|
|
v ) # SONiC image version
|
|
|
|
sonic_version=${OPTARG}
|
|
|
|
;;
|
|
|
|
p ) # Platform info
|
|
|
|
sonic_platform=${OPTARG}
|
|
|
|
;;
|
|
|
|
\? ) echo "\
|
|
|
|
Usage: [-v <version> ] [ -p <platform> ] <DOCKER_IMAGE_FILE> <REGISTRY_SERVER> <REGISTRY_PORT> \
|
|
|
|
<REGISTRY_USERNAME> <REGISTRY_PASSWD> [<DOCKER_IMAGE_TAG>]"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND -1))
|
|
|
|
|
2016-12-01 04:18:59 -06:00
|
|
|
DOCKER_IMAGE_FILE=$1
|
|
|
|
REGISTRY_SERVER=$2
|
|
|
|
REGISTRY_PORT=$3
|
|
|
|
REGISTRY_USERNAME=$4
|
|
|
|
REGISTRY_PASSWD=$5
|
|
|
|
DOCKER_IMAGE_TAG=$6
|
2020-04-09 23:18:14 -05:00
|
|
|
REGISTRY_SERVER_WITH_PORT=${REGISTRY_SERVER}${REGISTRY_PORT:+:$REGISTRY_PORT}
|
2016-12-01 04:18:59 -06:00
|
|
|
|
2020-04-09 23:18:14 -05:00
|
|
|
push_it() {
|
|
|
|
# $1 - Given image name
|
|
|
|
# $2 - Remote image name
|
2016-12-01 04:18:59 -06:00
|
|
|
|
2020-04-09 23:18:14 -05:00
|
|
|
docker tag $1 $2
|
|
|
|
echo "Pushing $2"
|
|
|
|
image_sha=$(docker push $2 | sed -n "s/.*: digest: sha256:\([0-9a-f]*\).*/\\1/p")
|
|
|
|
echo "Remove $2"
|
|
|
|
docker rmi $2 || true
|
|
|
|
echo "Image sha256: $image_sha"
|
2016-12-01 04:18:59 -06:00
|
|
|
}
|
|
|
|
|
2020-04-09 23:18:14 -05:00
|
|
|
set -e
|
2016-12-01 04:18:59 -06:00
|
|
|
|
2020-04-09 23:18:14 -05:00
|
|
|
echo "Loading image ${DOCKER_IMAGE_FILE}"
|
|
|
|
docker load < ${DOCKER_IMAGE_FILE}
|
2016-12-01 04:18:59 -06:00
|
|
|
|
|
|
|
## Login the docker image registry server
|
|
|
|
## Note: user name and password are passed from command line
|
2020-04-09 23:18:14 -05:00
|
|
|
docker login -u ${REGISTRY_USERNAME} -p "${REGISTRY_PASSWD}" ${REGISTRY_SERVER_WITH_PORT}
|
|
|
|
|
|
|
|
## Get Docker image name
|
|
|
|
docker_image_name=$(basename ${DOCKER_IMAGE_FILE} | cut -d. -f1)
|
|
|
|
remote_image_name=${REGISTRY_SERVER_WITH_PORT}/${docker_image_name}
|
|
|
|
|
2022-05-09 03:43:21 -05:00
|
|
|
for tag in ${DOCKER_IMAGE_TAG}
|
|
|
|
do
|
|
|
|
push_it ${docker_image_name} ${remote_image_name}:$tag
|
|
|
|
done
|
2020-04-09 23:18:14 -05:00
|
|
|
|
|
|
|
if [ -n "${sonic_version}" ] && [ -n "${sonic_platform}" ]
|
|
|
|
then
|
|
|
|
remote_image_name=${REGISTRY_SERVER_WITH_PORT}/sonic-dockers/${sonic_platform}/${docker_image_name}:${sonic_version}
|
|
|
|
push_it ${docker_image_name} ${remote_image_name}
|
|
|
|
else
|
|
|
|
## Fetch the Jenkins build number if inside it
|
|
|
|
[ ${BUILD_NUMBER} ] || {
|
|
|
|
echo "No BUILD_NUMBER found, setting to 0."
|
|
|
|
BUILD_NUMBER="0"
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp="$(date -u +%Y%m%d)"
|
|
|
|
build_version="${timestamp}.bld-${BUILD_NUMBER}"
|
|
|
|
push_it ${docker_image_name} ${remote_image_name}:${build_version}
|
|
|
|
fi
|
|
|
|
|
2017-05-19 15:49:43 -05:00
|
|
|
docker rmi $docker_image_name || true
|
2020-04-09 23:18:14 -05:00
|
|
|
echo "Job completed"
|
|
|
|
|