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
- 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
- Open permission:
sudo chmod +x /usr/local/bin/gitlab-runner
- Registered Runner
- 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:
- Install Docker and start it
- 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:
- Run the following command:
sudo gitlab-runner register
Copy the code
- 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
- Enter the token used to register Runner:
Please enter the gitlab-ci token for this runner
xxx
Copy the code
- 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
- Enter the label bound to Runner (modifiable) :
Please enter the gitlab-ci tags for this runner (comma separated):
runner
Copy the code
- 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
- 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_HOST
Settings -> 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: