From e2dff225f06cbfeb54f3bd0316f88c5bc49c818c Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Tue, 23 Jan 2024 10:52:25 -0700 Subject: [PATCH] updates --- .github/workflows/main.yml | 41 ++++++++++++++++++++++++++++++++++++++ project/app/api/ping.py | 2 +- project/tests/test_ping.py | 2 +- release.sh | 12 +++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 release.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9013472..402617a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -75,3 +75,44 @@ jobs: run: docker exec fastapi-tdd python -m black . --check - name: isort run: docker exec fastapi-tdd python -m isort . --check-only + + deploy: + name: Deploy to Heroku + runs-on: ubuntu-latest + needs: [build, test] + env: + HEROKU_APP_NAME: quiet-citadel-80656 + HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/summarizer + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + ref: main + - name: Log in to GitHub Packages + run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Pull image + run: | + docker pull ${{ env.IMAGE }}:latest || true + - name: Build image + run: | + docker build \ + --cache-from ${{ env.IMAGE }}:latest \ + --tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \ + --file ./project/Dockerfile.prod \ + "./project" + - name: Log in to the Heroku Container Registry + run: docker login -u _ -p ${HEROKU_AUTH_TOKEN} registry.heroku.com + env: + HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }} + - name: Push to the registry + run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }} + - name: Set environment variables + run: | + echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV + echo "HEROKU_AUTH_TOKEN=${{ secrets.HEROKU_AUTH_TOKEN }}" >> $GITHUB_ENV + - name: Release + run: | + chmod +x ./release.sh + ./release.sh diff --git a/project/app/api/ping.py b/project/app/api/ping.py index 034e33a..b5d625f 100644 --- a/project/app/api/ping.py +++ b/project/app/api/ping.py @@ -8,7 +8,7 @@ @router.get("/ping") async def pong(settings: Settings = Depends(get_settings)): return { - "ping": "pong!", + "ping": "pong", "environment": settings.environment, "testing": settings.testing, } diff --git a/project/tests/test_ping.py b/project/tests/test_ping.py index ef72e0d..34c89d6 100644 --- a/project/tests/test_ping.py +++ b/project/tests/test_ping.py @@ -1,4 +1,4 @@ def test_ping(test_app): response = test_app.get("/ping") assert response.status_code == 200 - assert response.json() == {"environment": "dev", "ping": "pong!", "testing": True} + assert response.json() == {"environment": "dev", "ping": "pong", "testing": True} diff --git a/release.sh b/release.sh new file mode 100644 index 0000000..bb7e334 --- /dev/null +++ b/release.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +set -e + +IMAGE_ID=$(docker inspect ${HEROKU_REGISTRY_IMAGE} --format={{.Id}}) +PAYLOAD='{"updates": [{"type": "web", "docker_image": "'"$IMAGE_ID"'"}]}' + +curl -n -X PATCH https://api.heroku.com/apps/$HEROKU_APP_NAME/formation \ + -d "${PAYLOAD}" \ + -H "Content-Type: application/json" \ + -H "Accept: application/vnd.heroku+json; version=3.docker-releases" \ + -H "Authorization: Bearer ${HEROKU_AUTH_TOKEN}"