drone部署go项目的一些示例记录

118次阅读
没有评论

共计 2429 个字符,预计需要花费 7 分钟才能阅读完成。

.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 # 在 drone 的 secret 里面设置的变量
    password:
      from_secret: docker_pass # 在 drone 的 secret 里面设置的变量
    registry: registry.cn-hongkong.aliyuncs.com
    repo: registry.cn-hongkong.aliyuncs.com/xxx/drone_test
    # tags: ${DRONE_TAG=latest} 如果是自己写webhook的话,可以启用该方式
    auto_tag: true # Portainer 使用该方式比较方便
    insecure: true

- name: 触发部署
  image: plugins/webhook
  settings:
    urls: 
      from_secret: webhook_url # 在 drone 的 secret 里面设置的变量
    debug: true

trigger:
  event:
  - tag # 触发条件,tag增加修改时触发

Dockerfile 文件,注意,文件名必须是 Dockerfile

FROM alpine:latest
LABEL maintainer="eric chan"
###############################################################################
#                                INSTALLATION
###############################################################################

# 使用国内alpine源
# RUN echo http://mirrors.ustc.edu.cn/alpine/v3.8/main/ > /etc/apk/repositories
RUN echo http://mirrors.aliyun.com/alpine/v3.8/main/ > /etc/apk/repositories

# 设置系统时区 - +8时区
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

###############################################################################
#                                   START
###############################################################################
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()       //解析url传递的参数,对于POST则解析响应包的主体(request body)
    //fmt.Println(r.Form) //这些信息是输出到服务器端的打印信息
    //fmt.Println("path", r.URL.Path)
    //fmt.Println("scheme", r.URL.Scheme)
    //for k, v := range r.Form {
    //    fmt.Println("key:", k)
    //    fmt.Println("value:", strings.Join(v, ""))
    //}
    fmt.Fprintf(w, "service 67 start...") //这个写入到w的是输出到客户端的 也可以用下面的 io.WriteString对象
    fmt.Println("time:",time.Now())
    //注意:如果没有调用ParseForm方法,下面无法获取表单的数据
    //query := r.URL.Query()
    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
正文完
 0
Eric chan
版权声明:本站原创文章,由 Eric chan 于2021-06-29发表,共计2429字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。