[ $IS_DEBUG = "true" ] && echo original parameters=[$@] # project path for being packaged PROJECT_HOME="." # to store log files and other temp cache TMP_DIR="" # bash script log path LOG_FILE="" # which strategy will be used to build the image PACKAGE_STRATEGY="Dockerfile" # or "buildah" # image info. like nginx:v1 IMAGE_INFO="" # build file path, often be a Dockerfile or a bash script BUILD_FILE="" # do you need to push the image to the remote repository PUSH_FLAG=false # do you need to run `mvn clean package` before build image PACKAGE_FLAG=true # do you need to build image BUILD_FLAG=true # username:password CREDS="" # logPrint() { message="$1" echo "$(date +'%F %T') ${message}" >>$LOG_FILE }
show_help() { echo "执行打包&发布镜像" echo "./package-opt.sh [options]..." echo "-p, --project-home 项目根目录,如/home/projects/springboot-demo" echo "-i, --image-info 镜像信息,包含镜像名以及版本,格式同docker,[imageName:version],如nginx:v1" echo "-s, --package-strategy 打包策略接受参数[ Dockerfile(default) | buildah ],是使用Dockerfile的方式,还是使用buildah的方式。如果是Dockerfile方式,项目根目录下需要有Dockerfile文件,如果是buildah方式,则项目根目录下需要有buildah.sh文件" echo "-b, --build-file 打包执行文件路径,Dockerfile或者buildah.sh,如果值为Dockerfile,将默认使用Dockerfile打包策略,否则使用buildah打包策略" echo " --push-flag 是否发布到远程仓库,true or false(default)" echo " --package-flag 是否执行maven打包,true(default) or false" echo " --build-flag 是否执行本地镜像构建,true(default) or false" echo "-r --remote-registry 远程仓库地址,例如:docker://docker.vemic.com/fas-oss。默认为环境变量REMOTE_IMAGE_REGISTRY的值。如果未设置该环境变量,则默认docker://127.0.0.1:5000。" echo " --creds [username[:password]] 远程仓库用户名密码。--creds=username:password" echo "-h, --help 帮助选项。" } # read args ARGS_CMD=$(getopt -o hp:i:s:b:r: --long help,project-home:,image-info:,package-strategy:,build-file:,push-flag:,package-flag:,build-flag:,creds:,remote-registry -n $(basename $0) -- "$@") # echo ARGS_CMD=[$ARGS] [ $? -ne 0 ] && exit 1 # echo formatted parameters=[$@] # dispatch formatted arguments to position arguments($1,$2,...) eval set -- "$ARGS_CMD" while [ -n "$1" ]; do case "$1" in -h | --help) show_help shift 1 exit 1 ;; -p | --project-home) PROJECT_HOME=$2 if [ ! -d $PROJECT_HOME ]; then echo "project home not exist! target: $PROJECT_HOME" shift 2 exit 1 fi shift 2 ;; -i | --image-info) IMAGE_INFO=$2 [ -z "$IMAGE_INFO" ] && echo "please set image info (like: -i nginx:v1)" && exit shift 2 ;; -b | --build-file) BUILD_FILE=$2 if [ ! -f $BUILD_FILE ]; then echo "build file not exist! file path: $BUILD_FILE" exit 1 elif [[ "$(basename $BUILD_FILE)" = "Dockerfile" ]]; then PACKAGE_STRATEGY="Dockerfile" else PACKAGE_STRATEGY="buildah" fi shift 2 ;; -s | --package-strategy) strategy=$2 if [[ $strategy != "Dockerfile" && $strategy != "buildah" ]]; then echo "package strategy requires arg 'Dockerfile' or 'buildah'" && exit 1 fi PACKAGE_STRATEGY=$2 shift 2 ;; --push-flag) if [[ $2 != "true" && $2 != "false" ]]; then echo "--push-flag must be true or false!" && exit 1 fi PUSH_FLAG=$2 shift 2 ;; --package-flag) if [[ $2 != "true" && $2 != "false" ]]; then echo "--package-flag must be true or false!" && exit 1 fi PACKAGE_FLAG=$2 shift 2 ;; --build-flag) if [[ $2 != "true" && $2 != "false" ]]; then echo "--build-flag must be true or false!" && exit 1 fi BUILD_FLAG=$2 shift 2 ;; -r | --remote-registry) if [[ ! "$2" =~ ^docker://.*|^docker-daemon:*|^oci:* ]]; then echo "-r|--remote-registry is empty! please set correct address. see --help for more info." && exit 1 fi REMOTE_REGISTRY=$2 shift 2 ;; --creds) if [[ $2 = "" ]]; then echo "--creds is empty. please set --creds=username:password" && exit 1 fi CREDS=$2 shift 2 ;; \:) echo "argument value missing! $1" show_help && exit 1 ;; \?) echo "unknown argument! $1" show_help exit 1 ;; --) shift break ;; *) echo "error arg! $1" show_help exit 1 ;; esac done # make preparations make_preparations() { echo "make_pre" TMP_DIR="/tmp/oci-package/$(date +%Y%m%d)" mkdir -p $TMP_DIR LOG_FILE="${TMP_DIR}/$(date +%H%M%S).log" touch $LOG_FILE } # mvn operations do_compile() { echo "maven compile work start" if [ -f "$PROJECT_HOME pom.xml" ]; then echo "pom.xml does not exist in the specified directory $PROJECT_HOME" && exit 1 fi echo "target project home is : $PROJECT_HOME" cd $PROJECT_HOME mvn clean package -Dmaven.test.skip=true >>$LOG_FILE 2>&1 if [ $? -ne 0 ]; then echo "mvn clean package failed! Please see $LOG_FILE" && exit 1 else echo "mvn packaged successfully!" fi }
push_image() { echo "start push image..." cmd="buildah push --tls-verify=false" [ -n "$CREDS" ] && cmd="${cmd} --creds=$CREDS" if [[ "$REMOTE_REGISTRY" =~ ^docker://.* ]]; then cmd="${cmd} $IMAGE_INFO $REMOTE_REGISTRY/$IMAGE_INFO" elif [[ "$REMOTE_REGISTRY" =~ ^docker-daemon:* ]]; then cmd="${cmd} $IMAGE_INFO docker-daemon:$IMAGE_INFO" elif [[ "$REMOTE_REGISTRY" =~ ^oci:* ]]; then cmd="${cmd} $IMAGE_INFO $REMOTE_REGISTRY:$IMAGE_INFO" else echo "REMOTE_IMAGE_REGISTRY invalid, please start with 'docker://' or 'docker-daemon:' or 'oci:'. see --help for more info." fi eval $cmd if [ $? -eq 0 ]; then echo "push image successfully!" else echo "push image to $REMOTE_REGISTRY/$IMAGE_INFO failed!" && exit 1 fi }
package_oci_image_for_docker() { cd $PROJECT_HOME if [ -f "$PROJECT_HOME Dockerfile" ]; then echo "Dockerfile does not exist in the specified directory $PROJECT_HOME" exit 1 else cmd='buildah bud --layers '${BUILD_FILE:+"-f $BUILD_FILE"}' -t $IMAGE_INFO .' eval $cmd [ $? -ne 0 ] && echo "build with Dockerfile failed!" && exit 1 fi }
package_oci_image() { cd $PROJECT_HOME if [ -f "$PROJECT_HOME buildah.sh" ]; then echo "buildah.sh does not exist in the specified directory $PROJECT_HOME" exit 1 else source ${BUILD_FILE:-buildah.sh} >$LOG_FILE 2>$LOG_FILE [ $? -ne 0 ] && echo "build with buildah failed!" && exit 1 fi } # execute package do_oci_package() { echo "begin oci package,PACKAGE_STRATEGY=$PACKAGE_STRATEGY" if [ $PACKAGE_STRATEGY = "Dockerfile" ]; then package_oci_image_for_docker elif [ $PACKAGE_STRATEGY = "buildah" ]; then package_oci_image else echo "unknown package strategy: $PACKAGE_STRATEGY" && exit 1 fi }