From 2f469add135c6cf25ff6afb1bbc15c6c56c4bd64 Mon Sep 17 00:00:00 2001 From: Dyakov Roman Date: Tue, 15 Nov 2022 00:59:59 +0300 Subject: [PATCH] CI --- .github/workflows/build_and_publish.yml | 93 ++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_and_publish.yml b/.github/workflows/build_and_publish.yml index b4a764f..c50488f 100644 --- a/.github/workflows/build_and_publish.yml +++ b/.github/workflows/build_and_publish.yml @@ -3,6 +3,8 @@ name: Build, publish and deploy docker on: push: branches: ['main'] + tags: + - 'v*' env: REGISTRY: ghcr.io @@ -32,7 +34,10 @@ jobs: 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: @@ -40,3 +45,89 @@ jobs: 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://printer.api.test.profcomff.com/ + env: + CONTAITER_NAME: com_profcomff_api_printer_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 \ + ${{ 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 \ + --volume com_profcomff_api_printer_test_static:/app/static \ + --env DB_DSN=${{ secrets.DB_DSN }} \ + --env SECRET_KEY='${{ secrets.SECRET_KEY }}' \ + --env ROOT=https://printer.api.test.profcomff.com/ \ + --env PIN_SYMBOLS=0123456789 \ + --env PIN_LENGTH=8 \ + --env ALLOW_STUDENT_NUMBER=true \ + --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://printer.api.profcomff.com/ + env: + CONTAITER_NAME: com_profcomff_api_printer + 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 \ + ${{ 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 \ + --volume com_profcomff_api_printer_static:/app/static \ + --env DB_DSN='${{ secrets.DB_DSN }}' \ + --env SECRET_KEY='${{ secrets.SECRET_KEY }}' \ + --env ROOT=https://printer.api.profcomff.com/ \ + --env PIN_SYMBOLS=0123456789 \ + --env PIN_LENGTH=8 \ + --env ALLOW_STUDENT_NUMBER=true \ + --name ${{ env.CONTAITER_NAME }} \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest