本文基於GitLab CI簡介
本文的主線 項目 => 私鑰 => CI/CD => 多Runner
項目
- 這裏以基於Node的React爲例
cnpm i -g create-react-app
create-react-app react-demo
私鑰
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/ci
將~/.ssh/ci.pub內容上傳至目標服務器的~/.ssh/authorized_keys
設置變量http://172.10.66.100/root/react-demo/-/settings/ci_cd => 設置私鑰
CI/CD
vim .gitlab-ci.yml
image: node:12
stages:
- build
- deploy
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules
build:
stage: build
before_script:
- npm install --registry=https://registry.npm.taobao.org
script:
- npm run build
artifacts:
expire_in: 10 days
paths:
- ./build/
deploy_testing:
stage: deploy
environment:
name: testing
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- export HOST_ALIAS=`echo $CI_COMMIT_REF_NAME | sed 's/\(.*\)\/.*/\1/g' | tr '[:upper:]' '[:lower:]'`
- export HOST_IP="192.168.2.23${HOST_ALIAS:3:1}"
- echo "HOST_ALIAS is $HOST_ALIAS, HOST_IP is $HOST_IP"
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- scp -P10133 -r build/* op@${HOST_IP}:/tmp/2yuanlin/
only:
- /^dev.*$/i
構建環境推薦配置>=2C8G
多Runner
vim .gitlab-ci.yml
image: node:12
stages:
- build
- deploy
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules
build_testing:
stage: build
tags:
- testing
before_script:
- npm i --registry=https://registry.npm.taobao.org
script:
- npm run build
artifacts:
expire_in: 10 days
paths:
- ./dist/
only:
- /^dev.*$/i
build_production:
stage: build
tags:
- production
before_script:
- npm i --registry=https://registry.npm.taobao.org
script:
- npm run build
artifacts:
expire_in: 10 days
paths:
- ./dist/
only:
- tags
deploy_testing:
stage: deploy
tags:
- testing
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- export HOST_ALIAS=`echo $CI_COMMIT_REF_NAME | sed 's/\(.*\)\/.*/\1/g' | tr '[:upper:]' '[:lower:]'`
- export HOST_IP="192.168.2.23${HOST_ALIAS:3:1}"
- echo "HOST_ALIAS is $HOST_ALIAS, HOST_IP is $HOST_IP"
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- scp -P10133 -r dist/* op@${HOST_IP}:/tmp/2yuanlin/
only:
- /^dev.*$/i
deploy_production:
stage: deploy
tags:
- production
script:
- echo "Deploy Production"
only:
- tags
- 上述testing和production標籤配置在不同Runner