Gitlab Runner

Add the.gitlab-ci.yml file in the root directory of the project. The whole continuous integration system is provided by Gitlab. What you need to do is to add a Runner to the system to parse the script part of the file.

Install Gitlab Runner

Install on macOS install on macOS

  1. Download:
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
Copy the code
  1. Open permission:sudo chmod +x /usr/local/bin/gitlab-runner
  2. Registered Runner
  3. Install and start Runner:
 cd ~
 gitlab-runner install
 gitlab-runner start
Copy the code

There are a few things to do before registering a Runner:

  1. Install Docker and start it
  2. Obtain the Token on the Gitlab interface. The acquisition process is as follows:
  • Log in to Gitlab and go to Settings -> CI/CD -> Runners
  • The obtained Token is shown in the figure below:

To register with Gitlab Runner:

  1. Run the following command:
sudo gitlab-runner register
Copy the code
  1. Enter the URL for the Gitlab instance:
 Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
 https://gitlab.com
Copy the code
  1. Enter the token used to register Runner:
Please enter the gitlab-ci token for this runner
xxx
Copy the code
  1. Enter a description of Runner, which can be modified later in the GitLab interface:
 Please enter the gitlab-ci description for this runner
 [hostame] my-runner
Copy the code
  1. Enter the label bound to Runner (modifiable) :
 Please enter the gitlab-ci tags for this runner (comma separated):
 runner
Copy the code
  1. Select Runner’s execution mode:
 Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
 docker
Copy the code
  1. If you choose to execute in Docker, you will be asked to fill in the default image, which can also be defined in.gitlab-ci.yml:
Please enter the Docker image (eg. Ruby :2.1): alpine:latestCopy the code

Gitlab also provides non-interactive registration with a one-line command:

sudo gitlab-runner register \
  --non-interactive \
  --url "https://gitlab.com/" \
  --registration-token "Acquired token" \
  --executor "docker" \
  --docker-image alpine:latest \
  --description "runner" \
  --tag-list "docker,aws" \
  --run-untagged \
  --locked="false" \
Copy the code

The configuration file config.toml is generated in the ~/etc/gitlab-runner/ directory. You can see the active runner in the gitlab Settings:

Configure the.gitlab-ci.yml file

Here only pasted personal configuration, detailed information can be found, here is not to repeat:

stages:
  - build
  - deploy

build:
  image: node:alpine
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist/
  only:
    - master

deploy_staging:
  image: alpine:latest
  stage: deploy
  before_script:
    - apk update
    - apk add --no-cache rsync openssh
  script:
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa
    - chmod 600 ~/.ssh/id_dsa
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - rsync -rav --delete dist/ "$SERVER_USER_HOST:$SERVER_MASTER_PATH"
  only:
    - master


Copy the code

Two projects are created in the configuration file, called Build and deploy.

  • A new dist file is marked with artifacts in the build project so that it can be reused in other projects.
  • Log in to the server using SSH.
  • Use rsync to perform remote file synchronization.
  • For security purposes, the configuration file is similar$SERVER_USER_HOSTSettings -> CI/CD -> Variables;
  • Remove tags detection in Gitlab Settings:

Run Runner, sudo gitlab-runner run, and the following screenshot is taken after the successful startup:

The following screenshot is taken after the construction is successful: