github 上Go项目使用Travis CI和Docker Hub实现持续集成

网友投稿 235 2022-10-22

github 上Go项目使用Travis CI和Docker Hub实现持续集成

介绍

在本文中,我们将介绍如何使用Github,Travis-CI和Docker Hub创建一个简单的持续集成过程。

项目

项目目录

news_watch_notice├── cmd //main├── conf├── dis├── Dockerfile├── Makefile├── pkg├── qrcode├── .travis.yml├── README.md├── vendor├── utils

Dockerfile

FROM alpine:3.6MAINTAINER hanyajun0123@gmail.comRUN apk update && apk add curl bash tree tzdata \ && cp -r -f /usr/share/zoneinfo/Hongkong /etc/localtimeADD news_watch_notice /usr/bin/ADD news_watch_notice.sha /usr/bin/CMD ["news_watch_notice"]

makefile

TARGET=news_watch_noticePKG=$(TARGET)TAG=latestIMAGE_PREFIX?=hanyajunIMAGE_PREFIX_PRD=hanyajunTARGET_IMAGE_DEV=$(IMAGE_PREFIX)/$(TARGET):$(TAG)TARGET_IMAGE_PRD=$(IMAGE_PREFIX_PRD)/$(TARGET):$(TAG)all: image$(TARGET): CGO_ENABLED=0 go build -o dist/$(TARGET) $(PKG)/cmdgitlog:target: mkdir -p dist git log | head -n 1 > dist/news_watch_notice.sha docker run --rm -i -v `pwd`:/go/src/$(PKG) \ -w /go/src/$(PKG) golang:1.11.5 \ make $(TARGET)image-dev: target cd dist && cp ../Dockerfile ./ && \ docker build -t $(TARGET_IMAGE_DEV) .push-dev: docker push $(TARGET_IMAGE_DEV)image-prd: target cd dist && cp ../Dockerfile ./ && \ docker build -t $(TARGET_IMAGE_PRD) .push-prd: docker push $(TARGET_IMAGE_PRD)clean: rm -rf dist.PHONY: image target clean push $(TARGET)

.travis.yml

language: gogo: # 语言版本号 - "1.11.5" # 默认使用最新版本,注意,需要 "1.10" 版本的时候必须表示为字符串形式,如果写成 1.10 则会使用 1.1 版本;x表示对应前缀的最新版本services: - docker #需要的docker环境install: - make image-dev #buildscript: - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin - make push-dev # push

travis的配置(​​hub的用户和密码环境变量(push需要登录认证)

DOCKER_PASSWORDDOCKER_USERNAME

提交项目代码触发ci

部分ci日志

docker_mtudocker stop/waitingdocker start/running, process 3638services0.02s$ sudo service docker startstart: Job is already running: dockerUpdating gimmegit.checkout0.85s$ git clone --depth=50 --branch=master Han-Ya-Jun/news_watch_noticeCloning into 'Han-Ya-Jun/news_watch_notice'...$ cd Han-Ya-Jun/news_watch_notice$ git checkout -qf e05633a1c6e0c3289c85aedf28461b718fe1af13$ travis_export_go 1.11.5 github.com/Han-Ya-Jun/news_watch_noticeSetting environment variables from repository settings$ export DOCKER_PASSWORD=[secure]$ export DOCKER_USERNAME=[secure]$ export GOBIN=$GOPATH/src/bin6.93s$ travis_setup_gogo version go1.11.5 linux/amd64$ export GOPATH="/home/travis/gopath"$ export PATH="/home/travis/gopath/bin:/home/travis/.gimme/versions/go1.11.5.linux.amd64/bin:/home/travis/bin:/home/travis/bin:/home/travis/.local/bin:/opt/pyenv/shims:/home/travis/.phpenv/shims:/home/travis/perl5/perlbrew/bin:/home/travis/.nvm/versions/node/v8.9.1/bin:/home/travis/.kiex/elixirs/elixir-1.4.5/bin:/home/travis/.kiex/bin:/home/travis/.rvm/gems/ruby-2.4.1/bin:/home/travis/.rvm/gems/ruby-2.4.1@global/bin:/home/travis/.rvm/rubies/ruby-2.4.1/bin:/home/travis/gopath/bin:/home/travis/.gimme/versions/go1.7.4.linux.amd64/bin:/usr/local/phantomjs/bin:/usr/local/phantomjs:/usr/local/neo4j-3.2.7/bin:/usr/local/maven-3.5.2/bin:/usr/local/cmake-3.9.2/bin:/usr/local/clang-5.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/travis/.rvm/bin:/home/travis/.phpenv/bin:/opt/pyenv/bin:/home/travis/.yarn/bin"$ export GO111MODULE="auto"$ gimme versionv1.5.3$ go versiongo version go1.11.5 linux/amd64go.env$ go envGOARCH="amd64"GOBIN="/home/travis/gopath/src/bin"GOCACHE="/home/travis/.cache/go-build"GOEXE=""GOFLAGS=""GOHOSTARCH="amd64"GOHOSTOS="linux"GOOS="linux"GOPATH="/home/travis/gopath"GOPROXY=""GORACE=""GOROOT="/home/travis/.gimme/versions/go1.11.5.linux.amd64"GOTMPDIR=""GOTOOLDIR="/home/travis/.gimme/versions/go1.11.5.linux.amd64/pkg/tool/linux_amd64"GCCGO="gccgo"CC="gcc"CXX="g++"CGO_ENABLED="1"GOMOD=""CGO_CFLAGS="-g -O2"CGO_CPPFLAGS=""CGO_CXXFLAGS="-g -O2"CGO_FFLAGS="-g -O2"CGO_LDFLAGS="-g -O2"PKG_CONFIG="pkg-config"GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build083711362=/tmp/go-build -gno-record-gcc-switches"install43.66s$ make image-devmkdir -p distgit log | head -n 1 > dist/news_watch_notice.shadocker run --rm -i -v `pwd`:/go/src/news_watch_notice \ -w /go/src/news_watch_notice golang:1.11.5 \ make news_watch_noticeUnable to find image 'golang:1.11.5' locally1.11.5: Pulling from library/golang22dbe790f715: Pulling fs layer0250231711a0: Pulling fs layer6fba9447437b: Pulling fs layerc2b4d327b352: Pulling fs layer619f4932b7ea: Pulling fs layere2fd6cbd3c6f: Pulling fs layer1d96446d2b20: Pulling fs layerc2b4d327b352: Waitinge2fd6cbd3c6f: Waiting1d96446d2b20: Waiting619f4932b7ea: Waiting6fba9447437b: Verifying Checksum6fba9447437b: Download complete0250231711a0: Download complete22dbe790f715: Verifying Checksum22dbe790f715: Download completec2b4d327b352: Verifying Checksumc2b4d327b352: Download complete619f4932b7ea: Verifying Checksum619f4932b7ea: Download complete1d96446d2b20: Verifying Checksum1d96446d2b20: Download completee2fd6cbd3c6f: Verifying Checksume2fd6cbd3c6f: Download complete22dbe790f715: Pull complete0250231711a0: Pull complete6fba9447437b: Pull completec2b4d327b352: Pull complete619f4932b7ea: Pull completee2fd6cbd3c6f: Pull complete1d96446d2b20: Pull completeDigest: sha256:342872745847966ab630e9cc5b6980ed3fe7f4e3ea44b10efac995575730c9b8Status: Downloaded newer image for golang:1.11.5CGO_ENABLED=0 go build -o dist/news_watch_notice news_watch_notice/cmdcd dist && cp ../Dockerfile ./ && \ docker build -t [secure]/news_watch_notice:latest .Sending build context to Docker daemon 14.78MBStep 1/6 : FROM alpine:3.63.6: Pulling from library/alpine5a3ea8efae5d: Pulling fs layer5a3ea8efae5d: Verifying Checksum5a3ea8efae5d: Download complete5a3ea8efae5d: Pull completeDigest: sha256:ee0c0e7b6b20b175f5ffb1bbd48b41d94891b0b1074f2721acb008aafdf25417Status: Downloaded newer image for alpine:3.6 ---> 43773d1dba76Step 2/6 : MAINTAINER [secure]0123@gmail.com ---> Running in 1f4d4d0db1cb ---> 0eadcccd5d22Removing intermediate container 1f4d4d0db1cbStep 3/6 : RUN apk update && apk add curl bash tree tzdata && cp -r -f /usr/share/zoneinfo/Hongkong /etc/localtime ---> Running in 7f859dd06afcfetch [[8442 distinct packages available(1/11) Installing ncurses-terminfo-base (6.0_p20171125-r1)(2/11) Installing ncurses-terminfo (6.0_p20171125-r1)(3/11) Installing ncurses-libs (6.0_p20171125-r1)(4/11) Installing readline (6.3.008-r5)(5/11) Installing bash (4.3.48-r1)Executing bash-4.3.48-r1.post-install(6/11) Installing ca-certificates (20161130-r2)(7/11) Installing libssh2 (1.8.2-r0)(8/11) Installing libcurl (7.61.1-r2)(9/11) Installing curl (7.61.1-r2)(10/11) Installing tree (1.7.0-r0)(11/11) Installing tzdata (2019a-r0)Executing busybox-1.26.2-r11.triggerExecuting ca-certificates-20161130-r2.triggerOK: 17 MiB in 24 packages ---> f923b8dbfbf2Removing intermediate container 7f859dd06afcStep 4/6 : ADD news_watch_notice /usr/bin/ ---> a855f5307179Step 5/6 : ADD news_watch_notice.sha /usr/bin/ ---> 9014ba35ce0fStep 6/6 : CMD news_watch_notice ---> Running in 109449b18721 ---> acb6623feb46Removing intermediate container 109449b18721Successfully built acb6623feb46Successfully tagged [secure]/news_watch_notice:latest0.49s$ echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdinLogin SucceededThe command "echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin" exited with 0.5.21s$ make push-devdocker push [secure]/news_watch_notice:latestThe push refers to a repository [docker.io/[secure]/news_watch_notice]c0546ad83870: Preparing869b27ab7e53: Preparing47b50a8202a0: Preparing721384ec99e5: Preparing721384ec99e5: Layer already existsc0546ad83870: Pushed47b50a8202a0: Pushed869b27ab7e53: Pushedlatest: digest: sha256:74a706f3f8b5b2ead4a7722ca72d8f1eaa450801847662402e4e3e34f4d73158 size: 1157The command "make push-dev" exited with 0.

成功将项目打包成image并推送到docker hub上

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用go mod的go程序如何写Dockerfile
下一篇:解决Spring Security的权限配置不生效问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~