From bf4303ec6355f9d1c8a916cddf21987e42fb0771 Mon Sep 17 00:00:00 2001 From: Josh Willox Date: Mon, 10 Jun 2024 16:37:09 +1000 Subject: [PATCH] ci: improve build workflow --- .github/workflows/ci.yaml | 137 +++++++++++++++++++++++++++++++++ .github/workflows/deploy.yaml | 137 +++++++++++++++++++++++++++++++++ .github/workflows/publish.yaml | 44 ----------- .github/workflows/test.yaml | 35 --------- rclone_backup/Dockerfile | 21 +++-- rclone_backup/build.yaml | 1 - rclone_backup/config.yaml | 2 - 7 files changed, 289 insertions(+), 88 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/deploy.yaml delete mode 100644 .github/workflows/publish.yaml delete mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..36a61c2 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,137 @@ +name: "CI" + +on: + push: + branches: + - "main" + - "feat**" + tags-ignore: + - "**" + pull_request: + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + information: + name: "Gather add-on information" + runs-on: ubuntu-latest + outputs: + architectures: ${{ steps.information.outputs.architectures }} + base_image_signer: ${{ steps.information.outputs.codenotary_base_image }} + build: ${{ steps.information.outputs.build }} + description: ${{ steps.information.outputs.description }} + name: ${{ steps.information.outputs.name }} + slug: ${{ steps.override.outputs.slug }} + target: ${{ steps.information.outputs.target }} + steps: + - name: "โคต๏ธ Check out code from GitHub" + uses: actions/checkout@v4 + + - name: "๐Ÿš€ Run add-on information action" + id: information + uses: frenck/action-addon-information@v1.4.2 + + - name: "๐Ÿš€ Process possible slug override" + id: override + run: | + echo "slug=hassio-rclone-backup" >> "$GITHUB_OUTPUT" + + lint-addon: + name: Lint Add-on + needs: + - information + runs-on: ubuntu-latest + steps: + - name: โคต๏ธ Check out code from GitHub + uses: actions/checkout@v4 + - name: ๐Ÿš€ Run Add-on Lint + uses: frenck/action-addon-linter@v2.15.1 + with: + community: false + path: "./${{ needs.information.outputs.target }}" + + build: + name: "Build ${{ matrix.architecture }}" + needs: + - information + - lint-addon + runs-on: ubuntu-latest + strategy: + matrix: + architecture: ${{ fromJson(needs.information.outputs.architectures) }} + steps: + - name: "โคต๏ธ Check out code from GitHub" + uses: actions/checkout@v4 + + - name: "๐Ÿ— Set up build cache" + id: cache + uses: actions/cache@v4.0.2 + with: + path: /tmp/.docker-cache + key: docker-${{ matrix.architecture }}-${{ github.sha }} + restore-keys: | + docker-${{ matrix.architecture }} + - name: "๐Ÿ— Set up QEMU" + uses: docker/setup-qemu-action@v3.0.0 + + - name: "๐Ÿ— Set up Docker Buildx" + uses: docker/setup-buildx-action@v3.3.0 + + - name: "โ„น๏ธ Compose build flags" + id: flags + run: | + echo "date=$(date +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_OUTPUT" + from=$(yq --no-colors eval ".build_from.${{ matrix.architecture }}" "${{ needs.information.outputs.build }}") + echo "from=${from}" >> "$GITHUB_OUTPUT" + + if [[ "${{ matrix.architecture}}" = "amd64" ]]; then + echo "platform=linux/amd64" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "i386" ]]; then + echo "platform=linux/386" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "armhf" ]]; then + echo "platform=linux/arm/v6" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "armv7" ]]; then + echo "platform=linux/arm/v7" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "aarch64" ]]; then + echo "platform=linux/arm64/v8" >> "$GITHUB_OUTPUT" + else + echo "::error ::Could not determine platform for architecture ${{ matrix.architecture }}" + exit 1 + fi + + - name: "โคต๏ธ Download base image" + if: steps.flags.outputs.from != 'null' + run: docker pull "${{ steps.flags.outputs.from }}" + + - name: "๐Ÿš€ Build" + uses: docker/build-push-action@v5.3.0 + with: + push: false + context: ${{ needs.information.outputs.target }} + file: ${{ needs.information.outputs.target }}/Dockerfile + cache-from: | + type=local,src=/tmp/.docker-cache + ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:edge + cache-to: type=local,mode=max,dest=/tmp/.docker-cache-new + platforms: ${{ steps.flags.outputs.platform }} + build-args: | + BUILD_ARCH=${{ matrix.architecture }} + BUILD_DATE=${{ steps.flags.outputs.date }} + BUILD_DESCRIPTION=${{ needs.information.outputs.description }} + BUILD_FROM=${{ steps.flags.outputs.from }} + BUILD_NAME=${{ needs.information.outputs.name }} + BUILD_REF=${{ github.sha }} + BUILD_REPOSITORY=${{ github.repository }} + BUILD_VERSION=edge + + # This ugly bit is necessary, or our cache will grow forever... + # Well until we hit GitHub's limit of 5GB :) + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + - name: "๐Ÿšš Swap build cache" + run: | + rm -rf /tmp/.docker-cache + mv /tmp/.docker-cache-new /tmp/.docker-cache diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000..518dead --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,137 @@ +name: "Deploy" + +concurrency: + group: queue + +on: + release: + types: [published] + +jobs: + information: + name: "โ„น๏ธ Gather add-on information" + runs-on: ubuntu-latest + outputs: + architectures: ${{ steps.information.outputs.architectures }} + base_image_signer: ${{ steps.information.outputs.codenotary_base_image }} + build: ${{ steps.information.outputs.build }} + description: ${{ steps.information.outputs.description }} + environment: ${{ steps.release.outputs.environment }} + name: ${{ steps.information.outputs.name }} + signer: ${{ steps.information.outputs.codenotary_signer }} + slug: ${{ steps.override.outputs.slug }} + target: ${{ steps.information.outputs.target }} + version: ${{ steps.release.outputs.version }} + steps: + - name: "Checkout the repository" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "๐Ÿš€ Run add-on information action" + id: information + uses: frenck/action-addon-information@v1.4.2 + + - name: "๐Ÿš€ Process possible slug override" + id: override + run: | + echo "slug=hassio-rclone-backup" >> "$GITHUB_OUTPUT" + + - name: "โ„น๏ธ Gather version and environment" + id: release + run: | + version="${{ github.event.release.tag_name }}" + version="${version,,}" + version="${version#v}" + echo "version=${version}" >> "$GITHUB_OUTPUT" + + deploy: + name: "๐Ÿ‘ท Build & Deploy ${{ matrix.architecture }}" + needs: information + runs-on: ubuntu-latest + strategy: + matrix: + architecture: ${{ fromJson(needs.information.outputs.architectures) }} + steps: + - name: "Checkout the repository" + uses: actions/checkout@v4 + + - name: "๐Ÿ— Set up build cache" + id: cache + uses: actions/cache@v4.0.2 + with: + path: /tmp/.docker-cache + key: docker-${{ matrix.architecture }}-${{ github.sha }} + restore-keys: | + docker-${{ matrix.architecture }} + - name: "๐Ÿ— Set up QEMU" + uses: docker/setup-qemu-action@v3.0.0 + + - name: "๐Ÿ— Set up Docker Buildx" + uses: docker/setup-buildx-action@v3.3.0 + + - name: "โ„น๏ธ Compose build flags" + id: flags + run: | + echo "date=$(date +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_OUTPUT" + from=$(yq --no-colors eval ".build_from.${{ matrix.architecture }}" "${{ needs.information.outputs.build }}") + echo "from=${from}" >> "$GITHUB_OUTPUT" + + if [[ "${{ matrix.architecture}}" = "amd64" ]]; then + echo "platform=linux/amd64" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "i386" ]]; then + echo "platform=linux/386" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "armhf" ]]; then + echo "platform=linux/arm/v6" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "armv7" ]]; then + echo "platform=linux/arm/v7" >> "$GITHUB_OUTPUT" + elif [[ "${{ matrix.architecture }}" = "aarch64" ]]; then + echo "platform=linux/arm64/v8" >> "$GITHUB_OUTPUT" + else + echo "::error ::Could not determine platform for architecture ${{ matrix.architecture }}" + exit 1 + fi + + - name: "Login to GitHub Container Registry" + uses: docker/login-action@v3.2.0 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: "โคต๏ธ Download base image" + if: steps.flags.outputs.from != 'null' + run: docker pull "${{ steps.flags.outputs.from }}" + + - name: "๐Ÿš€ Build" + uses: docker/build-push-action@v5.3.0 + with: + push: true + tags: | + ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:latest + ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:${{ needs.information.outputs.version }} + context: ${{ needs.information.outputs.target }} + file: ${{ needs.information.outputs.target }}/Dockerfile + cache-from: | + type=local,src=/tmp/.docker-cache + ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:edge + cache-to: type=local,mode=max,dest=/tmp/.docker-cache-new + platforms: ${{ steps.flags.outputs.platform }} + build-args: | + BUILD_ARCH=${{ matrix.architecture }} + BUILD_DATE=${{ steps.flags.outputs.date }} + BUILD_DESCRIPTION=${{ needs.information.outputs.description }} + BUILD_FROM=${{ steps.flags.outputs.from }} + BUILD_NAME=${{ needs.information.outputs.name }} + BUILD_REF=${{ github.sha }} + BUILD_REPOSITORY=${{ github.repository }} + BUILD_VERSION=${{ needs.information.outputs.version }} + + # This ugly bit is necessary, or our cache will grow forever... + # Well until we hit GitHub's limit of 5GB :) + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + - name: "๐Ÿšš Swap build cache" + run: | + rm -rf /tmp/.docker-cache + mv /tmp/.docker-cache-new /tmp/.docker-cache diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml deleted file mode 100644 index 9528dda..0000000 --- a/.github/workflows/publish.yaml +++ /dev/null @@ -1,44 +0,0 @@ -name: "Publish" - -on: - release: - types: [published] - -env: - TARGET: rclone_backup - -jobs: - publish: - name: Publish - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - id-token: write - steps: - - name: "Checkout the repository" - uses: actions/checkout@v4 - with: - fetch-depth: 0 - ref: main - - - name: "Get addon information and update build args" - id: information - uses: ./.github/actions/addon-info - - - name: "Login to GitHub Container Registry" - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: "Publish" - uses: home-assistant/builder@master - with: - args: | - --all \ - --target ${{ env.TARGET }} \ - --image ${{ steps.information.outputs.image }} \ - --docker-hub ${{ steps.information.outputs.repository }} \ - --release ${{ github.event.release.tag_name }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml deleted file mode 100644 index 62c20d6..0000000 --- a/.github/workflows/test.yaml +++ /dev/null @@ -1,35 +0,0 @@ -name: "Test" - -on: - push: - branches: - - "main" - - "feat**" - tags-ignore: - - "**" - pull_request: - -env: - TARGET: rclone_backup - -jobs: - build: - name: Test Build - runs-on: ubuntu-latest - steps: - - name: "Checkout the repository" - uses: actions/checkout@v4 - - - name: "Get addon information and update build args" - id: information - uses: ./.github/actions/addon-info - - - name: "Test build" - uses: home-assistant/builder@master - with: - args: | - --test \ - --all \ - --target ${{ env.TARGET }} \ - --image ${{ steps.information.outputs.image }} \ - --docker-hub ${{ steps.information.outputs.repository }} diff --git a/rclone_backup/Dockerfile b/rclone_backup/Dockerfile index ca19815..94682ad 100644 --- a/rclone_backup/Dockerfile +++ b/rclone_backup/Dockerfile @@ -29,10 +29,12 @@ FROM $BUILD_FROM # Build arguments ARG BUILD_ARCH -ARG BUILD_NAME +ARG BUILD_DATE ARG BUILD_DESCRIPTION +ARG BUILD_NAME +ARG BUILD_REF ARG BUILD_REPOSITORY -ARG BUILD_SHA +ARG BUILD_VERSION ENV LANG C.UTF-8 # renovate: datasource=github-releases depName=rclone packageName=rclone/rclone @@ -81,13 +83,20 @@ RUN if [ "${BUILD_ARCH}" = "armhf" ]; then \ # Labels LABEL \ + io.hass.name="${BUILD_NAME}" \ + io.hass.description="${BUILD_DESCRIPTION}" \ + io.hass.arch="${BUILD_ARCH}" \ + io.hass.type="addon" \ + io.hass.version=${BUILD_VERSION} \ + maintainer="Josh Willox" \ org.opencontainers.image.title="${BUILD_NAME}" \ org.opencontainers.image.description="${BUILD_DESCRIPTION}" \ + org.opencontainers.image.vendor="" \ + org.opencontainers.image.authors="Josh Willox" \ org.opencontainers.image.licenses="MIT" \ org.opencontainers.image.url="https://github.com/${BUILD_REPOSITORY}" \ org.opencontainers.image.source="https://github.com/${BUILD_REPOSITORY}" \ org.opencontainers.image.documentation="https://github.com/${BUILD_REPOSITORY}/blob/main/README.md" \ - org.opencontainers.image.revision=${BUILD_SHA} \ - org.opencontainers.image.authors="" \ - org.opencontainers.image.vendor="" \ - maintainer="" + org.opencontainers.image.created=${BUILD_DATE} \ + org.opencontainers.image.revision=${BUILD_REF} \ + org.opencontainers.image.version=${BUILD_VERSION} diff --git a/rclone_backup/build.yaml b/rclone_backup/build.yaml index 76a5e4d..c08e2ae 100644 --- a/rclone_backup/build.yaml +++ b/rclone_backup/build.yaml @@ -5,4 +5,3 @@ build_from: armhf: ghcr.io/hassio-addons/base-python/armhf:13.1.3 armv7: ghcr.io/hassio-addons/base-python/armv7:13.1.3 i386: ghcr.io/hassio-addons/base-python/i386:13.1.3 -args: {} diff --git a/rclone_backup/config.yaml b/rclone_backup/config.yaml index d037eb7..734665f 100644 --- a/rclone_backup/config.yaml +++ b/rclone_backup/config.yaml @@ -11,8 +11,6 @@ arch: - i386 init: false url: https://github.com/jcwillox/hassio-rclone-backup -startup: application -boot: auto homeassistant_api: true ingress: true panel_icon: mdi:cloud-sync