Skip to content

Commit

Permalink
refacto: github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bepi-octo committed Jul 4, 2024
1 parent 65310f6 commit fa60be9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 79 deletions.
118 changes: 39 additions & 79 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
@@ -1,97 +1,57 @@
name: Build and deploy when pushing on staging or main

name: Build and push to docker registry when pushing on staging or main
permissions:
packages: write
on:
push:
branches:
- main
- staging
pull_request:
branches:
- main
- dev

jobs:

build-and-deploy:

build-and-push:
environment: ${{ github.ref_name == 'main' && 'prod' || 'staging' }}

name: Deploy from ${{ github.ref_name }}/${{ github.sha }}

name: Build and push from ${{ github.ref_name }}/${{ github.sha }}
runs-on: ubuntu-latest

env:
IMAGE_NAME: ghcr.io/${{ github.repository }}/albert-tchap
IMAGE_TAG: ${{ github.sha }}
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
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
key: ${{ secrets.CI_DEPLOY_USER_SSH_PRIVATE_KEY }}
name: id_rsa
known_hosts: ${{ secrets.CI_DEPLOY_HOST }}
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Adding server to known hosts
run: ssh-keyscan -H ${{ secrets.CI_DEPLOY_HOST }} >> ~/.ssh/known_hosts
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- 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: Create .env file with current variables and secrets
run: |
cd ./${{ env.CI_JOB_ID }}
touch .env
echo JOIN_ON_INVITE=${{ vars.JOIN_ON_INVITE }} >> .env
echo SALT=${{ secrets.SALT }} >> .env
echo MATRIX_HOME_SERVER=${{ secrets.MATRIX_HOME_SERVER }} >> .env
echo MATRIX_BOT_USERNAME=${{ secrets.MATRIX_BOT_USERNAME }} >> .env
echo MATRIX_BOT_PASSWORD=${{ secrets.MATRIX_BOT_PASSWORD }} >> .env
echo ERRORS_ROOM_ID=${{ secrets.ERRORS_ROOM_ID }} >> .env
echo USER_ALLOWED_DOMAINS=${{ secrets.USER_ALLOWED_DOMAINS }} >> .env
echo GROUPS_USED=${{ vars.GROUPS_USED }} >> .env
echo ALBERT_API_URL=${{ secrets.ALBERT_API_URL }} >> .env
echo ALBERT_API_TOKEN=${{ secrets.ALBERT_API_TOKEN }} >> .env
echo ALBERT_MODEL_NAME=${{ vars.ALBERT_MODEL_NAME }} >> .env
echo ALBERT_MODE=${{ vars.ALBERT_MODE }} >> .env
cat .env
- name: Copy deployment files to remote server via SCP
uses: appleboy/scp-action@master
- name: Build and push
uses: docker/build-push-action@v6
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 }}"

- 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
push: true
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }},${{ env.IMAGE_NAME }}:latest

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 }}
deploy-staging:
environment: ${{ github.ref_name == 'main' && 'prod' || 'staging' }}
name: Deploy from ${{ github.ref_name }}/${{ github.sha }}
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/remove_deployment_from_github_actions'
needs: build-and-push
steps:
- name: Trigger staging deployment
run: |
curl --request POST \
--form token=${{ secrets.GITLAB_CI_TOKEN }} \
--form ref=main \
--form "variables[docker_image_tag]=latest" \
--form "variables[application_to_deploy]=albert-tchap" \
--form "variables[deployment_environment]=staging" \
"https://gitlab.com/api/v4/projects/58117805/trigger/pipeline"
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ ADD ./app ./app
ADD ./pyproject.toml ./app/pyproject.toml

WORKDIR /code/app

CMD ["python3", "."]

0 comments on commit fa60be9

Please sign in to comment.