Skip to content

Commit

Permalink
Merge pull request #115 from Dapp-Learning-DAO/feature/prepare-prod-env
Browse files Browse the repository at this point in the history
Add deploy support in Prod env
  • Loading branch information
yanyanho authored Jan 12, 2024
2 parents b724da4 + ded52d4 commit 2ff0000
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
66 changes: 44 additions & 22 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ name: Deploy Official-website-backend to dev/prod Server
on:
push:
branches:
- main
- dev
tags:
- 'v*.*.*'

jobs:
deployment:
Expand All @@ -14,58 +15,79 @@ jobs:
env:
DOCKER_USERNAME: ${{ vars.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_IMAGE_VERSION: dev-latest
DOCKER_IMAGE_NAME: dl-official-website-backend

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set environment variables based on branch
- name: Set environment variables based on branch or tag
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "DOCKER_IMAGE_VERSION=main-latest" >> $GITHUB_ENV
echo "BRANCH_NAME=main" >> $GITHUB_ENV
elif [[ "${{ github.ref }}" == "refs/heads/dev" ]]; then
if [[ "${{ github.ref }}" == "refs/heads/dev" ]]; then
echo "DOCKER_IMAGE_VERSION=dev-latest" >> $GITHUB_ENV
echo "BRANCH_NAME=dev" >> $GITHUB_ENV
echo "RUN_ENV=dev" >> $GITHUB_ENV
echo "BRANCH_OR_TAG_NAME=dev" >> $GITHUB_ENV
else
echo "Build production release for tag ${{ github.ref }}"
TAG_NAME=${GITHUB_REF#refs/*/}
echo "RUN_ENV=prod" >> $GITHUB_ENV
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_ENV
echo "DOCKER_IMAGE_VERSION=${TAG_NAME}" >> $GITHUB_ENV
echo "BRANCH_OR_TAG_NAME=${TAG_NAME}" >> $GITHUB_ENV
fi
- name: Display current branch
run: |
echo "Current branch is $BRANCH_NAME"
echo "Current build is on $BRANCH_OR_TAG_NAME"
- name: Build Docker image
run: |
docker build -t $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_VERSION .
docker tag $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_VERSION $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:${RUN_ENV}-latest
- name: Push Docker image to registry
run: |
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker push $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_VERSION
docker push $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:${RUN_ENV}-latest
- name: SSH login to server and restart service on DEV server
uses: appleboy/ssh-action@master
if: contains(github.ref, 'dev')
env:
DOCKER_USERNAME: ${{env.DOCKER_USERNAME}}
DOCKER_IMAGE_NAME: ${{env.DOCKER_IMAGE_NAME}}
DOCKER_IMAGE_VERSION: ${{env.DOCKER_IMAGE_VERSION}}
RUN_ENV: ${{env.RUN_ENV}}
with:
host: ${{ vars.DEV_SERVER_HOST }} #服务器ip地址
username: ${{ vars.DEV_SERVER_USERNAME }} #服务器ssh用户名
key: ${{ secrets.DEV_SSH_PRIVATE_KEY }} #服务器ssh私钥
port: ${{ vars.DEV_PORT }} # 服务器ssh端口(默认22)
host: ${{ vars.DEV_SERVER_HOST }}
username: ${{ vars.DEV_SERVER_USERNAME }}
key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
port: ${{ vars.DEV_PORT }}
envs: DOCKER_USERNAME,DOCKER_IMAGE_NAME,DOCKER_IMAGE_VERSION,RUN_ENV
script: |
cd /root/Official-website-backend
docker compose pull
docker pull $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_VERSION
docker pull $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$RUN_ENV-latest
docker-compose down && docker-compose up -d
- name: SSH login to server and restart service on PROD server
uses: appleboy/ssh-action@master
if: contains(github.ref, 'main')
if: startsWith(github.ref, 'refs/tags/v')
env:
DOCKER_USERNAME: ${{env.DOCKER_USERNAME}}
DOCKER_IMAGE_NAME: ${{env.DOCKER_IMAGE_NAME}}
DOCKER_IMAGE_VERSION: ${{env.DOCKER_IMAGE_VERSION}}
RUN_ENV: ${{env.RUN_ENV}}
with:
host: ${{ vars.PROD_SERVER_HOST }} #服务器ip地址
username: ${{ vars.PROD_SERVER_USERNAME }} #服务器ssh用户名
key: ${{ secrets.PROD_SSH_PRIVATE_KEY }} #服务器ssh私钥
port: ${{ vars.PROD_PORT }} # 服务器ssh端口(默认22)
host: ${{ vars.PROD_SERVER_HOST }}
username: ${{ vars.PROD_SERVER_USERNAME }}
key: ${{ secrets.PROD_SSH_PRIVATE_KEY }}
port: ${{ vars.PROD_PORT }}
envs: DOCKER_USERNAME,DOCKER_IMAGE_NAME,DOCKER_IMAGE_VERSION,RUN_ENV
script: |
cd /root/Official-website-backend
docker compose pull
docker-compose down && docker-compose up -d
cd /data/Official-website-backend
docker pull $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_VERSION
docker pull $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:$RUN_ENV-latest
echo "Exec docker-compose down && docker-compose up -d in $(pwd) on $(date +'%Y-%m-%d %H:%M:%S')" > deploy.txt
# docker-compose down && docker-compose up -d
4 changes: 3 additions & 1 deletion deploy/docker-compose-dependency.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
version: '3.5'
services:
db:
image: mysql:latest
image: mysql:8.0
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_DATABASE=dl
- MYSQL_ROOT_PASSWORD=root
Expand Down

0 comments on commit 2ff0000

Please sign in to comment.