本文内容纲要:
- 喜大奔的go mod
- 环境
- godep
- go mod
- dockerfile
- 减小体积
- 镜像瘦身最终效果
- CI/CD
喜大奔的go mod
官方背书的go mod拯救了我的代码洁癖症!
环境
- go v1.12
- docker ce 18.09.0
- gitlab ce latest
godep
写go程序,若是仅仅是你一个人写,或者就是写个小工具玩儿玩儿,依赖管理对你来说可能没那么重要。
godepvendor
这样的实现方式,每次更新了外部依赖,其他人就得拉下来一大坨。。。
go mod
来看看使用官方的module来管理依赖的工程结构:
是不是,清爽无比,项目也整个瘦身了!
简单的说一下go mod help,至于开启go mod的步骤,其他网文一大堆,就不复制了。毕竟本文是说go工程CI/CD的。
go v1.12go mod help
The commands are:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
download
dockerfile
来看看我这个工程的dockerfile:
FROM golang:1.12 as build
ENV GOPROXY https://go.likeli.top
ENV GO111MODULE on
WORKDIR /go/cache
ADD go.mod .
ADD go.sum .
RUN go mod download
WORKDIR /go/release
ADD . .
RUN GOOS=linux CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o app main.go
FROM scratch as prod
COPY --from=build /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /go/release/app /
COPY --from=build /go/release/conf.yaml /
CMD ["/app"]
go.modgo.sum
在dockerfile的第一步骤中,先启动module模式,且配置代理,因为有些墙外的包服务没有梯子的情况下也是无法下载回来的,这里的代理域名是我自己的,有需要的也可以用。
RUN go mod downloadgo.modgo.sum不用重复的去外网下载依赖
使用缓存构建的效果:
这个加速效果是很明显的。
减小体积
-ldflags="-s -w"
-s -w
-s-w
看起来效果不错?
使用scratch镜像
golang:1.12scratch
最小基础镜像
- scratch: 空的基础镜像,最小的基础镜像
- busybox: 带一些常用的工具,方便调试, 以及它的一些扩展busybox:glibc
- alpine: 另一个常用的基础镜像,带包管理功能,方便下载其它依赖的包
镜像瘦身最终效果
好了,看看最终构建的应用的效果:
16.4MB
CI/CD
.gitlab-ci.yml
before_script:
- if [[ $(whereis docker-compose | wc -l) -eq 0 ]]; then curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose; fi
# ******************************************************************************************************
# ************************************** 测试环境配置 ****************************************************
# ******************************************************************************************************
deploy-test-tour:
stage: deploy
tags:
- build
only:
- release/v2.0
script:
- export PRODUCTION=false
- docker-compose stop
- docker-compose up -d --build
# ******************************************************************************************************
# ************************************** 生产环境配置 ****************************************************
# ******************************************************************************************************
deploy-prod-tour:
stage: deploy
tags:
- release
only:
- master
script:
- export PRODUCTION=true
- docker-compose stop
- docker-compose up -d --build
docker-composebefore_script
我这个项目做了点儿工程化,所以稍微正规点儿,分出了两个环境,测试和生产环境。分别绑定到不同的分支上。
正主就是下面执行的这三行:
export PRODUCTION=false
docker-compose stop
docker-compose up -d --build
exportdocker-compose stopdocker-compose up -d --build
看实际的发布截图:
首次执行,总共:1 minute 22 seconds
使用缓存构建,总共:33 seconds
本文内容总结:喜大奔的go mod,环境,godep,go mod,dockerfile,减小体积,镜像瘦身最终效果,CI/CD,