First, preparation
  • 1.1. Gitlab environment
  • 1.2. Adockerandgitlab-runnerThe cloud server of the environment (used hereCentOS764 a)
  • 1.3. Project code
  • 1.4. Dockerfile
  • 1.5. .gitlab-ci.yml

Two, environment configuration

  • 2.1. Register the Runner server to perform the deployment task for the project
  • 2.2. Cloud server registration runner

Three, submit updates and automatically deployed to the server, test address: http://182.61.57.121:8000

  • 3.1. Commit code to git Master branch
  • 3.2. Wait for the Job task to complete
  • 3.3 Test Results

First, preparation

  1. Gitlab environment (official hosted repository is used for convenience)
  2. Equipped withdockerandgitlab-runnerThe cloud server of the environment (used hereCentOS764 a)
  3. Project code, here I use Golang as the development language, other development languages are the same operation process
  1. Dockerfilefile
# image file
FROM golang:latest
# WeiXiuZhe
MAINTAINER Razil "[email protected]"
# Mirror project path
WORKDIR $GOPATH/src/kun.com/cicd-demo
Copy the current directory code to the image
COPY . $GOPATH/src/kun.com/cicd-demo
RUN go build.
# Exposed port
EXPOSE 8000
# program entry
ENTRYPOINT ["./cicd-demo"]Copy the code
  1. .gitlab-ci.ymlFile,Grammar check
stages:
  - deploy

docker-deploy:
  stage: deploy
  Execute the Job content
  script:
    Generate cicD-Demo image from Dockerfile
    - docker build -t cicd-demo .
    # Delete containers that are already running
    - if [ $(docker ps -aq --filter name= cicd-demo) ]; then docker rm -f cicd-demo; fi
    Start the container with an image and map local port 8000 to container port 8000
    - docker run -d -p 8000: 8000 --name cicd-demo cicd-demo
  tags:
    # Server that executes the Job
    - kun
  only:
    Only the master branch is executed
    - masterCopy the code

Two, environment configuration

1. Register the Runner server that performs the deployment task for the project

Because I have configured it, there is runner at the bottom. The label of Runner is the identification of multiple runners when they perform tasks. I will use the label as in the later deployment processkunRather thantestThe server executes the job. The following shows the configuration process.

2. Cloud server registration runner

Commit the update and automatically deploy to the server

After the runner registers successfully, the git command is used to submit updates to the master branch. If the master branch is modified, the Job will be executed.

At this point, through the linkhttp://182.61.57.121:8000You can see that the server has the code deployed and is accessible.

Extension: Through the practice of GitFlow workflow common operation process, can have a convenient and efficient system continuous integration solution.


Please follow my official account kK Studio for more technical blog posts.