From 37b229e059c037123d0ae854aea9c2019775eb2e Mon Sep 17 00:00:00 2001 From: semen603089 Date: Wed, 7 Dec 2022 23:04:03 +0300 Subject: [PATCH] CI Init --- .github/workflows/build_and_publish.yml | 128 ++++++++++++++++++++++++ Dockerfile | 14 +-- 2 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/build_and_publish.yml diff --git a/.github/workflows/build_and_publish.yml b/.github/workflows/build_and_publish.yml new file mode 100644 index 0000000..5cc0e43 --- /dev/null +++ b/.github/workflows/build_and_publish.yml @@ -0,0 +1,128 @@ +name: Build, publish and deploy docker + +on: + push: + branches: ['main'] + tags: + - 'v*' + + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + name: Build and push + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=tag,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + type=raw,value=test,enable=true + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + deploy-testing: + name: Deploy Testing + needs: build-and-push-image + runs-on: [self-hosted, Linux] + environment: + name: Testing + url: https://services.api.test.profcomff.com/ + env: + CONTAITER_NAME: com_profcomff_api_services_test + permissions: + packages: read + + steps: + - name: Pull new version + run: docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:test + + - name: Migrate DB + run: | + docker run \ + --rm \ + --network=web \ + --env DB_DSN=${{ secrets.DB_DSN }} \ + --name ${{ env.CONTAITER_NAME }}_migration \ + --workdir="/" \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:test \ + alembic upgrade head + + - name: Run new version + id: run_test + run: | + docker stop ${{ env.CONTAITER_NAME }} || true && docker rm ${{ env.CONTAITER_NAME }} || true + docker run \ + --detach \ + --restart on-failure:3 \ + --network=web \ + --env DB_DSN=${{ secrets.DB_DSN }} \ + --name ${{ env.CONTAITER_NAME }} \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:test + + deploy-production: + name: Deploy Production + needs: build-and-push-image + if: startsWith(github.ref, 'refs/tags/v') + runs-on: [self-hosted, Linux] + environment: + name: Production + url: https://services.api.profcomff.com/ + env: + CONTAITER_NAME: com_profcomff_api_services + permissions: + packages: read + + steps: + - name: Pull new version + run: docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + + - name: Migrate DB + run: | + docker run \ + --rm \ + --network=web \ + --env DB_DSN=${{ secrets.DB_DSN }} \ + --name ${{ env.CONTAITER_NAME }}_migration \ + --workdir="/" \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \ + alembic upgrade head + + - name: Run new version + id: run_test + run: | + docker stop ${{ env.CONTAITER_NAME }} || true && docker rm ${{ env.CONTAITER_NAME }} || true + docker run \ + --detach \ + --restart always \ + --network=web \ + --env DB_DSN='${{ secrets.DB_DSN }}' \ + --name ${{ env.CONTAITER_NAME }} \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest diff --git a/Dockerfile b/Dockerfile index cbec1e2..74e725b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ -FROM python:3.10 -WORKDIR /app +FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11 +ENV APP_NAME=services_backend +ENV APP_MODULE=${APP_NAME}.routes.base:app COPY ./requirements.txt /app/ -RUN pip install --no-cache-dir -r /app/requirements.txt +RUN pip install -U -r /app/requirements.txt -ADD gunicorn_conf.py alembic.ini /app/ -ADD migrations /app/migrations -ADD services-backend /app/services-backend +COPY ./alembic.ini /alembic.ini +COPY ./migrations /migrations/ -CMD [ "gunicorn", "-k", "uvicorn.workers.UvicornWorker", "-c", "/app/gunicorn_conf.py", "services-backend.routes.base:app" ] +COPY ./${APP_NAME} /app/${APP_NAME} \ No newline at end of file