Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dockers with platform & SONiC version as part of name/tag. #4337

Merged
merged 5 commits into from
Apr 10, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 61 additions & 25 deletions push_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,76 @@ REGISTRY_SERVER=$2
REGISTRY_PORT=$3
REGISTRY_USERNAME=$4
REGISTRY_PASSWD=$5
DOCKER_IMAGE_TAG=$6
DOCKER_IMAGE_TAG=""
PLATFORM=""
SONIC_VERSION=""

shift 5

if [ "$#" != "0" ]
then
DOCKER_IMAGE_TAG=$1
shift
fi

if [ "$#" != "0" ]
then
PLATFORM=$1
shift
fi

if [ "$#" != "0" ]
then
SONIC_VERSION=$1
Copy link
Collaborator

@qiluo-msft qiluo-msft Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SONIC_VERSION [](start = 4, length = 13)

What if user only have SONIC_VERSION but no PLATFORM? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script does not support it. Being positional args user can put "generic", when PLATFORM can't be given.

fi

push_it() {
# $1 - Given image name
# $2 - Remote image name

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"
}

set -e
docker load < $DOCKER_IMAGE_FILE

echo "Loading image ${DOCKER_IMAGE_FILE}"
docker load < ${DOCKER_IMAGE_FILE}

## Fetch the Jenkins build number if inside it
[ ${BUILD_NUMBER} ] || {
echo "No BUILD_NUMBER found, setting to 0."
BUILD_NUMBER="0"
}

## Prepare tag
docker_image_name=$(basename $DOCKER_IMAGE_FILE | cut -d. -f1)
remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$DOCKER_IMAGE_TAG
timestamp="$(date -u +%Y%m%d)"
build_version="${timestamp}.bld-${BUILD_NUMBER}"
build_remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$build_version

## Add registry information as tag, so will push as latest
## Add additional tag with build information
docker tag $docker_image_name $remote_image_name
docker tag $docker_image_name $build_remote_image_name

## Login the docker image registry server
## Note: user name and password are passed from command line
docker login -u $REGISTRY_USERNAME -p "$REGISTRY_PASSWD" $REGISTRY_SERVER:$REGISTRY_PORT

## Push image to registry server
## And get the image digest SHA256
echo "Pushing $remote_image_name"
image_sha=$(docker push $remote_image_name | sed -n "s/.*: digest: sha256:\([0-9a-f]*\).*/\\1/p")
docker rmi $remote_image_name || true
echo "Image sha256: $image_sha"
echo "Pushing $build_remote_image_name"
docker push $build_remote_image_name
docker rmi $build_remote_image_name || true
docker login -u ${REGISTRY_USERNAME} -p "${REGISTRY_PASSWD}" ${REGISTRY_SERVER}:${REGISTRY_PORT}

## Get Docker image name
docker_image_name=$(basename ${DOCKER_IMAGE_FILE} | cut -d. -f1)
remote_image_name=${REGISTRY_SERVER}:${REGISTRY_PORT}/${docker_image_name}

[ -z "${DOCKER_IMAGE_TAG}" ] || {
push_it ${docker_image_name} ${remote_image_name}:${DOCKER_IMAGE_TAG}
}

[ -n "${SONIC_VERSION}" ] || {
# If SONIC_VERSION is not given, tag it with build number.
timestamp="$(date -u +%Y%m%d)"
build_version="${timestamp}.bld-${BUILD_NUMBER}"
push_it ${docker_image_name} ${remote_image_name}:${build_version}
}

[ -z "${SONIC_VERSION}" ] || {
remote_image_name=${REGISTRY_SERVER}/sonic-dockers/${PLATFORM}/${docker_image_name}:${SONIC_VERSION}
push_it ${docker_image_name} ${remote_image_name}
}

docker rmi $docker_image_name || true
echo "Job completed"