[GIT] Based on gitlab-ci.yml release front end Engineering

be based on. gitlab-ci.yml release front end Engineering

Date : 2021.06.11
Author: jwensh

Key words: gitlab ci/cd

1. Demand

  1. Use gitlab to manage the engineering code, and use gitlab Ci to publish the engineering code to the test or development environment
  2. The trigger condition can trigger the publishing operation according to the commit, branch or tag of the push
  3. Appeal operation completed from 0 to 1

2. Practice

1. Add for the project gitlab-ci.yml file

Note: gitlab-ci.yml is a YAML file, so you must pay special attention to indentation. Always use spaces instead of tabs.

  • The point to note here is, how do you want to build your pipeline? What steps and operations are there
  • For this I recorded: I want to execute
    • 1. Download the master code and install the dependency
    • 2. Package and generate deployable files
    • 3. Deploy the code to the designated place
    • 4. After successful execution, delete the packaged file
  • In fact, the above execution can be realized by shell script
  • Want to know the specific gitlab-ci.yml content details can be viewed Official documents , or gitlab help( http://your.site.com/help/ci/yaml/README.md )Help documentation

Special reminder: the built-in variables of gitlab will vary according to different versions. Please check the specific gitlab help document

stages:
  - install
  - build
  - deploy
  - cleanup

install:
  stage: install
  script:
    - echo $CI_BUILD_REF
    - git log -1 --format=%s
    - echo $CI_PROJECT_DIR
    - cnpm install
  only:
    - /^v[0-9]/
  environment:
    name: test-192
  tags:
    - onetest

package:
  stage: build
  script:
    - cnpm run build:prod
  only:
    - /^v[0-9]/
  tags:
    - onetest
  environment:
    name: test-192

deploy:
  stage: deploy
  script:
    - sh ./build/deploy-192.sh $CI_PROJECT_DIR // This one can complete four operations
  only:
    - master
    - /^v[0-9]/
  tags:
    - onetest
  environment:
    name: test-192

cleanup:
  stage: cleanup
  script:
    - rm -rf ./dist
  only:
    - /^v[0-9]/
  tags:
    - onetest
  when: on_success
  environment:
    name: test-192
  • Under the popular interpretation, I think gitlab CI The understanding of YML document is to define an activity. The process of this activity is controlled by stage s, and then there are four hosts in each link (i.e. install, package, deploy and cleanup). In order to complete this activity successfully, many people sign up to participate in tribute programs (jobs), and the activity stipulates the content and form of the program, When and who (only or except and when)? What are you doing? With that host? In which venue do you perform (tags)?

2. Add a gitlab runner for the project

  • What is gitlab runner? You can have a look Official documents Understand its principle deeply. If jenkins is used, it is not difficult to understand its function

  • Look at the picture below

  • To download the appropriate package for your system:

  1. stay gitlab-runner Find the latest file name and options.
  2. Select a version and download the binary, such as Download any other tagged versions For the latest version of GitLab Runner.
  • For example, for Debian or Ubuntu:

    # Replace ${arch} with any of the supported architectures, e.g. amd64, arm, arm64
    # A full list of architectures can be found here https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
    curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_${arch}.deb"
    
  • For example, for CentOS or Red Hat Enterprise Linux:

    # Replace ${arch} with any of the supported architectures, e.g. amd64, arm, arm64
    # A full list of architectures can be found here https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
    curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_${arch}.rpm"
    
  • LSB can be used_ Release - A to view the information of the current machine

  • install

  1. Install the software package for your system, as shown below.

    For example, for Debian or Ubuntu:

    dpkg -i gitlab-runner_<arch>.deb
    

    For example, for CentOS or Red Hat Enterprise Linux:

    rpm -i gitlab-runner_<arch>.rpm
    
  2. Register a runner

    stay Linux Register runners below:
    
    Run the following command:
    
    sudo gitlab-runner register
    
    Enter your GitLab example URL(also known as gitlab-ci coordinator URL). 
    Enter the token you obtained to register a runner.
    Enter a description of the runner. You can later GitLab Change this value in the user interface.
    Input and runner Associated labels, separated by commas. You can later GitLab Change this value in the user interface.
    provide runner executor. For most use cases, enter docker. 
    If you docker As an executor input, you will be asked to provide information for.gitlab-ci.yml.
    
    
    [@gd_63 tools]# curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm"
    total 138356
    -rw-r--r-- 1 root root 141657052 Jun  8 14:18 gitlab-runner_amd64.rpm
    (base) [@gd_63_192 tools]# sudo gitlab-runner register
    Runtime platform                                    arch=amd64 os=linux pid=4036 revision=7a6612da version=13.12.0
    Running in system-mode.
    
    Enter the GitLab instance URL (for example, https://gitlab.com/):
    https://git.***.com/
    Enter the registration token:
    novx3wu2atuU3NxRAi**
    Enter a description for the runner:
    [gd_63_192]: TM_onetest_frontend
    Enter tags for the runner (comma-separated):
    onetest
    Registering runner... succeeded                     runner=novx3wu2
    Enter an executor: virtualbox, kubernetes, custom, docker, shell, ssh, docker+machine, docker-ssh+machine, docker-ssh, parallels:
    docker
    Enter the default Docker image (for example, ruby:2.6):
    
    Enter the default Docker image (for example, ruby:2.6):
    ruby:2.6
    Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
    
    Default installation gitlab-runner
    
    catalogue: /home/gitlab-runner
     configuration file: /etc/gitlab-runner/config.toml 
    Configuration reference: https://docs.gitlab.com/runner/executors/README.html
    
    have access to gitlab-runner Command to operate this agent
    
  • After executing the above, you can see the corresponding runner on the settings/ci cd page under the gitlab project

3. Configure packaging and deployment related dependencies for the machine where the runner is located (omitted)

  • install node.js , cnpm (pay attention to version)

    npm install cnpm -g --registry=https://registry.npm.taobao.org
    
  • Install nginx (configure nginx.conf)

4. Test

  • The local code is submitted through branch or tag. If it meets the regular conditions, ci will be triggered

3. Problems encountered

  • I don't know what are the preset public variables of the script: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

  • What are the built-in environment variables https://docs.gitlab.com/ee/ci/variables/README.html#syntax-of-environment-variables-in-job-scripts

  • The possible reasons for the fatal: reference is not a tree problem in gitlab CI are:

    • The currently executed branch or tag is deleted
    • The current branch and tag have the same name
    • The configuration of only and expert in the yml file conflicts
  • After git push, it is found that the build cannot succeed. Is there a problem with the nodejs version of Need to download and install again

    Running with gitlab-runner 13.12.0 (7a6612da)
      on TM 8080f1e4
    Preparing the "shell" executor
    Using Shell executor...
    Preparing environment
    Running on gd_63...
    Getting source from Git repository
    Fetching changes...
    rm: cannot remove '/home/gitlab-runner/builds/8080f1e4/0/test_mm/TM/.git/hooks/post-checkout': Permission denied
    Cleaning up file based variables
    ERROR: Job failed: exit status 1
    
  • No directory permission: because the runner uses the gitlab runner user, if the whole ci is designed to operate the file directory with other permissions, an error will be reported, so the corresponding file or directory under chmod is required

Keywords: GitLab

Added by depraved on Tue, 01 Feb 2022 00:26:24 +0200