From 8f58d1eaa4c8e295b21245e2171b58cc340b24a2 Mon Sep 17 00:00:00 2001 From: Adrien Carpentier Date: Fri, 26 Apr 2024 15:48:51 +0200 Subject: [PATCH] feat: remove gitlab CI and create github CI --- .github/workflows/prod.yml | 66 +++++++++++++++++++++++++++++++++++ .github/workflows/staging.yml | 66 +++++++++++++++++++++++++++++++++++ .gitlab-ci.yml | 51 --------------------------- 3 files changed, 132 insertions(+), 51 deletions(-) create mode 100644 .github/workflows/prod.yml create mode 100644 .github/workflows/staging.yml delete mode 100644 .gitlab-ci.yml diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml new file mode 100644 index 0000000..3c9ff14 --- /dev/null +++ b/.github/workflows/prod.yml @@ -0,0 +1,66 @@ +name: Deploy to prod when pushing on main + +on: + push: + branches: + - main + +jobs: + + deploy-prod: + + name: Deploy to staging from ${{ github.ref_name }}/${{ github.sha }} + + runs-on: ubuntu-latest + + steps: + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Get GitHub Actions job ID + 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 "job_id=$job_id" >> $GITHUB_OUTPUT + + - 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_PROD_HOST }} + + - name: Adding server to known hosts + run: ssh-keyscan -H ${{ secrets.CI_DEPLOY_PROD_HOST }} >> ~/.ssh/known_hosts + + - name: Send deployment files to remote server + run: | + mkdir -p ./${{ steps.get-job-id.outputs.job_id }} + cp -r ./app ./${{ steps.get-job-id.outputs.job_id }} + cp ./docker-compose.yml ./${{ steps.get-job-id.outputs.job_id }}/docker-compose.yml + cp ./Dockerfile ./${{ steps.get-job-id.outputs.job_id }}/Dockerfile + cp ./pyproject.toml ./${{ steps.get-job-id.outputs.job_id }}/pyproject.toml + cp ./.env ./${{ steps.get-job-id.outputs.job_id }}/.env + scp -i ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} -o StrictHostKeyChecking=no -r ./${{ steps.get-job-id.outputs.job_id }} ${{ secrets.CI_DEPLOY_USER }}@${{ secrets.CI_DEPLOY_STAGING_HOST }}:/home/${{ secrets.CI_DEPLOY_USER }}/${{ steps.get-job-id.outputs.job_id }} + + - name: Execute server commands for deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.CI_DEPLOY_STAGING_HOST }} + username: ${{ secrets.CI_DEPLOY_USER }} + key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} + script: | + cd /home/${{ secrets.CI_DEPLOY_USER }}/${{ steps.get-job-id.outputs.job_id }} + mkdir -p /data/tchapbot + + export COMPOSE_FILE=/home/${{ secrets.CI_DEPLOY_USER }}/${{ steps.get-job-id.outputs.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 }}/${{ steps.get-job-id.outputs.job_id }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml new file mode 100644 index 0000000..681f87d --- /dev/null +++ b/.github/workflows/staging.yml @@ -0,0 +1,66 @@ +name: Deploy to staging when pushing on staging + +on: + push: + branches: + - staging + +jobs: + + deploy-staging: + + name: Deploy to staging from ${{ github.ref_name }}/${{ github.sha }} + + runs-on: ubuntu-latest + + steps: + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Get GitHub Actions job ID + 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 "job_id=$job_id" >> $GITHUB_OUTPUT + + - 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_STAGING_HOST }} + + - name: Adding server to known hosts + run: ssh-keyscan -H ${{ secrets.CI_DEPLOY_STAGING_HOST }} >> ~/.ssh/known_hosts + + - name: Send deployment files to remote server + run: | + mkdir -p ./${{ steps.get-job-id.outputs.job_id }} + cp -r ./app ./${{ steps.get-job-id.outputs.job_id }} + cp ./docker-compose.yml ./${{ steps.get-job-id.outputs.job_id }}/docker-compose.yml + cp ./Dockerfile ./${{ steps.get-job-id.outputs.job_id }}/Dockerfile + cp ./pyproject.toml ./${{ steps.get-job-id.outputs.job_id }}/pyproject.toml + cp ./.env ./${{ steps.get-job-id.outputs.job_id }}/.env + scp -i ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} -o StrictHostKeyChecking=no -r ./${{ steps.get-job-id.outputs.job_id }} ${{ secrets.CI_DEPLOY_USER }}@${{ secrets.CI_DEPLOY_STAGING_HOST }}:/home/${{ secrets.CI_DEPLOY_USER }}/${{ steps.get-job-id.outputs.job_id }} + + - name: Execute server commands for deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.CI_DEPLOY_STAGING_HOST }} + username: ${{ secrets.CI_DEPLOY_USER }} + key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }} + script: | + cd /home/${{ secrets.CI_DEPLOY_USER }}/${{ steps.get-job-id.outputs.job_id }} + mkdir -p /data/tchapbot + + export COMPOSE_FILE=/home/${{ secrets.CI_DEPLOY_USER }}/${{ steps.get-job-id.outputs.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 }}/${{ steps.get-job-id.outputs.job_id }} diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index 7f5a80c..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,51 +0,0 @@ -stages: - - deploy - -default: - before_script: - - | # export env file and copy routing table to pyalbert config - if [[ $CI_COMMIT_BRANCH = "staging" ]]; then - echo "info: export staging env file" - while IFS='=' read -r key value; do export "$key"="$value"; done < <(grep -v "^#" $STAGING__ENV_FILE) - cp $STAGING__ENV_FILE ./.env - - elif [[ $CI_COMMIT_BRANCH = "main" ]]; then - echo "info: export production env file" - while IFS='=' read -r key value; do export "$key"="$value"; done < <(grep -v "^#" $PROD__ENV_FILE) - cp $PROD__ENV_FILE ./.env - fi - -deploy: - rules: - - if: $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "staging" - stage: deploy - image: alpine:latest - script: - - | # ssh connection setup - chmod og= $CI_DEPLOY_USER_SSH_PRIVATE_KEY - apk update - apk add openssh-client - - | # send deployment files to remote server - mkdir -p ./${CI_JOB_ID} - cp -r ./app ./${CI_JOB_ID} - cp ./docker-compose.yml ./${CI_JOB_ID}/docker-compose.yml - cp ./Dockerfile ./${CI_JOB_ID}/Dockerfile - cp ./pyproject.toml ./${CI_JOB_ID}/pyproject.toml - cp ./.env ./${CI_JOB_ID}/.env - scp -i $CI_DEPLOY_USER_SSH_PRIVATE_KEY -o StrictHostKeyChecking=no -r ./${CI_JOB_ID} ${CI_DEPLOY_USER}@${CI_DEPLOY_HOST}:/home/${CI_DEPLOY_USER}/${CI_JOB_ID} - - | # deploy app to remote server - ssh -i $CI_DEPLOY_USER_SSH_PRIVATE_KEY -o StrictHostKeyChecking=no ${CI_DEPLOY_USER}@${CI_DEPLOY_HOST} " - cd /home/${CI_DEPLOY_USER}/${CI_JOB_ID} - mkdir -p /data/tchapbot - - export COMPOSE_FILE=/home/${CI_DEPLOY_USER}/${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/${CI_DEPLOY_USER}/${CI_JOB_ID} - "