From 688d04b5604ef750cd958c728194e80de2b31c27 Mon Sep 17 00:00:00 2001 From: Adrien Carpentier Date: Fri, 17 May 2024 16:55:23 +0900 Subject: [PATCH] ci: merge two ci workflows into one and fix SCP target issue --- .../{staging.yml => build_and_deploy.yml} | 9 ++- .github/workflows/prod.yml | 78 ------------------- 2 files changed, 5 insertions(+), 82 deletions(-) rename .github/workflows/{staging.yml => build_and_deploy.yml} (92%) delete mode 100644 .github/workflows/prod.yml diff --git a/.github/workflows/staging.yml b/.github/workflows/build_and_deploy.yml similarity index 92% rename from .github/workflows/staging.yml rename to .github/workflows/build_and_deploy.yml index 28e3661..700ee34 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/build_and_deploy.yml @@ -1,8 +1,9 @@ -name: Deploy to staging when pushing on staging +name: Build and deploy when pushing on staging or main on: push: branches: + - main - staging - feat/github-ci @@ -10,7 +11,7 @@ jobs: deploy-staging: - environment: staging + environment: ${{ github.ref_name == 'main' && 'prod' || 'staging' }} name: Deploy to staging from ${{ github.ref_name }}/${{ github.sha }} @@ -55,7 +56,7 @@ jobs: username: ${{ secrets.CI_DEPLOY_USER }} key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} source: "./${{ env.CI_JOB_ID }}" - target: "/home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}" + target: "/home/${{ secrets.CI_DEPLOY_USER }}" - name: Execute server commands for deploy uses: appleboy/ssh-action@master @@ -66,7 +67,7 @@ jobs: script: | cd /home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }} mkdir -p /data/tchapbot - + export COMPOSE_FILE=/home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}/docker-compose.yml export COMPOSE_PROJECT_NAME=albert-tchapbot diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml deleted file mode 100644 index 7c9e629..0000000 --- a/.github/workflows/prod.yml +++ /dev/null @@ -1,78 +0,0 @@ -name: Deploy to prod when pushing on main - -on: - push: - branches: - - main - -jobs: - - deploy-prod: - - environment: prod - - name: Deploy to prod from ${{ github.ref_name }}/${{ github.sha }} - - runs-on: ubuntu-latest - - steps: - - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Get current CI job ID - env: - GH_TOKEN: ${{ github.token }} - run: | - jobs=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id}}/attempts/${{ github.run_attempt }}/jobs) - job_id=$(echo $jobs | jq -r '.jobs[] | select(.runner_name=="${{ runner.name }}") | .id') - echo "CI_JOB_ID=$job_id" >> $GITHUB_ENV - echo "This CI job ID is ${{ env.CI_JOB_ID }}" - - - name: Install SSH key - uses: shimataro/ssh-key-action@v2 - with: - key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} - name: id_rsa - known_hosts: ${{ secrets.CI_DEPLOY_HOST }} - - - name: Adding server to known hosts - run: ssh-keyscan -H ${{ secrets.CI_DEPLOY_HOST }} >> ~/.ssh/known_hosts - - - name: Prepare deployment files on runner for this CI job ID - run: | - mkdir -p ./${{ env.CI_JOB_ID }} - cp -r ./app ./${{ env.CI_JOB_ID }} - cp ./docker-compose.yml ./${{ env.CI_JOB_ID }}/docker-compose.yml - cp ./Dockerfile ./${{ env.CI_JOB_ID }}/Dockerfile - cp ./pyproject.toml ./${{ env.CI_JOB_ID }}/pyproject.toml - - - name: Copy deployment files to remote server via SCP - uses: appleboy/scp-action@master - with: - host: ${{ secrets.CI_DEPLOY_HOST }} - username: ${{ secrets.CI_DEPLOY_USER }} - key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} - source: "./${{ env.CI_JOB_ID }}" - target: "/home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}" - - - name: Execute server commands for deploy - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.CI_DEPLOY_HOST }} - username: ${{ secrets.CI_DEPLOY_USER }} - key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} - script: | - cd /home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }} - mkdir -p /data/tchapbot - - export COMPOSE_FILE=/home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}/docker-compose.yml - export COMPOSE_PROJECT_NAME=albert-tchapbot - - docker compose down - docker tag albert/tchapbot:latest albert/tchapbot:old || true - docker image rm albert/tchapbot:latest || true - docker compose up --detach - docker image rm albert/tchapbot:old || true - - rm -rf /home/${{ secrets.CI_DEPLOY_USER }}/${{ env.CI_JOB_ID }}