Docker   发布时间:2019-11-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为容器化的 Go 程序搭建 CI大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

jenkins 的声明式 pipeline 为一个简单的 Golang web 应用搭建 CI 环境。如果你还不太了解 jenkins 及其声明式 pipeline,请先参考笔者的 ,或者直接到 进行学习。说明:本文的演示环境为 ubuntu 16.04。

jenkins 环境

jenkins 环境的搭建。本文演示的 demo 只要求 jenkins server 连接了一个带有 go 标签的 agent,该 agent 上安装了 docker:

title="为容器化的 Go 程序搭建 CI" alt="为容器化的 Go 程序搭建 CI" src="@R_674_10107@s://cn.js-code.com/res/2019/02-09/09/1a03dabe8c566598bd87cb01cbb6641b.png" >

jenkins 邮件通知中的 SMTP server。

下载该程序。

import (
<span style="color: #800000">"<span style="color: #800000">fmt<span style="color: #800000">"
<span style="color: #800000">"<span style="color: #800000">net/http<span style="color: #800000">"
<span style="color: #800000">"<span style="color: #800000">strings<span style="color: #800000">"<span style="color: #000000">
)

func getNameLen(name <span style="color: #0000ff">string) <span style="color: #0000ff">int<span style="color: #000000"> {
<span style="color: #0000ff">return<span style="color: #000000"> len(name)
}

func sayHello(w http.ResponseWriter,r *<span style="color: #000000">http.Request) {
message :=<span style="color: #000000"> r.URL.Path
message = strings.TrimPrefix(message,<span style="color: #800000">"<span style="color: #800000">/<span style="color: #800000">"<span style="color: #000000">)
message = <span style="color: #800000">"<span style="color: #800000">Hello <span style="color: #800000">" + message + <span style="color: #800000">"<span style="color: #800000"> : <span style="color: #800000">" + fmt.Sprintf(<span style="color: #800000">"<span style="color: #800000">%d<span style="color: #800000">"<span style="color: #000000">,getNameLen(message))
w.Write([]<span style="color: #0000ff">byte<span style="color: #000000">(message))
}

func main() {
http.HandleFunc(<span style="color: #800000">"<span style="color: #800000">/<span style="color: #800000">"<span style="color: #000000">,sayHello)
<span style="color: #0000ff">if err := http.ListenAndServe(<span style="color: #800000">"<span style="color: #800000">:8088<span style="color: #800000">",nil); err !=<span style="color: #000000"> nil {
panic(err)
}
}

为容器化的 Go 程序搭建 CI

import (
<span style="color: #800000">"<span style="color: #800000">testing<span style="color: #800000">"<span style="color: #000000">
)

func Test_GetNameLen_1(t *<span style="color: #000000">testing.T) {
<span style="color: #0000ff">if l := getNameLen(<span style="color: #800000">"<span style="color: #800000">nick<span style="color: #800000">"); l != <span style="color: #800080">4<span style="color: #000000"> {
t.Error(<span style="color: #800000">"<span style="color: #800000">test failed,the length of nick is not correct.<span style="color: #800000">"<span style="color: #000000">)
} <span style="color: #0000ff">else<span style="color: #000000"> {
t.Log(<span style="color: #800000">"<span style="color: #800000">test passed.<span style="color: #800000">"<span style="color: #000000">)
}
}

func Test_GetNameLen_2(t *<span style="color: #000000">testing.T) {
<span style="color: #0000ff">if l := getNameLen(<span style="color: #800000">""); l != <span style="color: #800080">0<span style="color: #000000"> {
t.Error(<span style="color: #800000">"<span style="color: #800000">test failed,the length of empty string is not correct.<span style="color: #800000">"<span style="color: #000000">)
} <span style="color: #0000ff">else<span style="color: #000000"> {
t.Log(<span style="color: #800000">"<span style="color: #800000">test passed.<span style="color: #800000">"<span style="color: #000000">)
}
}

golang:1.11.0 CGO_ENABLED=0<span style="color: #008080">FROM<span style="color: #000000"> alpine:latest
<span style="color: #008080">RUN
<span style="color: #000000"> apk --no-cache add ca-certificates
<span style="color: #008080">WORKDIR
<span style="color: #000000"> /root/
<span style="color: #008080">COPY
--from=0<span style="color: #000000"> /go/src/gowebdemo .
<span style="color: #008080">EXPOSE 8088
<span style="color: #008080">CMD ["./gowebdemo"]

( sh(script: ,returnStatus: ) != = ( currentBuild.result == ,body: gopro build failed
Project: ${env.JOB_NAME}
Build Number: ${env.BUILD_NUMBER}
URL de build: ${env.BUILD_URL}
,cc: ,charset : ,: ,mimeType: ,replyTo: ,subject: ${env.JOB_NAME},to: ,body: gopro build success
Project: ${env.JOB_NAME}
Build Number: ${env.BUILD_NUMBER}
URL de build: ${env.BUILD_URL}
,charset: ,subject: ${env.JOB_NAME},to:

#!/bin/set -<span style="color: #000000">x
go get -d -v golang.org/x/net/<span style="color: #000000">html
go get -u github.com/jstemmer/go-junit-<span style="color: #000000">report
go test -v <span style="color: #800080">2>&<span style="color: #800080">1 ><span style="color: #000000"> tmp
status=$?<span style="color: #000000">
$GOPATH/bin/go-junit-report < tmp ><span style="color: #000000"> test_output.xml

exit ${status}

为容器化的 Go 程序搭建 CI

#!/bin/set -<span style="color: #000000">ex
docker build -<span style="color: #000000">t gowebdemo .

remove all none tag images

<span style="color: #0000ff">if [ ! -z <span style="color: #800000">"<span style="color: #800000">$(docker images -q --filter 'dangling=true')<span style="color: #800000">" ]; <span style="color: #0000ff">then<span style="color: #000000">
docker rmi $(docker images -q --filter <span style="color: #800000">"<span style="color: #800000">dangling=true<span style="color: #800000">"<span style="color: #000000">)
<span style="color: #0000ff">fi

#!/bin/set -<span style="color: #000000">ex

remove the current app instance

<span style="color: #0000ff">if [ -n <span style="color: #800000">"<span style="color: #800000">$(docker ps -aq -f name=nickwebdemo)<span style="color: #800000">" ]; <span style="color: #0000ff">then<span style="color: #000000">
docker <span style="color: #0000ff">rm -<span style="color: #000000">f nickwebdemo
<span style="color: #0000ff">fi<span style="color: #000000">

run a new app instance

docker run -<span style="color: #000000">d \
-p <span style="color: #800080">8088:<span style="color: #800080">8088<span style="color: #000000"> \
--<span style="color: #000000">name nickwebdemo \
--restart=<span style="color: #000000">always \
gowebdemo

为容器化的 Go 程序搭建 CI

为容器化的 Go 程序搭建 CI

为容器化的 Go 程序搭建 CI

func Test_GetNameLen_3(t * l := getNameLen(); l !=

为容器化的 Go 程序搭建 CI

为容器化的 Go 程序搭建 CI

大佬总结

以上是大佬教程为你收集整理的为容器化的 Go 程序搭建 CI全部内容,希望文章能够帮你解决为容器化的 Go 程序搭建 CI所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: