From 847495a662adfb979330fbde7e235207032ea3c9 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 18 Jan 2022 15:56:26 +0100 Subject: [PATCH] Update builder.yaml --- .github/workflows/builder.yaml | 275 ++++++++++----------------------- 1 file changed, 84 insertions(+), 191 deletions(-) diff --git a/.github/workflows/builder.yaml b/.github/workflows/builder.yaml index 9d6425bb5..76cd33eb2 100644 --- a/.github/workflows/builder.yaml +++ b/.github/workflows/builder.yaml @@ -1,215 +1,108 @@ -name: Deploy addons +name: Builder + +env: + BUILD_ARGS: "--test" + MONITORED_FILES: "CHANGELOG.md" + on: push: branches: - - master + - main + pull_request: + branches: + - main jobs: - check-addon-changes: + init: runs-on: ubuntu-latest + name: Initialize builds outputs: - changedAddons: ${{ steps.filter.outputs.changes }} + changed_addons: ${{ steps.changed_addons.outputs.addons }} + changed: ${{ steps.changed_addons.outputs.changed }} steps: + - name: Check out the repository + uses: actions/checkout@v2.4.0 - - name: â†Šī¸ Checkout - uses: actions/checkout@v2 + - name: Get changed files + id: changed_files + uses: jitterbit/get-changed-files@v1 - - name: 📂 Detect chanced files - uses: dorny/paths-filter@v2 - id: filter - with: - filters: .github/paths-filter.yml + - name: Find add-on directories + id: addons + uses: home-assistant/actions/helpers/find-addons@master - build-addon: - name: Build and deploy addon - if: ${{ needs.check-addon-changes.outputs.changedAddons != '[]' }} + - name: Get changed add-ons + id: changed_addons + run: | + declare -a changed_addons + for addon in ${{ steps.addons.outputs.addons }}; do + if [[ "${{ steps.changed_files.outputs.all }}" =~ $addon ]]; then + for file in ${{ env.MONITORED_FILES }}; do + if [[ "${{ steps.changed_files.outputs.all }}" =~ $addon/$file ]]; then + if [[ ! "${changed_addons[@]}" =~ $addon ]]; then + changed_addons+=("\"${addon}\","); + fi + fi + done + fi + done + changed=$(echo ${changed_addons[@]} | rev | cut -c 2- | rev) + if [[ -n ${changed} ]]; then + echo "Changed add-ons: $changed"; + echo "::set-output name=changed::true"; + echo "::set-output name=addons::[$changed]"; + else + echo "No add-on had any monitored files changed (${{ env.MONITORED_FILES }})"; + fi + build: + needs: init runs-on: ubuntu-latest - needs: check-addon-changes + if: needs.init.outputs.changed == 'true' + name: Build ${{ matrix.arch }} ${{ matrix.addon }} add-on strategy: - fail-fast: false matrix: - addon: ${{ fromJSON(needs.check-addon-changes.outputs.changedAddons) }} + addon: ${{ fromJson(needs.init.outputs.changed_addons) }} + arch: ["aarch64", "amd64", "armhf", "armv7"] + steps: + - name: Check out repository + uses: actions/checkout@v2.4.0 - - name: â†Šī¸ Checkout - uses: actions/checkout@v2 - - - name: â„šī¸ Gather addon info - id: information - uses: frenck/action-addon-information@v1.3.1 + - name: Get information + id: info + uses: home-assistant/actions/helpers/info@master with: - path: "./${{ matrix.addon }}/" + path: "./${{ matrix.addon }}" - - name: đŸ—„ī¸ Cache docker layers - uses: actions/cache@v2 - with: - path: /tmp/buildx-cache - key: ${{ runner.os }}-buildx-${{ matrix.addon }}-${{ hashFiles('**/Dockerfile') }} - restore-keys: ${{ runner.os }}-buildx-${{ matrix.addon }}- - - - name: 🔖 Create addon image tags - id: tags - shell: bash + - name: Check if add-on should be built + id: check run: | - imagetemplate=${{ steps.information.outputs.image }} - version=${{ steps.information.outputs.version }} - echo "Using imagetemplate '$imagetemplate'" - echo "::set-output name=armhf::${imagetemplate/\{arch\}/armhf}${version}" - echo "::set-output name=armv7::${imagetemplate/\{arch\}/armv7}${version}" - echo "::set-output name=aarch64::${imagetemplate/\{arch\}/aarch64}${version}" - echo "::set-output name=amd64::${imagetemplate/\{arch\}/amd64}${version}" - echo "::set-output name=i386::${imagetemplate/\{arch\}/i386}${version}" - - name: đŸˇī¸ Create addon labels - id: labels - shell: bash - run: | - labels="io.hass.version=${{ steps.information.outputs.version }}" - labels=$(printf "$labels\nio.hass.name=${{ steps.information.outputs.name }}") - labels=$(printf "$labels\nio.hass.description=${{ steps.information.outputs.description }}") - labels=$(printf "$labels\nio.hass.type=addon") - labels=$(printf "$labels\nio.hass.url=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/tree/master/${{ matrix.addon }}") - labels=$(printf "$labels\norg.opencontainers.image.title=${{ steps.information.outputs.name }}") - labels=$(printf "$labels\norg.opencontainers.image.description=${{ steps.information.outputs.description }}") - labels=$(printf "$labels\norg.opencontainers.image.version=${{ steps.information.outputs.version }}") - labels=$(printf "$labels\norg.opencontainers.image.authors=alexbelgium ") - labels=$(printf "$labels\norg.opencontainers.image.url=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}") - labels=$(printf "$labels\norg.opencontainers.image.source=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/tree/master/${{ matrix.addon }}") - labels=$(printf "$labels\norg.opencontainers.image.created=$(date -Is)") - labels=$(printf "$labels\norg.opencontainers.image.revision=${GITHUB_SHA}") - echo "Generic labels: $labels" - armhf_labels=$(printf "$labels\nio.hass.arch=armhf") - armv7_labels=$(printf "$labels\nio.hass.arch=armv7") - aarch64_labels=$(printf "$labels\nio.hass.arch=aarch64") - amd64_labels=$(printf "$labels\nio.hass.arch=amd64") - i386_labels=$(printf "$labels\nio.hass.arch=i386") - # allow multiline outputs, see https://github.community/t/set-output-truncates-multiline-strings/16852 - armhf_labels="${armhf_labels//$'\n'/'%0A'}" - armv7_labels="${armv7_labels//$'\n'/'%0A'}" - aarch64_labels="${aarch64_labels//$'\n'/'%0A'}" - amd64_labels="${amd64_labels//$'\n'/'%0A'}" - i386_labels="${i386_labels//$'\n'/'%0A'}" - echo "::set-output name=armhf::$armhf_labels" - echo "::set-output name=armv7::$armv7_labels" - echo "::set-output name=aarch64::$aarch64_labels" - echo "::set-output name=amd64::$amd64_labels" - echo "::set-output name=i386::$i386_labels" - - name: đŸ’Ŋ Create addon build-args - id: build_args - shell: bash - run: | - echo "::set-output name=armhf::BUILD_FROM=$(jq -r '.build_from.armhf // empty' ${{ steps.information.outputs.build }})" - echo "::set-output name=armv7::BUILD_FROM=$(jq -r '.build_from.armv7 // empty' ${{ steps.information.outputs.build }})" - echo "::set-output name=aarch64::BUILD_FROM=$(jq -r '.build_from.aarch64 // empty' ${{ steps.information.outputs.build }})" - echo "::set-output name=amd64::BUILD_FROM=$(jq -r '.build_from.amd64 // empty' ${{ steps.information.outputs.build }})" - echo "::set-output name=i386::BUILD_FROM=$(jq -r '.build_from.i386 // empty' ${{ steps.information.outputs.build }})" - - name: đŸ—ī¸ Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: đŸ—ī¸ Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: đŸ’ŋ Build Addon - armhf - if: ${{ steps.information.outputs.armhf == 'true' }} - uses: docker/build-push-action@v2 - with: - context: ${{ matrix.addon }} - push: false - load: true - file: ${{ matrix.addon }}/Dockerfile - tags: ${{ steps.tags.outputs.armhf }} - labels: | - ${{ steps.labels.outputs.armhf }} - build-args: ${{ steps.build_args.outputs.armhf }} - cache-from: type=local,src=/tmp/buildx-cache/armhf - cache-to: type=local,dest=/tmp/buildx-cache-new/armhf - - - name: đŸ’ŋ Build Addon - armv7 - if: ${{ steps.information.outputs.armv7 == 'true' }} - uses: docker/build-push-action@v2 - with: - context: ${{ matrix.addon }} - push: false - load: true - file: ${{ matrix.addon }}/Dockerfile - tags: ${{ steps.tags.outputs.armv7 }} - labels: | - ${{ steps.labels.outputs.armv7 }} - build-args: ${{ steps.build_args.outputs.armv7 }} - cache-from: type=local,src=/tmp/buildx-cache/armv7 - cache-to: type=local,dest=/tmp/buildx-cache-new/armv7 - - - name: đŸ’ŋ Build Addon - aarch64 - if: ${{ steps.information.outputs.aarch64 == 'true' }} - uses: docker/build-push-action@v2 - with: - context: ${{ matrix.addon }} - push: false - load: true - file: ${{ matrix.addon }}/Dockerfile - tags: ${{ steps.tags.outputs.aarch64 }} - labels: | - ${{ steps.labels.outputs.aarch64 }} - build-args: ${{ steps.build_args.outputs.aarch64 }} - cache-from: type=local,src=/tmp/buildx-cache/aarch64 - cache-to: type=local,dest=/tmp/buildx-cache-new/aarch64 - - - name: đŸ’ŋ Build Addon - amd64 - if: ${{ steps.information.outputs.amd64 == 'true' }} - uses: docker/build-push-action@v2 - with: - context: ${{ matrix.addon }} - push: false - load: true - file: ${{ matrix.addon }}/Dockerfile - tags: ${{ steps.tags.outputs.amd64 }} - labels: | - ${{ steps.labels.outputs.amd64 }} - build-args: ${{ steps.build_args.outputs.amd64 }} - cache-from: type=local,src=/tmp/buildx-cache/amd64 - cache-to: type=local,dest=/tmp/buildx-cache-new/amd64 - - - name: đŸ’ŋ Build Addon - i386 - if: ${{ steps.information.outputs.i386 == 'true' }} - uses: docker/build-push-action@v2 - with: - context: ${{ matrix.addon }} - push: false - load: true - file: ${{ matrix.addon }}/Dockerfile - tags: ${{ steps.tags.outputs.i386 }} - labels: | - ${{ steps.labels.outputs.i386 }} - build-args: ${{ steps.build_args.outputs.i386 }} - cache-from: type=local,src=/tmp/buildx-cache/i386 - cache-to: type=local,dest=/tmp/buildx-cache-new/i386 - - # Fix for https://github.com/docker/build-push-action/issues/252 - - name: đŸ—„ī¸ Update cache Folder - run: | - rm -rf /tmp/buildx-cache - mv /tmp/buildx-cache-new /tmp/buildx-cache - - name: 🔑 GHCR Login + if [[ "${{ steps.info.outputs.architectures }}" =~ ${{ matrix.arch }} ]]; then + echo "::set-output name=build_arch::true"; + echo "::set-output name=image::$(echo ${{ steps.info.outputs.image }} | cut -d'/' -f3)"; + if [[ -z "${{ github.head_ref }}" ]] && [[ "${{ github.event_name }}" == "push" ]]; then + echo "BUILD_ARGS=" >> $GITHUB_ENV; + fi + else + echo "${{ matrix.arch }} is not a valid arch for ${{ matrix.addon }}, skipping build"; + echo "::set-output name=build_arch::false"; + fi + - name: Login to GitHub Container Registry + if: env.BUILD_ARGS != '--test' uses: docker/login-action@v1.12.0 with: registry: ghcr.io username: ${{ github.repository_owner }} - password: ${{ secrets.CR_PAT }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: 🚀 Push images - shell: bash - run: | - if [ "${{ steps.information.outputs.armhf }}" = 'true' ]; then - docker push ${{ steps.tags.outputs.armhf }} - fi - if [ "${{ steps.information.outputs.armv7 }}" = 'true' ]; then - docker push ${{ steps.tags.outputs.armv7 }} - fi - if [ "${{ steps.information.outputs.aarch64 }}" = 'true' ]; then - docker push ${{ steps.tags.outputs.aarch64 }} - fi - if [ "${{ steps.information.outputs.amd64 }}" = 'true' ]; then - docker push ${{ steps.tags.outputs.amd64 }} - fi - if [ "${{ steps.information.outputs.i386 }}" = 'true' ]; then - docker push ${{ steps.tags.outputs.i386 }} - fi + - name: Build ${{ matrix.addon }} add-on + if: steps.check.outputs.build_arch == 'true' + uses: home-assistant/builder@2021.12.0 + with: + args: | + ${{ env.BUILD_ARGS }} \ + --${{ matrix.arch }} \ + --target /data/${{ matrix.addon }} \ + --image "${{ steps.check.outputs.image }}" \ + --docker-hub "ghcr.io/${{ github.repository_owner }}" \ + --addon