stages:
• build
• update
• deploy
• test
variables:
REGISTRY: harbor.lazypg.com
REGISTRY_IMAGE_TAG: harbor.lazypg.com/sloth/coupons-mgmt:test
cache:
key: modules
paths:
- node_modules/
- dist/
-------安装依赖及编译-------
build:
切换到vue环境的镜像中 在该镜像下完成编译工作
image: ebiven/vue-cli
stage: build
script:
- npm install
- npm run build-dev
只有代码push到test分支时才会触发该步骤
only:
- test
使用tag为test的gitlab-runner去构建
tags:
- test
-------制作镜像并上传到私有镜像仓库-------
update:
切换到docker环境的镜像中 在该镜像下完成编译镜像并推送镜像的工作
image: docker:latest
stage: update
script:
# 登入私有镜像仓库
- docker login -u $HARBOR_USERNAME -p $HARBOR_PASSWORD $REGISTRY
# 根据Dockerfile文件编译image镜像
- docker build -t $REGISTRY_IMAGE_TAG .
# push镜像到私有仓库
- docker push $REGISTRY_IMAGE_TAG
only:
- test
tags:
- test
----------------部署-----------------
deploy:
切换ubuntu作为deploy任务的镜像
image: kroniak/ssh-client
stage: deploy
script:
# Run ssh-agent (inside the build environment)
- eval (ssh-agent -s) ## ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store ## We're using tr to fix line endings which makes ed25519 keys work ## without extra base64 encoding. ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556 ## - echo "SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
# 创建SSH目录并给它正确的权限
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
# 给runner配置ssh登录不验证HostKey
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
# 采用docker-compose的方式部署服务,提前将项目中的docker-compose-ui.yml文件上传到目标服务器
- scp -P SSHTESTSERVERPORT./docker−compose−ui.ymlroot@SSH_TEST_SERVER_IP:/data/docker/
# 使用ssh远程登录目标服务器,并拉取之前build上传好的镜像进行部署
- ssh root@$SSH_TEST_SERVER_IP -p $SSH_TEST_SERVER_PORT "docker-compose -f /data/docker/docker-compose-ui.yml pull && docker-compose -f /data/docker/docker-compose-ui.yml up -d;"
manual表示需要手动触发
when: manual
allow_failure: false
only:
- test
tags:
- test
-------安装依赖及编译-------
test:
切换到vue环境的镜像中 在该镜像下完成编译工作
image: ebiven/vue-cli
stage: build
script:
-
只有代码push到test分支时才会触发该步骤
only:
- test
使用tag为test的gitlab-runner去构建
tags:
- test