Skip to content

Commit

Permalink
Improve docker build in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
tsx committed Feb 1, 2024
1 parent d24d7a8 commit c4bd0df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 103 deletions.
36 changes: 4 additions & 32 deletions .github/workflows/build-and-release.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: Build and Release

on:
push:
branches:
- '*'
push

concurrency:
group: build-${{ github.event.pull_request.number || github.ref }}
Expand Down Expand Up @@ -47,13 +45,12 @@ jobs:
strategy:
matrix:
include:
# Make sure only one of the builds have dockerhub_push: true
- arch: amd64
build_name: py3.8
dockerhub_push: true
python_version: "3.8"
run_tests: true
- arch: amd64
build_name: py3.9
dockerhub_push: false
python_version: "3.9"
run_tests: true
Expand All @@ -71,18 +68,12 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Get short sha
id: vars
run: |
echo ::set-output name=sha_short::${GITHUB_SHA::16}
shell: bash

- name: Prepare environment
run: |
if [ "$(git log -1 --pretty=%B | head -n 1 | grep '#notests')" ]; then
echo 'SKIP_TESTS="1"' >> $GITHUB_ENV
fi
echo "DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ steps.vars.outputs.sha_short }}-${{ matrix.build_name}}-${{ matrix.arch }}" >> $GITHUB_ENV
echo "DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ github.sha }}${{ matrix.dockerhub_push && '' || format('-{0}', matrix.python_version) }}-${{ matrix.arch }}" >> $GITHUB_ENV
shell: bash

- name: Set up QEMU
Expand Down Expand Up @@ -143,27 +134,7 @@ jobs:
- build

runs-on: ubuntu-20.04

strategy:
matrix:
include:
- build_name: py3.8

steps:
- name: Get short sha
id: vars
run: |
echo ::set-output name=sha_short::${GITHUB_SHA::16}
shell: bash

- name: Prepare environment
run: |
if [ "$(git log -1 --pretty=%B | head -n 1 | grep '#notests')" ]; then
echo 'SKIP_TESTS="1"' >> $GITHUB_ENV
fi
echo "DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ steps.vars.outputs.sha_short }}-${{ matrix.build_name}}" >> $GITHUB_ENV
shell: bash

- name: Login to DockerHub
uses: docker/login-action@v3
with:
Expand All @@ -172,6 +143,7 @@ jobs:

- name: Create manifest
run: |
DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ github.sha }}
docker buildx imagetools create \
-t ${{ env.DOCKER_IMAGE_TAG }} \
${{ env.DOCKER_IMAGE_TAG }}-amd64
Expand Down
79 changes: 8 additions & 71 deletions .github/workflows/build-arm64.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: Build arm64

on:
push:
branches:
- '*'
push

concurrency:
group: build-arm64-${{ github.event.pull_request.number || github.ref }}
Expand All @@ -12,37 +10,15 @@ concurrency:
env:
DOCKERHUB_NAMESPACE: closeio
PROJECT: sync-engine
python_version: "3.8"
arch: arm64

jobs:
build-arm64:
runs-on: ubuntu-20.04

strategy:
matrix:
include:
- arch: arm64
build_name: py3.8
dockerhub_push: true
python_version: "3.8"
run_tests: false

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Get short sha
id: vars
run: |
echo ::set-output name=sha_short::${GITHUB_SHA::16}
shell: bash

- name: Prepare environment
run: |
if [ "$(git log -1 --pretty=%B | head -n 1 | grep '#notests')" ]; then
echo 'SKIP_TESTS="1"' >> $GITHUB_ENV
fi
echo "DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ steps.vars.outputs.sha_short }}-${{ matrix.build_name}}-${{ matrix.arch }}" >> $GITHUB_ENV
shell: bash

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand All @@ -68,61 +44,21 @@ jobs:
- name: Build and push sync-engine images
uses: docker/build-push-action@v5
with:
platforms: linux/${{ matrix.arch }}
push: false
platforms: linux/${{ env.arch }}
push: true
load: true
build-args:
PYTHON_VERSION=${{ matrix.python_version }}
tags: |
${{ env.DOCKER_IMAGE_TAG }}
PYTHON_VERSION=${{ env.python_version }}
tags: ${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ github.sha }}-${{ env.arch }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run tests
if: ${{ matrix.run_tests }}
run: |
docker tag ${{ env.DOCKER_IMAGE_TAG }} ${{ env.PROJECT }}_app
if [[ -z $SKIP_TESTS ]]; then
docker-compose run app bash -ec '
bin/wait-for-it.sh mysql:3306 \
&& NYLAS_ENV=test pytest --cov-report= --cov=inbox tests/ \
&& coverage html -d pythoncov
'
else
echo Skipping tests
fi
- name: Push image
if: ${{ matrix.dockerhub_push }}
run: |
docker push ${{ env.DOCKER_IMAGE_TAG }}
multi-arch-manifest:
needs:
- build-arm64

runs-on: ubuntu-20.04

strategy:
matrix:
include:
- build_name: py3.8

steps:
- name: Get short sha
id: vars
run: |
echo ::set-output name=sha_short::${GITHUB_SHA::16}
shell: bash

- name: Prepare environment
run: |
if [ "$(git log -1 --pretty=%B | head -n 1 | grep '#notests')" ]; then
echo 'SKIP_TESTS="1"' >> $GITHUB_ENV
fi
echo "DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ steps.vars.outputs.sha_short }}-${{ matrix.build_name}}" >> $GITHUB_ENV
shell: bash

- name: Login to DockerHub
uses: docker/login-action@v3
with:
Expand All @@ -133,6 +69,7 @@ jobs:
# be appended to the existing manifest created by `build-and-release.yaml`.
- name: Create manifest
run: |
DOCKER_IMAGE_TAG=${{ env.DOCKERHUB_NAMESPACE }}/${{ env.PROJECT }}:${{ github.sha }}
docker buildx imagetools create --append \
-t ${{ env.DOCKER_IMAGE_TAG }} \
${{ env.DOCKER_IMAGE_TAG }}-arm64

0 comments on commit c4bd0df

Please sign in to comment.