services:
- docker:19.03.7-dind
- golang:1.17.8-alpine3.15
- docker:stable
stages:
- package
- build and push docker image
- deploy
variables:
KUBECONFIG: /etc/deploy/config
build:
tags:
- k8s
image: golang:1.17.8-alpine3.15
stage: package
# 只作用在main分支
only:
- main
script:
- export GO111MODULE=on
- export GOPROXY=https://goproxy.cn
- go mod init app
- go mod tidy
- go build .
docker build:
tags:
- k8s
variables:
DOCKER_HOST: tcp://0.0.0.0:2375
DOCKER_TLS_CERTDIR: ""
image: docker:stable
stage: build and push docker image
# 只作用在main分支
only:
- main
script:
- docker build -t test:v01 .
deploy:
tags:
- k8s
image: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/kubectl:1.16.6
stage: deploy
script:
- mkdir -p /etc/deploy
- echo $kube_config |base64 -d > $KUBECONFIG
- kubectl get pods -n gitlab
mkdir -p /etc/deploy
echo $kube_config | base64 -d > $KUBECONFIG
kubectl get pods -n gitlab
也可以使用以下方法,但是可读性脚差
deploy:
tags:
- k8s
image: registry.cn-hangzhou.aliyuncs.com/haoshuwei24/kubectl:1.16.6
stage: deploy
script:
- echo $kube_config | base64 -d > /tmp/config.yaml
- kubectl --kubeconfig /tmp/config.yaml get pods -n gitlab