diff --git a/.github/workflows/deploy_database_production.yml b/.github/workflows/deploy_database_production.yml new file mode 100644 index 0000000..c8665b7 --- /dev/null +++ b/.github/workflows/deploy_database_production.yml @@ -0,0 +1,26 @@ +name: deploy_database_production +concurrency: deploy_database_production + +on: + workflow_dispatch: + workflow_call: + secrets: + DATABASE_USERNAME: + required: true + DATABASE_PASSWORD: + required: true + +jobs: + upgrade: + runs-on: self-hosted + steps: + - uses: actions/checkout@v3 + - name: Upgrade database + run: docker-compose up -d backend_upgrade + - name: Stop container + run: docker-compose stop -d backend_upgrade + - name: Remove image + run: docker rmi elki_97413/onisep_backend_update:latest + env: + DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }} + DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index e876930..46ade1a 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -1,6 +1,7 @@ -name: Deployment +name: Deployments on: + workflow_dispatch: workflow_call: push: branches: @@ -19,17 +20,34 @@ env: ONISEP_APP_ID: ${{ secrets.ONISEP_APP_ID }} jobs: - build: + deploy-datatabse: + uses: ./.github/workflows/deploy_database_production.yml + secrets: inherit + deploy-api: runs-on: self-hosted steps: - name: Checkout code uses: actions/checkout@v3 - - name: Stop running Docker container - run: docker stop api - - name: remove container - run: docker rm api - - name: Remove previous Docker image - run: docker rmi onisep_api-api:latest - - name: Build and Run Docker container - run: docker-compose up -d + + - name: Check if container exists + id: check-container + run: | + if docker ps -a | grep -q 'onisep-api'; then + echo "Container onisep api exists" + echo "::set-output name=container_exists::true" + else + echo "Container onisep api does not exist" + echo "::set-output name=container_exists::false" + fi + + - name: Stop and Remove Old Container + if: steps.check-container.outputs.container_exists == 'true' + run: docker-compose stop onisep-api && docker-compose rm -f onisep-api + + - name: Remove Old Image + if: steps.check-container.outputs.container_exists == 'true' + run: docker rmi elki_97413/onisep:latest + + - name: Build Docker image and Start New Container + run: docker-compose up -d onisep-api diff --git a/.github/workflows/flake8.yml b/.github/workflows/flake8.yml index 48743c7..b12a3b7 100644 --- a/.github/workflows/flake8.yml +++ b/.github/workflows/flake8.yml @@ -1,4 +1,4 @@ -name: flake8 Lint +name: flake8 & ruff linter on: [push] @@ -17,3 +17,11 @@ jobs: uses: py-actions/flake8@v2 with: args: "--config=./.flake8" + ruff-check: + runs-on: ubuntu-latest + name: check + steps: + - uses: actions/checkout@v3 + - uses: chartboost/ruff-action@v1 + with: + args: "--config=./pyproject.toml" diff --git a/Dockerfile.database b/Dockerfile.database new file mode 100644 index 0000000..787231b --- /dev/null +++ b/Dockerfile.database @@ -0,0 +1,19 @@ +FROM python:slim-buster AS build +ENV PYTHONUNBUFFERED 1 + +LABEL authors="Angel-Dijoux" + +WORKDIR /app + +ENV JWT_SECRET_KEY=UNUSED +ENV ENV=production +ENV FLASK_APP=src + + +COPY requirements.txt Pipfile Pipfile.lock ./ + +RUN pip install --user --no-cache-dir pipenv + +COPY . . + +ENV PATH="${PATH}:/root/.local/bin" diff --git a/Dockerfile b/Dockerfile.prod similarity index 78% rename from Dockerfile rename to Dockerfile.prod index 4f1e5ff..c6e43f8 100644 --- a/Dockerfile +++ b/Dockerfile.prod @@ -14,10 +14,6 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . -COPY entrypoint.sh / -RUN chmod +x /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] - EXPOSE 5005 CMD ["gunicorn", "run:app" ,"-c" ,"gunicorn.conf.py" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 44e5fe5..c838603 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,11 +24,12 @@ services: - 8484:80 depends_on: - db - - api: + onisep-api: + image: elki_97413/onisep:latest build: context: . - dockerfile: Dockerfile + dockerfile: Dockerfile.prod + container_name: onisep-api restart: always ports: - 5005:5005 @@ -39,7 +40,28 @@ services: DATABASE_USERNAME: ${DATABASE_USERNAME} DATABASE_PASSWORD: ${DATABASE_PASSWORD} DATABASE_HOST: db - container_name: api + ONISEP_EMAIL: ${ONISEP_EMAIL} + ONISEP_PASSWORD: ${ONISEP_PASSWORD} + ONISEP_APP_ID: ${ONISEP_APP_ID} + backend_upgrade: + image: elki_97413/onisep_backend_update:latest + build: + context: . + dockerfile: Dockerfile.database + environment: + DATABASE_HOST: db + DATABASE_USERNAME: ${DATABASE_USERNAME} + DATABASE_PASSWORD: ${DATABASE_PASSWORD} + depends_on: + - db + command: + - /bin/sh + - -c + - | + pipenv install --deploy --ignore-pipfile + echo "------- Dependencies installed -------" + pipenv run flask db upgrade + echo "------- Database upgraded -------" volumes: db: diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100644 index c4d286e..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,12 +0,0 @@ -#! /bin/bash - -sleep 75 -if [ ! -d "/app/migrations" ]; then - echo "Init database" - flask db init -fi -echo "Upgrade tables" -flask db upgrade -echo "Finish" - -exec "$@" \ No newline at end of file