The result: when a branch has git push operations, it is automatically packaged and published to the server.

This time is only for learning and has not been implemented into specific projects, so it is implemented in the form of demo

First, set up a GITLab

My machine is Windows system, so I install a VMware and Linux system to build gitLab service. Ubuntu images can be downloaded directly from the official website.

  • 1. Install dependencies

sudo apt-get install curl openssh-server ca-certificates postfix
Copy the code

If you want to install Postifx to send mail, select Internet during the installation.

  • 2. Install GITLab CE

    • Online installation
    curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash sudo apt-get install Gitlab - ce = section 13.4.6 - ce. 0Copy the code
    • Offline installation

      在hereYou can install it online or offline. The official installation package is available for direct download. If you useTsinghua image library RPM installation packageBecause ofUbuntuIs based ondebianSo we have to convert todebFormat.

Install with DPKG after downloading deb file

DPKG -i gitlab - ce_13. 4.6 - ce. 0 _amd64. DebCopy the code

Once installed, open it/etc.gitlab/gitlab.rb, modifyexternal_url

Run sudo gitlab-ctl reconfigure to start gitlab, then access 192.168.68.128(this address is the VIRTUAL machine IP address), running on port 80 by default. If you can see the Gitlab page but report 500, it may be that the virtual machine allocated memory is too small, you can appropriately increase. Normal access will require you to set up a super administrator password, set up to go in you can start to create your project.

If the online installation is slow to download, there are two ways to replace the image source

  • graphically

The download source can be modified directly in software and updates

  • Modifying a Configuration File

The configuration file is/etc/apt/sources.list, add the mirror source you want, I use the mirror source of Ali, this isTsinghua mirror source use help, you can also find it directly in the official website of Qinghua MirrorGitlab Community Edition Image usage help

Install Docker

Why Docker? In fact, automatic deployment of Docker is not necessary, but the next Gitlab Runner is run through the way of Docker, the official mention, Gitlab Runner must be installed on a different server from Gitlab, quote:

  • Before registering a runner, you need to first:
  • Install it on a server separate than where GitLab is installed
  • Obtain a token:
    • For a shared runner, have an administrator go to the GitLab Admin Area and click Overview > Runners
    • For a group runner, go to Settings > CI/CD and expand the Runners section
    • For a project-specific runner, go to Settings > CI/CD and expand the Runners section

Here I will directly use the official online installation mode and install Docker on Ubuntu

Install and runsudo docker run hello-worldIf the following information is displayed, the installation is successful

Install and register gitlab-Runner

Please refer to the relevant official documents here

1. Obtain the latest image of GitLab Runner

docker pull gitlab/gitlab-runner:latest
Copy the code

2. Run and register GitLab Runner

sudo docker run -it --name gitlab-runner  \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock  \
gitlab/gitlab-runner:latest register
Copy the code

The last of theregisterIf you have already registered, you don’t need to bring it. When registering, you will be required to fill in gitLab address, project token and so on1). Enter your Gitlab address

2). Enter the project token. Here, I use the gitLab Runner corresponding to the project instead of the shared RunnerSettings->CI/CD->RunnersFound in



3). Enter a description of runner

4). The inputrunnerthetags

5). The way of execution here isdocker, the inputdockerCan be

6).dockerMirroring and Versionalpine:latest

Then look at the GitLab Runners page and see the following

4. Write Gitlab-CI.yML for the project

If you want the project to trigger gitlab-CI when pushed, create a.gitlab-ci.yml in the project root directory. I simply divided it into two steps, build and deploy. .gitlab-ci.yml file contents are as follows:

# Deploy stages: - build-deploy cache: Node-build: image: node:12.19.0 Job only: refs: # -dev-test-master # Execute script: # install dependencies - yarn - registry=https://registry.npm.taobao.org # builder CI = false, -ci =false NPM run build # Download artifacts: Paths: -build # Deploy to NGINX docker-build: Stage: deploy tags: -nodejs # Execute this job only on the specified branch: refs # -dev-test-master - sshpass -p $PASSWORD scp -o StrictHostKeyChecking=no -r ./build/* $CUSTOM_USERNAME@$CUSTOM_IP:/var/www/htmlCopy the code

Whenever there is apushOperation,gitlab runnerWill begin to executebuildanddeployTwo steps can be taken in the corresponding projectPipelinesSee:

I used SCP to copy files directly to the remote server. -p $PASSWORD indicates the PASSWORD of the server. -o StrictHostKeyChecking=no StrictHostKeyChecking is not performed. Otherwise, insufficient permissions will be reported. -r./build/* $CUSTOM_USERNAME@$CUSTOM_IP:/var/ WWW/HTML Copies all files under build to the specified directory. For cache issues, see this article

For security reasons, I configure the password and other private information on Gitlab:

5. Problem points

  • Re-pulling the image for each build causes a slow build

Pull_policy = "if-not-present"Copy the code

Through the vim/SRV/gitlab – runner/config/config. Toml modify the configuration file

  • Treating warnings as errors because process.env.CI = true.

CI needs to be set to false, otherwise warnings will be treated as errors and will not build

  • Permissions caused by the deploy step

You can add read and write permissions to the corresponding folder. Here you can enter the nginx folder

sudo chmod 777 html
Copy the code

Nginx configuration file in /etc/nginx directory nginx.conf

Ubuntu installs Nginx and its default directory structure

The ubuntu Nginx HTML directory is /var/www/html. Centos Nginx HTML directory is /usr/share/nginx/html