From d51a89f93f60e481f7d6ca60376aa31e61db6ddf Mon Sep 17 00:00:00 2001 From: Carlo Mion Date: Tue, 28 Feb 2023 18:12:57 +0100 Subject: [PATCH 1/2] ci(build-deploy.yml): add build and deploy Github workflow On PR against `dev` build the Python package and the Docker image. If there is push in the `dev` branch or a git tag, also deploy to Docker Hub and PyPi. --- .github/workflows/build-deploy.yml | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 .github/workflows/build-deploy.yml diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml new file mode 100644 index 000000000..ce41fba3a --- /dev/null +++ b/.github/workflows/build-deploy.yml @@ -0,0 +1,96 @@ +# Build a docker image and Python package. If on `dev`` branch or git tag: +# - Deploy Docker image to Docker Hub +# - Deploy Python package to PyPi +name: Build and deploy + +on: + push: + branches: ["dev"] + tags: ["*"] + pull_request: + branches: ["dev"] + +# Cancel a currently running workflow from the same PR, branch or tag when a new workflow is triggered: +# Taken from https://stackoverflow.com/a/72408109 +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +env: + # github.repository as / + IMAGE_NAME: ${{ github.repository }} + +jobs: + build_package: + name: Python package + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + # https://github.com/actions/setup-python + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: "3.10" + cache: 'pip' # caching pip dependencies + # Manually set pyproject.toml as the file to use for dependencies + # Workaround while waiting for https://github.com/actions/setup-python/issues/529 + cache-dependency-path: 'pyproject.toml' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e .[dev] + - name: Build Python package + run: python -m build + + # Publish package only on Git tag + - name: Publish package + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') + uses: pypa/gh-action-pypi-publish@release/v1 + with: + password: ${{ secrets.PYPI_API_TOKEN }} + + build_image: + name: Docker image + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v2.4.1 + # Login against a Docker registry (except on PR) + # https://github.com/docker/login-action + - name: Log into Docker Hub + if: github.event_name != 'pull_request' + uses: docker/login-action@v2.1.0 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v4.3.0 + with: + images: ${{ env.IMAGE_NAME }} + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v4.0.0 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/arm64/v8, linux/amd64 + cache-from: type=gha + cache-to: type=gha,mode=max From cc5ff2ed8ac6e101e9858c52a47bfd18b08b24b2 Mon Sep 17 00:00:00 2001 From: Carlo Mion Date: Tue, 28 Feb 2023 21:54:04 +0100 Subject: [PATCH 2/2] ci(build-deploy.yml): update actions/setup-python to v4 --- .github/workflows/build-deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml index ce41fba3a..43e441df5 100644 --- a/.github/workflows/build-deploy.yml +++ b/.github/workflows/build-deploy.yml @@ -33,7 +33,7 @@ jobs: uses: actions/checkout@v3 # https://github.com/actions/setup-python - name: Set up Python - uses: actions/setup-python@v3 + uses: actions/setup-python@v4 with: python-version: "3.10" cache: 'pip' # caching pip dependencies