This article focuses on the automatic deployment of simple Django projects through the configuration of GitLab

  1. The route includes gitLab services, which include the Container Registry service
  2. Gitlab-runner, the service performs the tasks configured in gitlab-ci.yml
  3. Finally deploy the target server

Note:

  1. To use the private Container Registry, note that it is added in daemon.json

  1. Docker executor is configured in gitlab-ci.yml, docker in Docker, so you need to configure the following
services:
  - name: Docker: 19.03.12 - dind
    command: ["- insecure - the registry = 192.168.247.191:5005"]
Copy the code

Install gitlab – ce

Docker installation

Uninstall the old version

 sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
Copy the code

Install yum-utils and set up repo

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code

Note that repo is replaced with Ali

Install the docker

sudo yum install -y docker-ce docker-ce-cli containerd.io
Copy the code

Docker acceleration

sudo mkdir -p /etc/docker 
sudo tee /etc/docker/daemon.json <<-'EOF' 
{ 
"registry-mirrors": ["https://7clusxie.mirror.aliyuncs.com"] 
} 
EOF 
sudo systemctl daemon-reload 
sudo systemctl restart docker
Copy the code

Docker compose installation

Sudo curl - L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname - s) - $(uname -m)" - o  /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose docker-compose --versionCopy the code

Gitlab – ce installation

Create the gitlab directory, create docker-comemage. yml in the directory, and fill in the contents

version: '3.5'
services:
  gitlab:
    image: 'gitlab/gitlab-ce:latest'
    container_name: gitlab
    restart: always
    hostname: '192.168.247.191'
    environment:
        GITLAB_OMNIBUS_CONFIG: | external_url 'http://192.168.247.191:8929' gitlab_rails [' gitlab_shell_ssh_port] = 2224 gitlab_rails = [' time_zone '] 'Asia/Shanghai'    ports:
        - '8929:8929'
        - '2224:22'
        - '5005:5005'
    volumes:
        - '$GITLAB_HOME/config:/etc/gitlab'
        - '$GITLAB_HOME/logs:/var/log/gitlab'
        - '$GITLAB_HOME/data:/var/opt/gitlab'
Copy the code

$GITLAB_HOME to modify the configuration, mainly in this directory

Docker-compose up -d starts the service

Install gitlab – runner

Create the gitlab-runner directory, create docker-comemage. yml in the directory, and fill in the contents

version: '3.5'
services:
  gitlab-runner:
    image: gitlab/gitlab-runner:latest
    restart: always
    volumes:
      - '$GITLAB_HOME/config/gitlab-runner:/etc/gitlab-runner'
      - '/var/run/docker.sock:/var/run/docker.sock'
Copy the code

Docker-compose up -d starts the service

Registered runner

The following is an example of registering a designated runner, which only serves the current project

Docker exec - it gitlab - runner_name gitlab - runner register - n \ \ - url http://192.168.247.191:8929/ - registration - token  kysM1xT_j3schppMffpv \ --executor docker \ --description "dj_todo-02" \ --docker-privileged \ --docker-image "Docker/compose: 1.29.2 \" - the docker - pull - policy if - not - the presentCopy the code

Generate a config.yml file similar to the following in the corresponding directory of GITLAB_HOME

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "test"
  url = "http://192.168.247.174:8929/"
  token = "WZKc8dmkY1RrNHjtWrc9"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "Docker/compose: 1.29.2"
    privileged = true # The self-configured mirror address takes effect
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    pull_policy = ["if-not-present"]
Copy the code

Avoid close channel

Ssh-keygen -t rsa # Generate the public and private keys ssh-copy-id -i id_rsa.pub [email protected] #cp the public key to the target server SSH [email protected] testCopy the code

In the CI variable area, configure sensitive information used in pipeline, such as SSH private key

Write. Gitlab – ci. Yml

image:
  name: Docker/compose: 1.29.2
  entrypoint: [""]

services:
  - name: Docker: 19.03.12 - dind
    command: ["- insecure - the registry = 192.168.247.191:5005"]

stages:
  - build
  - deploy

variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  CI_REGISTRY: "192.168.247.191:5005"
  CI_REGISTRY_USER: 'root'
  CI_CI_REGISTRY_GROUP: 'test'
  PROJECT_NAME: 'django_todo'
  IMAGE_NAME: "$CI_REGISTRY/$CI_CI_REGISTRY_GROUP/$PROJECT_NAME"
  REMOTE_SERVER: 192.168247.214.

before_script:
  - apk add --no-cache openssh-client coreutils bash
  - echo "$PASSWORD" | docker login --username $CI_REGISTRY_USER --password-stdin $CI_REGISTRY


# Mirror build and push phase
build:
  stage: build
  script:
    - docker pull $IMAGE_NAME:latest || true
    #- docker pull $IMAGE:nginx || true
    - docker build --tag $PROJECT_NAME:latest .
    - docker tag $PROJECT_NAME:latest  $IMAGE_NAME:latest
    - docker push $IMAGE_NAME:latest

Configure SSH and remote deployment
deploy:
  stage: deploy
  script:
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - cat ~/.ssh/id_rsa
    - chmod 700 ~/.ssh/id_rsa
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/id_rsa
    - ssh-keyscan -p 22222 -H $REMOTE_SERVER >> ~/.ssh/known_hosts
    - ssh -p 22222 root@$REMOTE_SERVER "docker stop $PROJECT_NAME; docker system prune -a -f; echo '$PASSWORD' | docker login --username root --password-stdin $CI_REGISTRY; docker pull $IMAGE_NAME:latest; docker run --name $PROJECT_NAME -d -p 8000:8000 $IMAGE_NAME:latest"
  #only:
    #- feature-test
Copy the code

There are two main stages

  1. Construction phase, mainly using Dockerfile, build image, push
  2. Use the private key, configure the password-free channel, and deploy

Related code to obtain: link:Pan.baidu.com/s/17Pz2Kf6Y…Extraction code: T4v6

conclusion

Gitlab + comes with GitLab Container Registry + Gitlab-Runner to achieve simple CI functions