gitlab ci/cd 部署
我的runner executor是采用docker的方式
.gitlab-ci.yml
#定义部署的流程
stages:
- build
- test
- deploy
#定义docker的镜像
image: docker:latest
#定义共用的build脚本
.build-script:
script:
- docker login -u $DOCKER_REGISTRY_USERNAME -p $DOCKER_REGISTRY_PASSWORD $DOCKER_REGISTRY_ADDR
- docker build . -t $DOCKER_IMAGE
- docker push $DOCKER_IMAGE
#定义共用的test脚本
.test-script:
script:
- echo "进行测试工作"
#定义共用的部署脚本
.deployment-script:
#ssh的准备工作
before_script:
- which ssh-agent || (sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update && apk add openssh-client )
- mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 700 ~/.ssh/id_rsa
- eval $(ssh-agent -s)
- ssh-add ~/.ssh/id_rsa
- ssh-keyscan -t rsa $SERVER_IP > ~/.ssh/known_hosts
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
- chmod 644 ~/.ssh/known_hosts
script:
- echo -e "DB_PORT=$DB_PORT\nDB_HOST=$DB_HOST\nDB_USER=$DB_USER\nDB_PASSWORD=$DB_PASSWORD\nHTTP_PORT=$HTTP_PORT\nDOCKER_IMAGE=$DOCKER_IMAGE" > .env
- which rsync || (sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update && apk add rsync )
- rsync -avzP --delete --exclude .git -e 'ssh -i ~/.ssh/id_rsa' . $SERVER_USER@$SERVER_IP:$CODE_PATCH
- ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP "cd $CODE_PATCH && docker-compose pull && docker-compose up -d && docker-compose restart "
#testing
build-testing:
stage: build
environment: testing
extends: .build-script
only:
- develop
test-testing:
stage: test
environment: testing
extends: .test-script
only:
- develop
deply-testing:
stage: deploy
environment: testing
extends: .deployment-script
only:
- develop
#staging
build-staging:
stage: build
environment: staging
extends: .build-script
only:
- release
test-staging:
stage: test
environment: staging
extends: .test-script
only:
- release
deploy-staging:
stage: deploy
environment: staging
extends: .deployment-script
only:
- release
#production
build-production:
stage: build
environment: production
extends: .build-script
only:
- tags
test-production:
stage: test
environment: production
extends: .test-script
only:
- tags
deploy-production:
stage: deploy
environment: production
extends: .deployment-script
only:
- tags
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭