-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement docker build pipeline on Jenkins with corresponding scripts (…
…#859) Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> Signed-off-by: Peter Zhu <zhujiaxi@amazon.com>
- Loading branch information
1 parent
fa2ebcd
commit 19c50e1
Showing
6 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/bin/bash | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# This script is to automate the docker multi arch image creation process | ||
|
||
set -e | ||
|
||
# Import libs | ||
. ../../lib/shell/file_management.sh | ||
|
||
# Variable | ||
OLDIFS=$IFS | ||
BUILDER_NUM=`date +%s` | ||
BUILDER_NAME="multiarch_${BUILDER_NUM}" | ||
DIR="" | ||
|
||
function usage() { | ||
echo "" | ||
echo "This script is used to build the Docker image with multi architecture (x64 + arm64). It prepares the files required by the Dockerfile in a temporary directory, then builds and tags the Docker image." | ||
echo "--------------------------------------------------------------------------" | ||
echo "Usage: $0 [args]" | ||
echo "" | ||
echo "Required arguments:" | ||
echo -e "-v TAG_NAME\tSpecify the image tag name such as 'centos7-x64-arm64-jdkmulti-node10.24.1-cypress6.9.1-20211019'" | ||
echo -e "-f DOCKERFILE\tSpecify the dockerfile full path, e.g. dockerfile/opensearch.al2.dockerfile." | ||
echo "" | ||
echo "Optional arguments:" | ||
echo -e "-h\t\tPrint this message." | ||
echo "--------------------------------------------------------------------------" | ||
} | ||
|
||
function cleanup_docker_buildx() { | ||
# Cleanup docker buildx | ||
echo -e "\n* Cleanup docker buildx" | ||
docker buildx use default | ||
docker buildx rm $BUILDER_NAME > /dev/null 2>&1 | ||
} | ||
|
||
function cleanup_all() { | ||
cleanup_docker_buildx | ||
File_Delete $DIR | ||
} | ||
while getopts ":hv:f:" arg; do | ||
case $arg in | ||
h) | ||
usage | ||
exit 1 | ||
;; | ||
v) | ||
TAG_NAME=$OPTARG | ||
;; | ||
f) | ||
DOCKERFILE=$OPTARG | ||
;; | ||
:) | ||
echo "-${OPTARG} requires an argument" | ||
usage | ||
exit 1 | ||
;; | ||
?) | ||
echo "Invalid option: -${arg}" | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
# Validate the required parameters to present | ||
if [ -z "$TAG_NAME" ] || [ -z "$DOCKERFILE" ] || [ -z "$REPOSITORY" ]; then | ||
echo "You must specify '-v TAG_NAME', '-f DOCKERFILE', '-r REPOSITORY'" | ||
usage | ||
exit 1 | ||
else | ||
echo $TAG_NAME $DOCKERFILE $REPOSITORY | ||
fi | ||
|
||
# Warning docker desktop | ||
if (! docker buildx version) | ||
then | ||
echo -e "\n* You MUST have Docker Desktop to use buildx for multi-arch images." | ||
exit 1 | ||
fi | ||
|
||
# Prepare docker buildx | ||
trap cleanup_all TERM INT EXIT | ||
DIR=`Temp_Folder_Create` | ||
echo New workspace $DIR | ||
echo -e "\n* Prepare docker buildx" | ||
docker buildx use default | ||
docker buildx create --name $BUILDER_NAME --use | ||
docker buildx inspect --bootstrap | ||
|
||
# Check buildx status | ||
echo -e "\n* Check buildx status" | ||
docker buildx ls | grep $BUILDER_NAME | ||
docker ps | grep $BUILDER_NAME | ||
|
||
# Build multi-arch images | ||
docker buildx build --platform linux/amd64,linux/arm64 -t opensearchstaging/ci-runner:${TAG_NAME} -f $DOCKERFILE --push . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/bin/bash | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# This script is to automate the docker single arch image creation process | ||
|
||
set -e | ||
|
||
function usage() { | ||
echo "" | ||
echo "This script is used to build the OpenSearch Docker image with single architecture (x64 or arm64). It prepares the files required by the Dockerfile in a temporary directory, then builds and tags the Docker image." | ||
echo "--------------------------------------------------------------------------" | ||
echo "Usage: $0 [args]" | ||
echo "" | ||
echo "Required arguments:" | ||
echo -e "-v TAG_NAME\tSpecify the image tag name such as 'centos7-x64-arm64-jdkmulti-node10.24.1-cypress6.9.1-20211019'" | ||
echo -e "-f DOCKERFILE\tSpecify the dockerfile full path, e.g. dockerfile/opensearch.al2.dockerfile." | ||
echo "" | ||
echo "Optional arguments:" | ||
echo -e "-h\t\tPrint this message." | ||
echo "--------------------------------------------------------------------------" | ||
} | ||
|
||
while getopts ":hv:f:" arg; do | ||
case $arg in | ||
h) | ||
usage | ||
exit 1 | ||
;; | ||
v) | ||
TAG_NAME=$OPTARG | ||
;; | ||
f) | ||
DOCKERFILE=$OPTARG | ||
;; | ||
:) | ||
echo "-${OPTARG} requires an argument" | ||
usage | ||
exit 1 | ||
;; | ||
?) | ||
echo "Invalid option: -${arg}" | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
# Validate the required parameters to present | ||
if [ -z "$TAG_NAME" ] || [ -z "$DOCKERFILE" ]; then | ||
echo "You must specify '-v TAG_NAME', '-f DOCKERFILE'" | ||
usage | ||
exit 1 | ||
else | ||
echo $TAG_NAME $DOCKERFILE | ||
fi | ||
|
||
# Docker build | ||
docker build -f $DOCKERFILE $DIR -t opensearchstaging/ci-runner:$TAG_NAME . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
docker/ci/dockerfiles/ci-runner.docker-builder.ubuntu2004.x64.dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# This is a docker image specifically for building docker images with single/multi-arch support | ||
# It has binfmt_support package installed to run non-native arch binary, as well as | ||
# qemu-user-static package to enable execution of different multi-arch containers | ||
|
||
# This can only be used on Ubuntu 2004 X64 version, as QEMU 5.0 is required to get buildx work properly without segfault | ||
# https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/1928075 | ||
|
||
FROM ubuntu:20.04 | ||
|
||
# Import necessary repository for installing qemu 5.0 | ||
RUN apt-get update -y && apt-get install -y software-properties-common && add-apt-repository ppa:jacob/virtualisation -y | ||
|
||
# Install necessary packages | ||
RUN apt-get update -y && apt-get upgrade -y && apt-get install -y binfmt-support qemu qemu-user qemu-user-static docker.io curl && apt clean -y | ||
|
||
# Install docker buildx | ||
RUN mkdir -p ~/.docker/cli-plugins && \ | ||
curl -SL https://github.com/docker/buildx/releases/download/v0.6.3/buildx-v0.6.3.linux-amd64 -o ~/.docker/cli-plugins/docker-buildx && \ | ||
chmod 775 ~/.docker/cli-plugins/docker-buildx && \ | ||
docker buildx version | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
pipeline { | ||
options { | ||
timeout(time: 3, unit: 'HOURS') | ||
} | ||
// environment { | ||
// CREDENTIAL_ID = "jenkins-staging-docker-staging-credential" | ||
// } | ||
agent none | ||
stages { | ||
stage('parameters') { | ||
steps { | ||
script { | ||
properties([ | ||
parameters([ | ||
string( | ||
defaultValue: 'https://github.com/opensearch-project/opensearch-build', | ||
name: 'DOCKER_BUILD_GIT_REPOSITORY', | ||
description: 'The git repository name that contains dockerfiles and the docker build script', | ||
trim: true | ||
), | ||
string( | ||
defaultValue: 'main', | ||
name: 'DOCKER_BUILD_GIT_REPOSITORY_REFERENCE', | ||
description: 'The git reference (branch/tag/commit_id) of above repository', | ||
trim: true | ||
), | ||
string( | ||
defaultValue: 'bash docker/ci/build-image-multi-arch.sh -v <TAG_NAME> -f <DOCKERFILE PATH>', | ||
name: 'DOCKER_BUILD_SCRIPT_WITH_COMMANDS', | ||
description: 'The script path of the docker build script, assuming you are already in root dir of DOCKER_BUILD_GIT_REPOSITORY', | ||
trim: true | ||
), | ||
booleanParam( | ||
defaultValue: true, | ||
name: 'IS_STAGING', | ||
description: 'Are we pushing docker images to staging (opensearchstaging) or production (opensearchproject) account' | ||
) | ||
]) | ||
]) | ||
} | ||
} | ||
} | ||
stage('docker-build') { | ||
agent { | ||
docker { | ||
label 'Jenkins-Agent-Ubuntu2004-X64-m52xlarge-Docker-Builder' | ||
image 'opensearchstaging/ci-runner:ubuntu2004-x64-docker-buildx0.6.3-qemu5.0' | ||
args '-u root -v /var/run/docker.sock:/var/run/docker.sock' | ||
alwaysPull true | ||
} | ||
} | ||
steps { | ||
script { | ||
git url: "$DOCKER_BUILD_GIT_REPOSITORY", branch: "$DOCKER_BUILD_GIT_REPOSITORY_REFERENCE" | ||
def CREDENTIAL_ID = "jenkins-staging-docker-staging-credential" | ||
if (env.IS_STAGING == "false"){ | ||
CREDENTIAL_ID = "jenkins-staging-docker-prod-token" | ||
sh "echo Switch to Production" | ||
} | ||
sh "echo Account: $CREDENTIAL_ID" | ||
withCredentials([usernamePassword(credentialsId: CREDENTIAL_ID, usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) { | ||
sh ''' | ||
set -ex | ||
echo Login to $CREDENTIAL_ID | ||
docker logout && docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD && eval $DOCKER_BUILD_SCRIPT_WITH_COMMANDS | ||
''' | ||
} | ||
} | ||
} | ||
post() { | ||
always { | ||
cleanWs disableDeferredWipeout: true, deleteDirs: true | ||
sh "docker logout && docker image prune -f --all" | ||
} | ||
} | ||
} | ||
} | ||
} |