2022-03-13:golang项目代码push到gogs上,如何自动编译、打镜像、k8s上运行?
答案2022-03-13:
[2022-02-23:如何搭建k8s单机环境(用k3s),并且搭建dashboard?](
[2022-03-12:k8s如何搭建gogs+drone实现自动化部署cicd,yaml如何写?](
上面的环境,都需要搭建。
测试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:
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: "#34;
kube_token:
from_secret: kube_token
skip_tls_verify: true
values:
- image.tag=${DRONE_COMMIT:0:8}
trigger:
branch:
- master
- aaa
获取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)
test1/Dockfile内容就太简单了,只需要打包,其他事情啥都不用干。如下:
FROM scratch
COPY releases/ /bin/
其他内容,见[git地址](。
效果如下: