Achieve the goal

  • Submission code is automatically built, automatically packaged as a Docker image

preparation

  • Makefile Learn and use makefiles
  • . Gitlab – ci. Yml. Gitlab – ci. Yml is introduced

Install gitlab_runner (docker)

  • Download address
Docker pull gitlab/gitlab-runner #/run/docker. Sock can run the host docker command docker run -d --name gitlab-runner --restart always -v /srv/runner/config:/etc/gitlab-runner -v /run/docker.sock:/var/run/docker.sock gitlab/gitlab-runnerCopy the code

Project directory

  • directory
├─ BuildDockerImage.sh ├─ Dockerfile ├─ Main. Go ├─ MakefileCopy the code
  • . Gitlab – ci. Yml content
# Run before_script before each task: - the export VERSION = ` echo ${CI_COMMIT_TAG} | awk -f "_" '{print $1}' ` # docker installation, due to need inside the container to use host docker command, Here you need to install a Docker executable and then mount the host docker.sock file to the same location in the container when you start the container. - curl -O https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && tar zxvf docker-latest.tgz && cp docker/docker /usr/local/bin/&&rm -rf docker docker-latest. TGZ stages: -build # Define job build: stage: build tags: -test script: -chmod +x *. Sh -make build_image ENV="prod" VERSION=${VERSION} only: - tagsCopy the code
  • The Makefile content
Export VERSION=1.0.0 build_image: @echo "buildDockerImage"./ builddockerimage.sh $(VERSION)Copy the code
  • buildDockerImage.sh
#! /bin/bash echo "GOPATH" if [-z "$GOPATH"]; Else echo "GOPATH=$GOPATH" fi if [! "$1"]; Then echo "Enter the app version" exit 1 fi app_name="app" new_version=$1 echo "Current path $(PWD)" echo" Compile $app_name application "CGO_ENABLED=0 GOOS= Linux go build -a -ldFlags' -extLDFlags "-static"' -o $app_name. Echo "FILE="$app_name" if [-f "$FILE" ]; Then echo "$FILE ready "else echo "$FILE not found" exit 1 fi echo "sudo Docker build-t $app_name:$new_version." docker build -t $app_name:$new_version. Echo "Delete old compiled file" rm -rf $app_nameCopy the code

Registered gitlab_runner

  • Find tokens in the project to build

  • Sign up for a Runner

[pc gitlabRunner]$ docker exec -it gitlab-runner gitlab-ci-multi-runner register Runtime platform arch=amd64 os=linux Pid =5901 Revision = 4C96e5AD version=12.9.0 Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g https://gitlab.com/): http://192.168.2.28/ // Gitlab address Please enter the gitlab-ci token for this runner: 52KL2wic1AwfxffvRrRs // Project Token Please enter the gitlab-CI description for this runner: Please enter the Gitlab-CI tags for this Runner (Comma Separated): Test // Add tags to the current runner. Gitlab-ci. yml, Registering runner... succeeded runner=52KL2wic Please enter the executor: docker-ssh, virtualbox, kubernetes, docker, parallels, shell, ssh, docker+machine, docker-ssh+machine, custom: Docker Please enter the default docker image (e.g. ruby:2.6): Golang :1.14.0 Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!Copy the code
  • Gitlab runner information

Modify the gitlab_runner configuration file in/SRV /runner/config

concurrent = 1 check_interval = 0 [session_server] session_timeout = 1800 [[runners]] name = "test docker" url = "http://192.168.2.28/" token = "XsnZbWbbkwujbAiXoJ9m" executor = "docker" [runners. Custom_build_dir] [runners. [runners. Cache. GCS] [runners. Docker] tls_verify = false image = "Golang :1.14.0" privileged = false disable_entrypoint_overwrite = false oom_kill_disable = false disable_cache = false volumes = ["/run/docker.sock:/var/run/docker.sock", "/cache"] pull_policy = "if-not-present" shm_size = 0Copy the code
  • Volumes: To run the host Docker command in the container.
  • Pull_policy: If the specified image does not exist, it will be pulled by docker pull.

Run the code

Git commit -a -m "test" git push ## Git tag -a "1.2.1" -m "test ci" git push origin 1.2.1Copy the code

The execution result

  • gitlab

  • docker

conclusion

  • There are many kinds of release process, here just provides a simple release mode, in fact, the release method is not necessarily the most powerful and best, to the most suitable for their team is the best
  • Welcome to add QQ to discuss

Contact QQ: 3355168235