golang项目代码push到gogs上,如何自动编译、打镜像、k8s上运行?
上面的环境,都需要搭建。
测试demo见 git地址
主要是test1/.drone.yml的编写。
from_secret: kube_token,这个kube_token需要在drone页面中配置,
如下:
kind: pipeline
type: docker
name: dev
steps:
- name: build
image: golang
pull: if-not-exists
environment:
GOPROXY: https://goproxy.cn,direct
CGO_ENABLED: 0
GO111MODULE: on
GOOS: linux
GOARCH: amd64
commands:
- go env
- go build -o releases/test1-api ./cmd/test1-api
- sed -i "s/【DRONE_COMMIT:0:8】/${DRONE_COMMIT:0:8}/g" .helm/dev/values.yaml
- ls releases
- name: image
image: plugins/docker
pull: if-not-exists
settings:
registry: 192.168.204.13:50000
repo: 192.168.204.13:50000/moonfddgroup/test1
dockerfile: ./Dockerfile
tags: "${DRONE_COMMIT:0:8}"
insecure: true
- name: deploy
image: "pelotech/drone-helm3"
pull: if-not-exists
settings:
mode: upgrade
chart: ./.helm/dev
vaules_yaml: ./.helm/dev/values.yaml
release: test1
namespace: droneci
kube_api_server: "https://192.168.204.13:6443"
kube_token:
from_secret: kube_token
skip_tls_verify: true
values:
- image.tag=${DRONE_COMMIT:0:8}
trigger:
branch:
- master
- aaa
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
获取kube_token如下:
echo $(k3s kubectl -n kube-system get secret $(k3s kubectl -n kube-system get secret | grep ^kuboard-user | awk '{print $1}') -o go-template='{{.data.token}}' | base64 -d)
- 1
- 2
test1/Dockfile内容就太简单了,只需要打包,其他事情啥都不用干。如下:
FROM scratch
COPY releases/ /bin/
- 1
- 2
其他内容,见git地址。
效果如下: