Created
August 10, 2023 03:49
-
-
Save ilaziness/6d3a3b6408c738a9f257f4a04d73fdfc to your computer and use it in GitHub Desktop.
构建goframe项目docker镜像
This file contains hidden or 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
| #!/bin/bash | |
| set -e | |
| ### | |
| ### build_docker.sh - build docker image | |
| ### | |
| ### Usage: | |
| ### build.sh [projectName] [version] | |
| ### | |
| ### Options: | |
| ### projectName 项目名称,git项目名称一致,会用来做编译后的文件名 | |
| ### version 镜像版本标签,比如0.1.1 1.2.5 | |
| ### Example: | |
| ### ./build_docker.sh stm_user_ser 0.1.0 | |
| ### | |
| projectName=$1 | |
| version=$2 | |
| dockerfile="manifest/docker/Dockerfile" | |
| imgTag="airmart-registry.cn-shanghai.cr.aliyuncs.com/csgo-qiyuan/$projectName:$version" | |
| help() { | |
| sed -rn 's/^### ?//;T;p' "$0" | |
| } | |
| if [[ $# -lt 2 ]] || [[ "$1" == "-h" ]]; then | |
| help | |
| exit 1 | |
| fi | |
| cd "$projectName" | |
| if [ ! -f "main.go" ];then | |
| echo "非服务项目" | |
| exit 1 | |
| fi | |
| # 拉取最新提交 | |
| echo 'git pull...' | |
| git pull | |
| # 编译go可执行文件 | |
| echo "开始编译..." | |
| if [ ! -d ./bin ];then | |
| echo "创建bin目录" | |
| mkdir ./bin | |
| fi | |
| echo "go build -o ./bin/linux_amd64/$projectName main.go" | |
| #go build -o ./bin/linux_amd64/$projectName main.go | |
| gitInfo=$(git log -1 --format="%cd %H" --date=format:"%Y-%m-%d %H:%M:%S") | |
| builtTime=$(date +"%F %X") | |
| builtInfo="{\"builtGit\":\"${gitInfo}\", \"builtTime\":\"${builtTime}\",\"builtVersion\":\"${version}\"}" | |
| infoBase64=$(echo "$builtInfo" | base64) | |
| go build -o ./bin/linux_amd64/"$projectName" -ldflags "-X 'github.com/gogf/gf/v2/os/gbuild.builtInVarStr=${infoBase64}'" main.go | |
| # build docker image | |
| echo "构建镜像..." | |
| if [ ! -f $dockerfile ];then | |
| echo "$dockerfile: 不存在" | |
| exit 1 | |
| fi | |
| cmd="docker build -f $dockerfile -t $imgTag --build-arg PROJECT_NAME=$projectName ." | |
| echo "$cmd" | |
| $cmd | |
| echo "完成。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment