在项目根目录下添加 .gitlab-ci.yml 文件,整个持续集成系统是 Gitlab 自带的,要作的就是添加一个 Runner 到系统里来解析文件中的 script 部分。html
官方提供了安装方法,我的选择的是 install on macOS,按顺序安装:node
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
复制代码
sudo chmod +x /usr/local/bin/gitlab-runner
cd ~
gitlab-runner install
gitlab-runner start
复制代码
在注册 Runner 前须要作一些准备:git
sudo gitlab-runner register
复制代码
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
复制代码
Please enter the gitlab-ci token for this runner
xxx
复制代码
Please enter the gitlab-ci description for this runner
[hostame] my-runner
复制代码
Please enter the gitlab-ci tags for this runner (comma separated):
runner
复制代码
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
docker
复制代码
Please enter the Docker image (eg. ruby:2.1):
alpine:latest
复制代码
Gitlab 同时提供非交互式的注册,一行命令就能够:docker
sudo gitlab-runner register \
--non-interactive \
--url "https://gitlab.com/" \
--registration-token "获取的 token " \
--executor "docker" \
--docker-image alpine:latest \
--description "runner" \
--tag-list "docker,aws" \
--run-untagged \
--locked="false" \
复制代码
注册流程走完会在 ~/etc/gitlab-runner/ 目录下生成 config.toml 配置文件,这时候就能够在 Gitlab 的设置中看到激活的 Runner: shell
在这里只贴上我的配置,详细的资料均可以查到,在这里就不赘述了:npm
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
复制代码
配置文件中创建了两个工程,分别是 build 和 deploy,须要补充的几点:安全
$SERVER_USER_HOST
这样的变量在 Settings -> CI/CD -> Variables 中设置;使用命令行运行 Runner,sudo gitlab-runner run
,启动成功后以下截图: ruby
构建成功后以下截图: bash