preface

During project development, one or two lines of code are often modified and released to multiple servers such as alpha test site, beta test site and official site. If manual operation is performed, it is necessary to log in to each server to update the code, package the code, restart some services and other operations, which is too tedious and time-consuming. Therefore, gitlab-Runner can be used instead of manual operation. The installation of Gitlab can be referred to “Gitlab with Docker Self-built Code Hosting Platform”.

Install GitLab – Runner

Docker – compose. Yml as follows:

version: '3'
services:
  gitlab-runner:
    image: gitlab/gitlab-runner
    restart: always
    container_name: gitlab-runner
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./config:/etc/gitlab-runner

networks:
  default:
    driver: bridge
Copy the code

Start the application with docker-compose up -d

Register Runner to GitLab server

  1. Check out Runner Registration Token

  2. Run the Docker exec-it gitlab-runner gitlab-runner register to register

  3. Check out GitLab Runner, pictured below. Shared Runner (available for all projects) registered successfully

Deploy the code using Runner

  1. Create a project, here using the Vue CLI to quickly create a front-end project

  2. Configure the project’s environment variables for the.gitlab-ci.yml file. Ansible is used for deployment here. Since the operations performed by each project are basically different, which means that the configuration needs to be carried out according to the project, write the Ansible configuration into the project variable and configure it according to your actual needs

  3. Write the.gitlab-ci.yml file in the root directory of the project. See the official documentation for details

    Variables: COMPILE_IMAGE: node:12.14.0-alpine ANSIBLE_IMAGE: ansible/centos7- Ansible cache: key: "$CI_PROJECT_NAMESPACE" paths: - node_modules stages: - build - deploy build_alpha: only: - /^v\d+\.\d+\.\d+-alpha$/ stage: build image: $COMPILE_IMAGE artifacts: name: "${CI_COMMIT_TAG}" paths: - dist/ script: - npm config set registry https://registry.npm.taobao.org/ - npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ - npm install - npm run build allow_failure: false deploy_alpha: only: - /^v\d+\.\d+\.\d+-alpha$/ stage: deploy image: $ANSIBLE_IMAGE variables: GIT_STRATEGY: none script: - mkdir ~/.ssh/ - echo -e "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - export ANSIBLE_HOST_KEY_CHECKING=False - echo -e "${ANSIBLE_HOSTS}" > ~/hosts - echo -e "${ANSIBLE_PLAYBOOK}" > ~/playbook.yml - cd ./dist - tar zcf .. /dist.tar.gz * - ansible-playbook ~/playbook.yml -i ~/hosts --extra-vars "{'server_name':'alpha_server'}" allow_failure:  falseCopy the code

    Gitlab-ci.yml, package the code and deploy it to the server using Ansible hosts:

    [alpha_server]
    demo.com
    Copy the code

    The playbook. Yml as follows:

    - hosts: '{{ server_name }}'
      remote_user: root
      tasks:
      - name: upload code
        synchronize:
          mode: push
          src: /builds/root/demo/dist.tar.gz
          dest: /tmp/dist.tar.gz
          rsync_opts:
            - "--compress"
      - name: update code
        shell: cd /app && rm -rf * && tar zxf /tmp/dist.tar.gz && chown -R nginx:nginx *
    Copy the code
  4. Test with a Tag (V0.0.1-alpha), as shown in the picture below. The two tasks of packaging and deployment are successfully executed, and the alpha test station can be accessed normally. The automatic deployment is complete.