加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为容器化的 Go 程序搭建 CI

发布时间:2020-12-16 03:57:58 所属栏目:安全 来源:网络整理
导读: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:

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)
}
}

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}

#!/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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读