refactor: initial refactor #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: lint-and-test | |
on: [pull_request] | |
env: | |
DOCKER_FILE_DIRECTORY: environments | |
DOCKER_COMPOSE_DIRECTORY: environments/ci | |
COMPOSE_DOCKER_CLI_BUILD: 1 | |
DOCKER_BUILDKIT: 1 | |
USE_CACHE: true | |
jobs: | |
lint-and-test: | |
name: ${{ matrix.os }} / ${{ matrix.python-version }} | |
runs-on: ${{ matrix.image }} | |
timeout-minutes: 10 | |
strategy: | |
fail-fast: false | |
matrix: | |
os: [ubuntu] | |
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] | |
include: | |
- os: ubuntu | |
image: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Check version info | |
run: pwd && docker compose --version && docker --version | |
# Provide a builder instance with docker buildx. | |
- name: Set up Docker Buildx | |
if: env.USE_CACHE == 'true' | |
uses: docker/setup-buildx-action@v3 | |
# Specify a cache directory for build cache, and reuse past build cache as long as there are no changes to the environment files. | |
- name: Cache Docker layers | |
if: env.USE_CACHE == 'true' | |
id: build-cache | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/.buildx-cache/${{ matrix.python-version }} | |
key: buildx-${{ hashFiles(format('{0}/Dockerfile', env.DOCKER_FILE_DIRECTORY), format('{0}/docker-compose.yaml', env.DOCKER_COMPOSE_DIRECTORY), 'poetry.lock') }} | |
# If build cache is found, use it to build the Docker image. | |
# The build is carried out using the `bake` command from buildx, referencing the `docker-compose.yaml` for CI. | |
- name: Build docker image with cache using buildx | |
if: steps.build-cache.outputs.cache-hit == 'true' && env.USE_CACHE == 'true' | |
uses: docker/bake-action@v4 | |
with: | |
files: docker-compose.yaml | |
workdir: ${{ env.DOCKER_COMPOSE_DIRECTORY }} | |
load: true | |
set: | | |
core.args.PYTHON_VERSION=${{ matrix.python-version }} | |
*.cache-from=type=local,src=/tmp/.buildx-cache/${{ matrix.python-version }} | |
# If build cache is not found, build the Docker image without cache. | |
# It also works with docker/bake-action, but the `bake` command creates `/tmp/.buildx-cache/${{ matrix.python-version }}` as specified in cache-from. | |
# Since it is a cache directory tracked by actions/cache, if the directory exists at the end of the workflow, unnecessary cache is created in the merge branch. | |
# We used the regular build command to avoid this problem. | |
- name: Build docker image without cache using normal build | |
if: steps.build-cache.outputs.cache-hit != 'true' || env.USE_CACHE != 'true' | |
run: docker compose build --parallel --build-arg PYTHON_VERSION=${{ matrix.python-version }} core | |
working-directory: ${{ env.DOCKER_COMPOSE_DIRECTORY }} | |
- name: Create and start docker container | |
run: docker compose up --no-build -d | |
working-directory: ${{ env.DOCKER_COMPOSE_DIRECTORY }} | |
# pytest-cov export coverage data to a file | |
# However, the directory made by actions/checkout does not allow write | |
# chmod is needed to resolve above problem | |
- name: Change permission | |
run: chmod 777 . | |
# In the built stage of Docker image, .venv dir is moved from working directory to prevent | |
# overwrite by volume operation of Docker. Here, .venv is moved back to working directory. | |
- name: Move .venv directory | |
run: docker compose exec -T core mv ../.venv . | |
working-directory: ${{ env.DOCKER_COMPOSE_DIRECTORY }} | |
- name: Run lint | |
run: docker compose exec -T core poe lint | |
working-directory: ${{ env.DOCKER_COMPOSE_DIRECTORY }} | |
- name: Run test code | |
run: docker compose exec -T core poe test | |
working-directory: ${{ env.DOCKER_COMPOSE_DIRECTORY }} |