golangE Eric chan2 分钟阅读156 阅读 drone部署go项目的一些示例记录
摘要.drone.yml 文件 kind: pipeline type: docker name: default steps: - name: 编译 image: golang:1.16.5-alpine pull: if-not-exist
.drone.yml 文件
kind: pipeline
type: docker
name: default
steps:
- name: 编译
image: golang:1.16.5-alpine
pull: if-not-exists
environment:
GOPROXY: https://mirrors.aliyun.com/goproxy/
commands:
- CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
- name: 构建镜像
image: plugins/docker
pull: if-not-exists
settings:
mirror: https://xxxx.mirror.aliyuncs.com
purge: false
username:
from_secret: docker_user
password:
from_secret: docker_pass
registry: registry.cn-hongkong.aliyuncs.com
repo: registry.cn-hongkong.aliyuncs.com/xxx/drone_test
auto_tag: true
insecure: true
- name: 触发部署
image: plugins/webhook
settings:
urls:
from_secret: webhook_url
debug: true
trigger:
event:
- tag
Dockerfile 文件,注意,文件名必须是 Dockerfile
FROM alpine:latest
LABEL maintainer="eric chan"
RUN echo http://mirrors.aliyun.com/alpine/v3.8/main/ > /etc/apk/repositories
RUN apk update && apk add tzdata ca-certificates bash
RUN rm -rf /etc/localtime && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
WORKDIR /go/drone-test/
COPY ./main /go/drone-test/main
EXPOSE 11080
CMD ./main
main.go 文件,参考用不是必须
package main
import (
"fmt"
"io"
"log"
"net/http"
"time"
)
func main() {
http.HandleFunc("/test", doRequest)
err := http.ListenAndServe(":11080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func doRequest(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Fprintf(w, "service 67 start...")
fmt.Println("time:",time.Now())
var uid string
if r.Method == "GET" {
uid = r.FormValue("uid")
} else if r.Method == "POST" {
uid = r.PostFormValue("uid")
}
io.WriteString(w, "uid = "+uid)
}
go.mod 文件,参考用不是必须
module drone-test
go 1.15