gitlab-cicd自动化:编译-打包-上传-部署

1.安装gitlab-runner

1.安装下载源

# For Debian/Ubuntu/Mint
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

# For RHEL/CentOS/Fedora
# curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash

2.安装

sudo apt-get install gitlab-runner
systemctl status gitlab-runner

在这里插入图片描述

2.注册runner

2.1找到gitlab上面URL和token

在这里插入图片描述

2.2注册

# 注册
gitlab-runner register

在这里插入图片描述

2.3 注册成功

在这里插入图片描述

3.使用docker方式编译部署(独立环境)

配置免sudo输入密码
配置免密登录

ps -ef|grep gitlab-runner

在这里插入图片描述

在这里插入图片描述

4.编写Dockerfile

在这里插入图片描述

5.编写Makefile

cat >> Makefile << EOF
GO=go
GOFLAG=-a -installsuffix cgo
PROJ=device-manager

# 当前系统的默认go配置
GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)



repo=$(PROJ)
version=$(shell cat ./version/VERSION)
time=$(shell date "+%m/%d/%Y %R %Z")


LDFLAG=-ldflags '-X "$(repo)/version.buildVersion=$(version)" -X "$(repo)/version.buildTime=$(time)"'

#IMAGE_NAME=registry.jiangxingai.com:5000/$(PROJ)
#TAG=x8664_cpu_$(version)
IMAGE_NAME=harbor.jiangxingai.com/library/aiot/$(PROJ)/x8664/others
TAG=$(version)
LATEST=latest

# 调用方法 $(call BUILD_PROC,goos,goarch)
define BUILD_PROC
	CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) $(GO) build $(GOFLAG) $(LDFLAG) -o $(PROJ) $<
endef


.PHONY: build
build: main.go
	$(call BUILD_PROC,$(GOOS),$(GOARCH))

.PHONY: linux-amd64
linux-amd64: main.go
	$(call BUILD_PROC,linux,amd64)

.PHONY: linux-arm64
linux-arm64: main.go
	$(call BUILD_PROC,linux,arm64)

.PHONY: linux-arm
linux-arm: main.go
	$(call BUILD_PROC,linux,arm)

docker-image: check_version
	# docker pull $(IMAGE_NAME):$(LATEST) || true
	docker build -t $(IMAGE_NAME):$(TAG) .
	# docker tag $(IMAGE_NAME):$(TAG) $(IMAGE_NAME):$(LATEST)

docker-push: docker-image
	docker push $(IMAGE_NAME):$(TAG)
	# docker push $(IMAGE_NAME):$(LATEST)

check_version:
ifeq ($(version),)
	$(error No version tag found)
endif

.PHONY: clean
clean:
	go clean

it: build docker-image docker-push


EOF

在这里插入图片描述

6.编写cicd文件

cat >> .gitlab-ci.yml << EOF

image: "golang:latest"

stages:
  - build
  - docker

build:
  stage: build
  script:
    - make build
  artifacts:
    paths:
      - /
  only:
    - develop

docker:
  image: docker:dind
  stage: docker
  script:
    - apk update
    - apk add make
    - apk add openssh-client
    - docker login harbor.jiangxingai.com --username $IOTEDGE_DOCKER_USERNAME --password $IOTEDGE_DOCKER_PASSWORD
    - make docker-push
    - ssh -tt ubuntu@10.51.4.3 "bash /home/ubuntu/Aiot/reload.sh"
  dependencies:
    - build
  only:
    - develop
EOF

在这里插入图片描述

7.reload.sh脚本(放在部署位置)

cat >> reload.sh << EOF

#!/bin/bash
cd /home/jx/Aiot

container_name="aiot-user-center-1.0.0"
image="harbor.jiangxingai.com/library/aiot/user-center/x8664/others"

sudo docker stop $container_name
sudo docker rm $container_name
image_id=$(sudo docker images |grep $image|awk -F ' ' '{print $3}')
if [ -x $image_id ]
then
    echo "===================================================="
    echo "没有获取到镜像id"
    echo "===================================================="
else
    sudo docker rmi $image_id
fi

sudo docker-compose up -d
echo "===================================================="
echo "部署成功"
echo "===================================================="

EOF

在这里插入图片描述

8-多种架构编译和cicd

Makefile

cat >> Makefile << EOF
GO=go
GOFLAG=-a -installsuffix cgo
PROJ=file-storage-proxy

# 当前系统的默认go配置
GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)



repo=$(PROJ)
version=$(shell cat ./version/VERSION)
time=$(shell date "+%m/%d/%Y %R %Z")


LDFLAG=-ldflags '-X "$(repo)/version.buildVersion=$(version)" -X "$(repo)/version.buildTime=$(time)"'

IMAGE_PROJ=aiot/$(PROJ)
# IMAGE_NAME=registry.jiangxingai.com:5000/$(IMAGE_PROJ)
# TAG=x8664_cpu_$(version)
#IMAGE_NAME=harbor.jiangxingai.com/library/$(IMAGE_PROJ)/arm32/others
IMAGE_NAME=harbor.jiangxingai.com/library/$(IMAGE_PROJ)
TAG=$(version)
LATEST=latest

# 调用方法 $(call BUILD_PROC,goos,goarch)
define BUILD_PROC
	CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) $(GO) build $(GOFLAG) $(LDFLAG) -o $(PROJ) $<
endef

define BUILD_IMAGE
	docker pull $(IMAGE_NAME)/$(1)/others:$(LATEST) || true
	docker buildx build -t $(IMAGE_NAME)/$(1)/others:$(TAG) --platform=$(2) -o type=docker .
	docker tag $(IMAGE_NAME)/$(1)/others:$(TAG) $(IMAGE_NAME)/$(1)/others:$(LATEST)
	docker push $(IMAGE_NAME)/$(1)/others:$(TAG)
    docker push $(IMAGE_NAME)/$(1)/others:$(LATEST)
endef

define PUSH_IMAGE
endef

.PHONY: build
build: main.go
	$(call BUILD_PROC,$(GOOS),$(GOARCH))

.PHONY: linux-amd64
linux-amd64: main.go
	$(call BUILD_PROC,linux,amd64)

.PHONY: linux-arm64
linux-arm64: main.go
	$(call BUILD_PROC,linux,arm64)

.PHONY: linux-arm
linux-arm: main.go
	$(call BUILD_PROC,linux,arm)

docker-image-amd64: check_version
	$(call BUILD_IMAGE,x8664,linux/amd64)

docker-image-arm64: check_version
	$(call BUILD_IMAGE,arm64,linux/arm64)

docker-image-arm32: check_version
	$(call BUILD_IMAGE,arm32,linux/arm)


check_version:
ifeq ($(version),)
	$(error No version tag found)
endif

.PHONY: clean
clean:
	go clean

EOF

.gitlab-ci.yml

cat >> .gitlab-ci.yml <<  EOF

image: "golang:latest"

stages:
  - build-amd64
  - docker-amd64
  - build-arm32
  - docker-arm32

build-amd64:
  image: "golang:latest"
  stage: build-amd64
  script:
    - make linux-amd64
  artifacts:
    paths:
      - /
  only:
    - yyl_dev

docker-amd64:
  image: docker:19.03
  stage: docker-amd64
  script:
    - apk add make
    - apk add openssh-client
    - docker login harbor.jiangxingai.com --username $IOTEDGE_DOCKER_USERNAME --password $IOTEDGE_DOCKER_PASSWORD
    - export DOCKER_CLI_EXPERIMENTAL=enabled
    - export DOCKER_BUILDKIT=1
    - docker build --platform=local -o . git://github.com/docker/buildx
    - mkdir -p ~/.docker/cli-plugins && mv buildx ~/.docker/cli-plugins/docker-buildx
    - docker buildx ls
    - make docker-image-amd64
  dependencies:
    - build-amd64
  only:
    - yyl_dev


build-arm32:
  image: "golang:latest"
  stage: build-arm32
  script:
    - make linux-arm
  artifacts:
    paths:
      - /
  only:
    - yyl_dev

docker-arm32:
  image: docker:19.03
  stage: docker-arm32
  script:
    - apk add make
    - apk add openssh-client
    - docker login harbor.jiangxingai.com --username $IOTEDGE_DOCKER_USERNAME --password $IOTEDGE_DOCKER_PASSWORD
    - export DOCKER_CLI_EXPERIMENTAL=enabled
    - export DOCKER_BUILDKIT=1
    - docker build --platform=local -o . git://github.com/docker/buildx
    - mkdir -p ~/.docker/cli-plugins && mv buildx ~/.docker/cli-plugins/docker-buildx
    - make docker-image-arm32
  dependencies:
    - build-arm32
  only:
    - yyl_dev


EOF

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐