GitLab CICD Day 30 - 完整代码

时间:2023-02-16 21:01:16
stages:
- testing
- build
- publish
- deploy_to_dev
- deploy_to_staging
- deploy_to_production
- deploy_to_aws

.deploy:
variables:
SERVER_PRIVATE_KEY: ""
SERVER_USER: ""
SERVER_URL: ""
HOME: ""
ENV: ""
APP_PORT: ""

before_script:
- eval $(ssh-agent -s)
- ssh-add <(echo "$SERVER_PRIVATE_KEY")
script:
- scp -o StrictHostKeyChecking=no ./docker-compose.yml $SERVER_USER@$SERVER_URL:$HOME
- ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_URL "
export COMPOSE_PROJECT_NAME=$ENV
export APP_PORT=$APP_PORT
docker compose down &&
docker compose up -d
"

environment:
name: $ENV
url: http://$SERVER_URL:$APP_PORT

run-tests:
stage: testing
image: denoland/deno:latest
script:
- deno test > test-report.txt
artifacts:
paths:
- test-report.txt
expire_in: "30 days"

build-docker-image:
stage: build
tags:
- shell
needs:
- run-tests
before_script:
- export IMAGE_VERSION=$(cat deno.json | jq -r .version)
script:
- docker build -t $CI_REGISTRY_IMAGE:$IMAGE_VERSION-build$CI_PIPELINE_IID .
- docker tag $CI_REGISTRY_IMAGE:$IMAGE_VERSION-build$CI_PIPELINE_IID $CI_REGISTRY_IMAGE:latest
- docker images

push-to-registry:
stage: publish
tags:
- shell
needs:
- build-docker-image
before_script:
- export IMAGE_VERSION=$(cat deno.json | jq -r .version)
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker push $CI_REGISTRY_IMAGE:$IMAGE_VERSION-build$CI_PIPELINE_IID
- docker push $CI_REGISTRY_IMAGE:latest

# Deploy to DigitalOcean(Dev:3000)
deploy-to-dev:
stage: deploy_to_dev
extends: .deploy
variables:
SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
SERVER_USER: $DEV_SERVER_USER
SERVER_URL: $DEV_SERVER_URL
ENV: dev
HOME: /root
APP_PORT: 3000

run-test-on-dev:
stage: deploy_to_staging
script:
- echo "run tests on dev server"

# Deploy to DigitalOcean(Staging:5000)
deploy-to-staging:
stage: deploy_to_staging
needs:
- run-test-on-dev
extends: .deploy
variables:
SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
SERVER_USER: $DEV_SERVER_USER
SERVER_URL: $DEV_SERVER_URL
ENV: staging
HOME: /root
APP_PORT: 5000

run-test-on-staging:
stage: deploy_to_production
script:
- echo "run tests on staging server"

# Deploy to DigitalOcean(Production:80)
deploy-to-do:
stage: deploy_to_production
needs:
- run-test-on-staging
extends: .deploy
variables:
SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
SERVER_USER: $DEV_SERVER_USER
SERVER_URL: $DEV_SERVER_URL
ENV: production-do
HOME: /root
APP_PORT: 80

# Deploy to AWS(Production:80)
deploy_to_aws:
stage: deploy_to_aws
needs:
- run-test-on-staging
before_script:
- cat $AWS_PRIVATE_KEY > private_key.pem
- chmod 400 private_key.pem
script:
- scp -i private_key.pem -o StrictHostKeyChecking=no ./docker-compose.yml $AWS_USER@$AWS_SERVER:/home/ec2-user
- ssh -o StrictHostKeyChecking=no -i private_key.pem $AWS_USER@$AWS_SERVER "
export COMPOSE_PROJECT_NAME=dev
export APP_PORT=80
docker compose down &&
docker compose up -d
"

environment:
name: production-aws
url: http://$AWS_SERVER