👷 chore: add shell script for blue-green deployment #16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Baekjoon-Bot CI/CD | |
on: | |
push: | |
branches: [ "main" ] | |
jobs: | |
CI: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Setup Node.js environment | |
uses: actions/setup-node@v3.8.1 | |
with: | |
node-version: "18.x" | |
- name: Install modules | |
run: npm ci | |
- name: Unit test | |
run: npm test | |
- name: Docker login | |
uses: docker/login-action@v3.0.0 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
logout: true | |
- name: Build and push to DockerHub | |
run: | | |
docker build -t ${{ secrets.PROJECT_NAME }} . | |
docker tag ${{ secrets.PROJECT_NAME }}:latest ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest | |
CD: | |
needs: CI | |
runs-on: ubuntu-latest | |
steps: | |
- name: Deploy to EC2 / Check bot status | |
uses: appleboy/ssh-action@v1.0.0 | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_KEY }} | |
script: | | |
echo "${{ secrets.DOCKERHUB_TOKEN }}" | sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin | |
sudo docker rm $(sudo docker stop $(sudo docker ps -a -q --filter ancestor=${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest --format="{{.ID}}")) | |
sudo docker rmi ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest | |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest | |
echo "DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}" > env.list | |
echo "RDS_ENDPOINT=${{ secrets.RDS_ENDPOINT }}" >> env.list | |
echo "RDS_USER=${{ secrets.RDS_USER }}" >> env.list | |
echo "RDS_PASSWORD=${{ secrets.RDS_PASSWORD }}" >> env.list | |
echo "RDS_DB=${{ secrets.RDS_DB }}" >> env.list | |
echo "BASE_URL=${{ secrets.BASE_URL }}" >> env.list | |
echo "TZ=Asia/Seoul" >> env.list | |
sudo docker run -d --env-file env.list --name boj-bot ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest | |
sleep 10s | |
sudo docker ps | grep ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest |