Brief summary of gitlab-ci

Due to work needs, I need to build a gitlab myself, register Gitlab-runner and then combine the two to deploy a simple project. Now I will briefly summarize some recent operations.

Ready to work

First of all, you need to have two centos servers, one to build gitlab, you can refer to Centos 7 to build a Gitlab server super detailed article to build gitlab, and then we can start gitlab-runner

Common vocabulary explanation

   1- gitlab

      GitLab is an open source application developed with Ruby on Rails, which implements a self-hosted Git project repository, which can access public or private projects through a web interface. It has similar functions to GitHub, able to browse source code, manage defects and comments. Can manage the team's access to the warehouse, it is very easy to browse the submitted version and provide a file history library. Team members can use the built-in simple chat program (Wall) to communicate. It also provides a code snippet collection function that can easily realize code reuse and facilitate searching when needed in the future.

    2- gitlab-ci

   Gitlab-ci  is the abbreviation of GitLab Continuous Integration. Starting from version 8.0 of Gitlab, gitlab has fully integrated Gitlab-CI, and is enabled by default for all projects. As long as the .gitlab-ci.yml file is added to the root directory of the project warehouse, and the Runner is configured, then every merge request (MR) or push will trigger the CI pipeline

    3- gitlab-runner

     Gitlab-runner is the runner of the .gitlab-ci.yml script, and Gitlab-runner is an isolated machine (or virtual machine) built based on the Gitlab-CI API. GitLab Runner does not need to be installed on the same machine as Gitlab, but considering the resource consumption and security issues of GitLab Runner, it is not recommended to install the two on the same machine. Gitlab Runner is divided into two types, Shared runners and Specific runners. Specific runners can only be used by designated projects, and Shared runners can run all projects with the Allow shared runners option enabled.

   4- pipelines

    Pipelines are different tasks defined in different stages in .gitlab-ci.yml. I understand pipelines as pipelines. The pipeline contains multiple stages ( stages ), and each stage contains one or more processes ( jobs ), such as purchasing, assembling, testing, packaging and then selling online, every push or MR Only after passing through the assembly line can the factory be qualified. And .gitlab-ci.yml defines which stages of this pipeline and what to do in each stage.

    5. Badges

    Badges , when Pipelines execution is completed, badges will be generated. You can add these badges to your README.md file or your website

 

Install gitlab-runner

 1- Download the Gitlab-runner corresponding to the system (currently installed version is 11.9.2) 

 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

 2- Add permissions

  After downloading, gitlab-runner defaults to /usr/local/bin

 sudo chmod +x /usr/local/bin/gitlab-runner

 3- Install and enable the service

 

 sudo gitlab-runner install --user=root --working-directory=/home/gitlab-runner
 sudo gitlab-runner start

注意:这里有各参数--user是用户,如果你没有用户或者不用root可以自己创建
 sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

 At this point, our installation has been basically completed, and all that is left is to register gitlab with gitlab-runner.

ps: GitLab-Runner is a tool used to execute .gitlab-ci.yml scripts (in the project). It can be understood that Runner is like a serious worker, and GitLab-CI is the center for managing workers. All workers must register in GitLab-CI and indicate which project they are serving. When the corresponding project changes, GitLab-CI will notify the corresponding worker to execute the corresponding script.

Register Runner

#  执行注册命令
sudo gitlab-runner register

#这里填入你的gitlab地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )

# 这里填入你的token
Please enter the gitlab-ci token for this runner

# 输入一个Runner的description ,可以在稍后的GitLab的UI中更改这个描述
Please enter the gitlab-ci description for this runner

# 输入Runner的tags(这个tags后面会用到)
Please enter the gitlab-ci tags for this runner (comma separated)

# 选择Runner的执行者 我选择 shell
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:



View token

So far, it has been installed and built successfully, now it is used

编写 .gitlab-ci.yml 

# 定义 stages(阶段,会依次执行)
stages:
  - install_deps
  - build_prod
  - unit_test
  - deploy_prod


# 安装构建依赖
install_deps_job:
  stage: install_deps
  # 在哪个分支才会执行脚本
  only:
    # - dev
    # - release
    - master
  script:
    - echo '模拟安装构建依赖阶段'
  tags:
    - my-tag

# 构建预prod环境src目录下应用
build_prod_job:
  stage: build_prod
  only:
    - master
  script:
    - echo '构建预prod环境src目录下应用阶段'
  tags:
    - my-tag
      
# 单元测试
project_unit_test:
  stage: unit_test
  only:
    - master
  script:
    - echo '项目单元测试'
  tags:
    - my-tag

# 部署生产环境
deploy_prod_job:
  stage: deploy_prod
  only:
    - master
  script:
    - echo '部署生产环境阶段'
  tags:
    - my-tag

In addition, you can verify the legality of the .gitlab-ci.yml syntax in CI/CD->pipelines->CI Lint

Then submit it with the project, check the pipeline

In addition, the grammar can be viewed: Gitlab CI yaml official configuration file translation

 

Problems encountered by myself

1- Remember that there is a dot in front of .gitlab-ci.yml and the suffix is ​​yml. This file must be in the root directory, otherwise the task will not be triggered if you submit it

2- If you want a registered runner you write, you need to use it yourself or use it for all projects

 3- pipelie reported insufficient permissions: Permission denied

  Please check the blog: Continuous integration permission error

 Recommended reading:

Introduction to GitLab CI-Getting Started

Gitlab automatic deployment two: install GITLAB-RUNNER

 

Guess you like

Origin blog.csdn.net/lin_keys/article/details/107514182