From dbdbb55328360071c049d35c5d8f0b70a4b30ef4 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 19 Mar 2024 06:34:03 +0100 Subject: [PATCH 1/3] feat: add github container image build --- .dockerignore | 3 + .github/workflows/image-build.yml | 124 ++++++++++++++++++++++++++++++ Dockerfile | 15 ++++ run | 58 ++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/workflows/image-build.yml create mode 100644 Dockerfile create mode 100755 run diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6a5e75b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.github +.travis.yml +Dockerfile diff --git a/.github/workflows/image-build.yml b/.github/workflows/image-build.yml new file mode 100644 index 0000000..09c1c54 --- /dev/null +++ b/.github/workflows/image-build.yml @@ -0,0 +1,124 @@ +name: image build + +on: + push: + branches: + - "master" + paths-ignore: + - 'README.md' + - 'LICENSE.md' + release: + types: [published] + +env: + REGISTRY: ghcr.io + REGISTRY_IMAGE: ghcr.io/${{ github.repository }} + #REGISTRY_IMAGE: ghcr.io/jelmert/cc2538-bsl + +# permissions are needed if pushing to ghcr.io +permissions: + packages: write + +jobs: + build: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + platform: + - linux/amd64 + - linux/arm/v6 + - linux/arm/v7 + - linux/arm64 + - linux/i386 + steps: + - + name: Prepare + run: | + platform=${{ matrix.platform }} + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV + - + name: Checkout + uses: actions/checkout@v4 + - + name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY_IMAGE }} + - + name: Set up QEMU + uses: docker/setup-qemu-action@v3 + if: matrix.platform != 'linux/amd64' + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - + name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - + name: Build and push by digest + id: build + uses: docker/build-push-action@v5 + with: + context: . + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true + - + name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + - + name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-${{ env.PLATFORM_PAIR }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge: + runs-on: ubuntu-latest + needs: + - build + steps: + - + name: Download digests + uses: actions/download-artifact@v4 + with: + path: /tmp/digests + pattern: digests-* + merge-multiple: true + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - + name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY_IMAGE }} + - + name: Login to Docker Hub + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - + name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) + - + name: Inspect image + run: | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fd9d961 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3-alpine +ARG REPO_URL='.[cc2538-bsl,intelhex]' +ADD . /tmp/build/ +RUN cd /tmp/build && \ + apk add --no-cache file git && \ + export PIP_ROOT_USER_ACTION=ignore && \ + pip3 install "${REPO_URL}" && \ + pip3 uninstall -y setuptools wheel pip && \ + apk del --no-cache -r git && \ + rm -r /root/.cache /tmp/build +RUN adduser -D user && addgroup user dialout +USER user +ADD run /usr/local/bin/ +WORKDIR /tmp +ENTRYPOINT ["run"] diff --git a/run b/run new file mode 100755 index 0000000..934ca60 --- /dev/null +++ b/run @@ -0,0 +1,58 @@ +#!/bin/sh +set -eu +[ -z "${TRACE-}" ] || set -x + +log() { + echo >&2 "${@}" +} + +error() { + log "ERROR: ${@}" + return 1 +} + +prepare_firmware() { + set -eu + file="${1}" + if [ ! -r "${file}" ]; then + error "Unreadable firmware file '${file}'" + elif file -b "${file}" | grep -qi ^zip; then + unzip "${file}" | sed -nE 's/ inflating: (.+)/\1/p' + else + echo "${file}" + fi +} + +run() { + local source="${1}" file + shift + if [ -n "${source}" ]; then + file="$(prepare_firmware "${source}")" + [ "${file}" = "${source}" ] || log "Firmware source: '${source}'" + if [ -z "${file}" ]; then + error "Firmware source does not contain a firmware file." + elif [ "$(echo "${file}" | wc -l)" -gt 1 ]; then + error "Expected exactly 1 firmware file, but found: $(echo "${file}" | xargs)" + else + log "Firmware file: '${file}'" + set -- "${@}" "${file}" + fi + fi + cc2538-bsl.py "${@}" +} + +main() { + local source + if [ -n "${FIRMWARE_FILE-}" ]; then + source="${FIRMWARE_FILE}" + elif [ -n "${FIRMWARE_URL-}" ]; then + log "Downloading firmware from ${FIRMWARE_URL}" + source="$(wget "${FIRMWARE_URL}" 2>&1 | sed -nE "s/^'(.+)' saved$/\1/p")" + [ -n "${source}" ] || error "Download of firmware was not successful." + else + log "WARNING: neither FIRMWARE_URL nor FIRMWARE_FILE is set as environment variable." + fi + run "${source-}" "${@}" +} + +main "${@}" From 714bca78471153d3deeff6f99a6941f16f130393 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 19 Mar 2024 06:49:31 +0100 Subject: [PATCH 2/3] chore: prepare pull request --- .github/workflows/image-build.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/image-build.yml b/.github/workflows/image-build.yml index 09c1c54..ef01f90 100644 --- a/.github/workflows/image-build.yml +++ b/.github/workflows/image-build.yml @@ -12,8 +12,7 @@ on: env: REGISTRY: ghcr.io - REGISTRY_IMAGE: ghcr.io/${{ github.repository }} - #REGISTRY_IMAGE: ghcr.io/jelmert/cc2538-bsl + REGISTRY_IMAGE: ghcr.io/jelmert/cc2538-bsl # permissions are needed if pushing to ghcr.io permissions: From 31d6ecaac9c9da6423a23611541cddf3c87757ee Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 19 Mar 2024 07:12:08 +0100 Subject: [PATCH 3/3] chore: fix comments --- .github/workflows/image-build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/image-build.yml b/.github/workflows/image-build.yml index ef01f90..2b43bc1 100644 --- a/.github/workflows/image-build.yml +++ b/.github/workflows/image-build.yml @@ -53,7 +53,7 @@ jobs: name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Login to GitHub Container Registry + name: Login to Image Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} @@ -105,7 +105,7 @@ jobs: with: images: ${{ env.REGISTRY_IMAGE }} - - name: Login to Docker Hub + name: Login to Image registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }}