gitlab ci

gitlab ci requires a .gitlab-ci.yml file in the root of your repository.

REACT and gitlab ci

let me show my .gitlab-ci.yml file

deploy test:
  stage: deploy
  tags:
    - dev-102
  script:
    - cd /home/apollo/test/surge-frontend
    - CI='' git pull
    - git checkout $CI_COMMIT_REF_NAME
    - CI='' git pull
    - CI='' npm run build

deploy prod:
  stage: deploy
  tags:
    - dev-102
  script:
    - cd /home/apollo/surge-frontend
    - git checkout master
    - CI='' git pull
    - CI='' npm run build
  only:
    - master

I have deployed a gitlab runner in the server, and tag the runner with dev-102, yml file config tags with dev-102, so the job will run in the server.
let’s see the deploy test script part, I have clone the repository to the path /home/apollo/test/surge-frontend and config the nginx server this path, so next step is git pull and git checkout $CI_COMMIT_REF_NAME, then npm run build to build the project.
in the ci progress, if command has warning, the job will fail, so I add CI='' before the command, to ignore the warning.

depploy prod have a only config, it will run when we merge branch to master.

gitlab runner

We can deploy gitlab runner with docker:

docker pull gitlab/gitlab-runner:latest
docker network create gitlab-runner-net || true
docker run -d --name gitlab-runner \
  --restart always \
  --network gitlab-runner-net \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

then we need to register the runner:

docker exec -it gitlab-runner gitlab-runner register

it will ask some questions, answer them like this:

  1. gitlab url: https://gitlab.com/
  2. registration token: you can find it in your project page, Settings > CI / CD > Runners > Set up a specific Runner manually
  3. description: dev-102
  4. tags: dev-102
  5. executor: docker
  6. default image: alphine:latest or anything you want

check runner

docker exec -it gitlab-runner gitlab-runner list

# 跟 GitLab 协调器握手验证(失败会打印原因)
docker exec -it gitlab-runner gitlab-runner verify --delete