From 68c0122650231d914e604d9e5184d9e110d64330 Mon Sep 17 00:00:00 2001 From: Splines Date: Wed, 10 Jan 2024 16:28:19 +0100 Subject: [PATCH] Rename `run_cypress_tests` to `test` & use buildx buildx is used for faster builds we also set up ghcr to use docker layer caching for hopefully even faster build times --- .github/workflows/docker-compose-cache.json | 70 +++++++++++++++++++ .github/workflows/tests.yml | 69 ++++++++++++++++++ docker/{run_cypress_tests => test}/Dockerfile | 0 .../Dockerfile_cypress | 0 .../docker-compose.local.yml | 6 +- .../docker-compose.yml | 6 +- .../{run_cypress_tests => test}/run_tests.sh | 0 7 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/docker-compose-cache.json create mode 100644 .github/workflows/tests.yml rename docker/{run_cypress_tests => test}/Dockerfile (100%) rename docker/{run_cypress_tests => test}/Dockerfile_cypress (100%) rename docker/{run_cypress_tests => test}/docker-compose.local.yml (93%) rename docker/{run_cypress_tests => test}/docker-compose.yml (93%) rename docker/{run_cypress_tests => test}/run_tests.sh (100%) diff --git a/.github/workflows/docker-compose-cache.json b/.github/workflows/docker-compose-cache.json new file mode 100644 index 000000000..ce6df524a --- /dev/null +++ b/.github/workflows/docker-compose-cache.json @@ -0,0 +1,70 @@ +{ + "target": { + "redis": { + "cache-from": [ + "type=registry,ref=ghcr.io/MaMpf-HD/redis:cache" + ], + "cache-to": [ + "type=registry,ref=ghcr.io/MaMpf-HD/redis:cache" + ], + "output": [ + "type=docker" + ] + }, + "solr": { + "cache-from": [ + "type=registry,ref=ghcr.io/MaMpf-HD/solr:cache" + ], + "cache-to": [ + "type=registry,ref=ghcr.io/MaMpf-HD/solr:cache" + ], + "output": [ + "type=docker" + ] + }, + "db": { + "cache-from": [ + "type=registry,ref=ghcr.io/MaMpf-HD/db:cache" + ], + "cache-to": [ + "type=registry,ref=ghcr.io/MaMpf-HD/db:cache" + ], + "output": [ + "type=docker" + ] + }, + "mailcatcher": { + "cache-from": [ + "type=registry,ref=ghcr.io/MaMpf-HD/mailcatcher:cache" + ], + "cache-to": [ + "type=registry,ref=ghcr.io/MaMpf-HD/mailcatcher:cache" + ], + "output": [ + "type=docker" + ] + }, + "mampf": { + "cache-from": [ + "type=registry,ref=ghcr.io/MaMpf-HD/mampf:cache" + ], + "cache-to": [ + "type=registry,ref=ghcr.io/MaMpf-HD/mampf:cache" + ], + "output": [ + "type=docker" + ] + }, + "cypress_runner": { + "cache-from": [ + "type=registry,ref=ghcr.io/MaMpf-HD/cypress_runner:cache" + ], + "cache-to": [ + "type=registry,ref=ghcr.io/MaMpf-HD/cypress_runner:cache" + ], + "output": [ + "type=docker" + ] + } + } +} \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 000000000..cdc3c3aea --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,69 @@ +name: Tests + +on: + push: + branches: + - main + - dev + - experimental + pull_request: + types: [opened, reopened, synchronize, ready_for_review] + +jobs: + unit-test-job: + name: Unit tests + runs-on: ubuntu-latest + steps: + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Build docker images + run: docker buildx bake --file docker/test/docker-compose.yml --file .github/workflows/docker-compose-cache.json + + # For docker layer caching also see this article [1] + # - name: Build docker images + # uses: docker/build-push-action@v5 + # with: + # push: false # only build + # context: . + # cache-from: type=gha + # cache-to: type=gha,mode=max + + - name: Create and migrate DB + run: docker compose run --entrypoint "" mampf sh -c "rake db:create db:migrate db:test:prepare" + working-directory: docker/test + + - name: Reindex sunspot + working-directory: docker/test + run: | + docker compose run --entrypoint="" mampf sh -c "RAILS_ENV=test rake sunspot:reindex" + + - name: Run unit tests + working-directory: docker/test + run: docker compose run --entrypoint="" mampf sh -c "RAILS_ENV=test rails spec" + + - name: Send test coverage report to Codecov + uses: codecov/codecov-action@v3 + with: + files: ./coverage/coverage.xml + fail_ci_if_error: true + verbose: true + +# [1] https://depot.dev/blog/docker-layer-caching-in-github-actions#docker-layer-caching-in-github-actions +# https://stackoverflow.com/questions/61491484/how-to-cache-docker-compose-build-inside-github-action +# https://docs.docker.com/build/ci/github-actions/cache/ +# https://github.com/docker/build-push-action/issues/493#issuecomment-961559685 +# https://github.com/orgs/community/discussions/25728 +# https://www.deploysentinel.com/blog/docker-buildx-cache-with-github-actions diff --git a/docker/run_cypress_tests/Dockerfile b/docker/test/Dockerfile similarity index 100% rename from docker/run_cypress_tests/Dockerfile rename to docker/test/Dockerfile diff --git a/docker/run_cypress_tests/Dockerfile_cypress b/docker/test/Dockerfile_cypress similarity index 100% rename from docker/run_cypress_tests/Dockerfile_cypress rename to docker/test/Dockerfile_cypress diff --git a/docker/run_cypress_tests/docker-compose.local.yml b/docker/test/docker-compose.local.yml similarity index 93% rename from docker/run_cypress_tests/docker-compose.local.yml rename to docker/test/docker-compose.local.yml index 7e704f774..4a3e87fe4 100644 --- a/docker/run_cypress_tests/docker-compose.local.yml +++ b/docker/test/docker-compose.local.yml @@ -30,12 +30,12 @@ services: mampf: build: context: ./../.. - dockerfile: docker/run_cypress_tests/Dockerfile + dockerfile: docker/test/Dockerfile image: mampf:tests ports: - "127.0.0.1:3000:3000" # TODO: Use this - # entrypoint: /usr/src/app/docker/run_cypress_tests/run_tests.sh + # entrypoint: /usr/src/app/docker/test/run_tests.sh entrypoint: ./entrypoint.sh environment: RAILS_ENV: test @@ -75,7 +75,7 @@ services: image: mampf-cypress build: context: ./../.. - dockerfile: docker/run_cypress_tests/Dockerfile_cypress + dockerfile: docker/test/Dockerfile_cypress environment: CYPRESS_baseUrl: http://mampf:3000 entrypoint: bash -c "while ! curl -s $$CYPRESS_baseUrl > /dev/null; do echo waiting for MaMpf to come online at $$CYPRESS_baseUrl; sleep 1; done; npx cypress run $$@" diff --git a/docker/run_cypress_tests/docker-compose.yml b/docker/test/docker-compose.yml similarity index 93% rename from docker/run_cypress_tests/docker-compose.yml rename to docker/test/docker-compose.yml index 70603e1cd..e303768cb 100644 --- a/docker/run_cypress_tests/docker-compose.yml +++ b/docker/test/docker-compose.yml @@ -36,10 +36,10 @@ services: mampf: build: context: ./../.. - dockerfile: docker/run_cypress_tests/Dockerfile + dockerfile: docker/test/Dockerfile image: mampf:tests # TODO: Use this - # entrypoint: /usr/src/app/docker/run_cypress_tests/run_tests.sh + # entrypoint: /usr/src/app/docker/test/run_tests.sh entrypoint: /usr/src/app/entrypoint.sh environment: RAILS_ENV: test @@ -83,7 +83,7 @@ services: image: mampf-cypress build: context: ./../.. - dockerfile: docker/run_cypress_tests/Dockerfile_cypress + dockerfile: docker/test/Dockerfile_cypress environment: CYPRESS_baseUrl: http://mampf:3000 volumes: diff --git a/docker/run_cypress_tests/run_tests.sh b/docker/test/run_tests.sh similarity index 100% rename from docker/run_cypress_tests/run_tests.sh rename to docker/test/run_tests.sh