From 85b934e8f99991c488ac0eccc426142938ea8d7f Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sat, 30 Sep 2023 11:13:48 +0200 Subject: [PATCH] feat(tool,ci): Cache docker images Signed-off-by: jld3103 --- .github/workflows/dart.yml | 30 ++++++++++++++++------------- .github/workflows/publish.yaml | 14 ++++++++------ tool/build-app.sh | 18 ++++------------- tool/build-dev-container.sh | 6 +++++- tool/common.sh | 22 +++++++++++++++++++++ tool/dev.sh | 3 ++- tool/generate-screenshots.sh | 4 +++- tool/send-push-notification-test.sh | 4 +++- 8 files changed, 64 insertions(+), 37 deletions(-) create mode 100644 tool/common.sh diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index c115c9e0..84c1545a 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -4,12 +4,6 @@ on: branches: - main pull_request: -defaults: - run: - shell: bash -env: - PUB_ENVIRONMENT: bot.github -permissions: read-all concurrency: group: dart-${{ github.head_ref || github.run_id }} @@ -19,21 +13,31 @@ jobs: dart-ci: name: Dart CI runs-on: ubuntu-22.04 + permissions: + packages: write steps: + - name: Checkout + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4 + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3 + - name: Build test Docker image + run: ./tool/build-dev-container.sh + + - name: Install dart + uses: dart-lang/setup-dart@8a4b97ea2017cc079571daec46542f76189836b1 # v1 - name: Cache dependencies uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3 with: path: ~/.pub-cache/hosted key: dart-ci - - name: Checkout repository - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4 - - - name: Install dart - uses: dart-lang/setup-dart@8a4b97ea2017cc079571daec46542f76189836b1 # v1 - name: Setup run: ./tool/setup.sh - - name: Build test Docker image - run: ./tool/build-dev-container.sh - name: Check formatting run: melos run format-check diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index eef46f10..cd3bec59 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -144,15 +144,17 @@ jobs: name: Linux arm64 runs-on: ubuntu-22.04 needs: setup + permissions: + packages: write steps: - name: Checkout uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4 - - name: Set docker image - id: docker_image - run: | - echo "local=nextcloud-neon-build-linux-arm64:${{ needs.setup.outputs.flutter_version }}" >> $GITHUB_OUTPUT - echo "remote=ghcr.io/$GITHUB_REPOSITORY/build-linux-arm64:${{ needs.setup.outputs.flutter_version }}" >> $GITHUB_OUTPUT - - run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Set up QEMU uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3 with: diff --git a/tool/build-app.sh b/tool/build-app.sh index fd5fd9d8..bd3435be 100755 --- a/tool/build-app.sh +++ b/tool/build-app.sh @@ -1,6 +1,7 @@ #!/bin/bash set -euxo pipefail cd "$(dirname "$0")/.." +source tool/common.sh targets=("linux/arm64" "linux/amd64") @@ -21,26 +22,15 @@ if [[ "$target" == "linux/arm64" ]] || [[ "$target" == "linux/amd64" ]]; then os="$(echo "$target" | cut -d "/" -f 1)" arch="$(echo "$target" | cut -d "/" -f 2)" - build_args=() - if [ -v GITHUB_REPOSITORY ]; then - image="ghcr.io/$GITHUB_REPOSITORY/build-$os-$arch" - build_args+=( - "--push" - "--cache-from" "type=registry,ref=$image" - "--cache-to" "type=registry,ref=$image,mode=max" - ) - tag="$image:$FLUTTER_VERSION" - else - tag="nextcloud-neon-build-$os-$arch:$FLUTTER_VERSION" - fi + tag="$(image_tag "build:$os-$arch")" - # shellcheck disable=SC2086 + # shellcheck disable=SC2046 docker buildx build \ --platform "$target" \ --progress plain \ --tag "$tag" \ --build-arg="FLUTTER_VERSION=$FLUTTER_VERSION" \ - ${build_args[*]} \ + $(cache_build_args "$tag") \ -f "tool/build/Dockerfile.$os" \ ./tool/build diff --git a/tool/build-dev-container.sh b/tool/build-dev-container.sh index 9df071bc..ce091348 100755 --- a/tool/build-dev-container.sh +++ b/tool/build-dev-container.sh @@ -1,5 +1,9 @@ #!/bin/bash set -euxo pipefail cd "$(dirname "$0")/.." +source tool/common.sh -docker build -t nextcloud-neon-dev -f - ./tool < tool/Dockerfile.dev +tag="$(image_tag "dev:latest")" + +# shellcheck disable=SC2046 +docker buildx build --tag "$tag" $(cache_build_args "$tag") -f - ./tool < tool/Dockerfile.dev diff --git a/tool/common.sh b/tool/common.sh new file mode 100644 index 00000000..fde88841 --- /dev/null +++ b/tool/common.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -euxo pipefail + +function image_tag() { + name="$1" + echo "ghcr.io/${GITHUB_REPOSITORY:-nextcloud/neon}/$name" +} + +function cache_build_args() { + tag="$1" + build_args=("--cache-from" "type=registry,ref=$tag") + if [ -v GITHUB_REPOSITORY ]; then + build_args+=( + "--push" + "--cache-to" "type=registry,ref=$tag,mode=max" + ) + else + build_args+=("--load") + fi + + echo "${build_args[*]}" +} diff --git a/tool/dev.sh b/tool/dev.sh index 5d083b03..a08d3e79 100755 --- a/tool/dev.sh +++ b/tool/dev.sh @@ -1,8 +1,9 @@ #!/bin/bash set -euxo pipefail cd "$(dirname "$0")/.." +source tool/common.sh ./tool/build-dev-container.sh echo "Running development instance on http://localhost. To access it in an Android Emulator use http://10.0.2.2" -docker run --rm -v nextcloud-neon-dev:/usr/src/nextcloud -v nextcloud-neon-dev:/var/www/html -p "80:80" --add-host=host.docker.internal:host-gateway nextcloud-neon-dev +docker run --rm -v nextcloud-neon-dev:/usr/src/nextcloud -v nextcloud-neon-dev:/var/www/html -p "80:80" --add-host=host.docker.internal:host-gateway "$(image_tag "dev:latest")" diff --git a/tool/generate-screenshots.sh b/tool/generate-screenshots.sh index 7ef1d6c6..4fef20aa 100755 --- a/tool/generate-screenshots.sh +++ b/tool/generate-screenshots.sh @@ -1,9 +1,11 @@ #!/bin/bash set -euxo pipefail cd "$(dirname "$0")/.." +source tool/common.sh ./tool/build-dev-container.sh -container_id="$(docker run --rm -d -p "80:80" nextcloud-neon-dev)" + +container_id="$(docker run --rm -d -p "80:80" "$(image_tag "dev:latest")")" function cleanup() { docker kill "$container_id" } diff --git a/tool/send-push-notification-test.sh b/tool/send-push-notification-test.sh index f6341d3b..9d25ac08 100755 --- a/tool/send-push-notification-test.sh +++ b/tool/send-push-notification-test.sh @@ -1,4 +1,6 @@ #!/bin/bash set -euxo pipefail +cd "$(dirname "$0")/.." +source tool/common.sh -docker exec -it "$(docker ps | grep nextcloud-neon-dev | cut -d " " -f 1)" /bin/bash -c "php -f occ notification:test-push $*" +docker exec -it "$(docker ps | grep "$(image_tag "dev:latest")" | cut -d " " -f 1)" /bin/bash -c "php -f occ notification:test-push $*"