From e8c8e1e4ad26a1d8121883d93eba180992b606b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:19:17 +0200 Subject: [PATCH 01/18] initialize the pipeline --- .github/workflows/ci.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..03cc95a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,23 @@ +# This file will be used when creating PRs to the develop branch. +# It should run the tests without building the docker images. + +name: CI +on: + pull_request: + branches: [develop] # If more branches are needed, add them here. + types: [synchronize, opened, reopened, ready_for_review] +concurrency: + group: "${{ github.workflow }}-${{ github.ref }}" + cancel-in-progress: true +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v2 + with: + python-version: 3.12 + - name: Install dependencies + run: | + python -m pip install pdm + pdm install From 99c9473faa64af1b22421793f8cde92f589d62cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:21:58 +0200 Subject: [PATCH 02/18] Add pytest step --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03cc95a..ecbdf85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,3 +21,6 @@ jobs: run: | python -m pip install pdm pdm install + - name: Run tests + run: | + pdm run pytest \ No newline at end of file From d3fa8857947f407b91cc5bddd6d9d1b6d1e6a30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:27:19 +0200 Subject: [PATCH 03/18] start db before pytest. --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ecbdf85..0178f42 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,4 +23,5 @@ jobs: pdm install - name: Run tests run: | + docker compose run --rm -d db pdm run pytest \ No newline at end of file From 5bab7d72220846c389d11ae68f0f76a00619228b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:30:00 +0200 Subject: [PATCH 04/18] add caching --- .github/workflows/ci.yml | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0178f42..1a6f4c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,15 +13,23 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v2 - with: - python-version: 3.12 - - name: Install dependencies - run: | - python -m pip install pdm - pdm install - - name: Run tests - run: | - docker compose run --rm -d db - pdm run pytest \ No newline at end of file + - uses: actions/checkout@v4 + - uses: actions/setup-python@v2 + with: + python-version: 3.12 + - name: Cache PDM packages + uses: actions/cache@v4 + with: + path: __pypackages__ + key: ${{ runner.os }}-pdm-${{ hashFiles('pdm.lock') }} + restore-keys: | + ${{ runner.os }}-pdm- + - name: Install dependencies + run: | + python -m pip install pdm + pdm install + - name: Run tests + run: | + docker compose run --rm -d db + nc --help + pdm run pytest From 172b7c45283131a81f5a939b20072db3b3fd1692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:03:42 +0200 Subject: [PATCH 05/18] wait for db --- .github/workflows/ci.yml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1a6f4c1..8edc0ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,8 +28,14 @@ jobs: run: | python -m pip install pdm pdm install - - name: Run tests + ls -la ./ + - name: Start db + run: docker compose run --rm -d db + - name: Wait for db run: | - docker compose run --rm -d db - nc --help - pdm run pytest + for i in {1..30}; do + nc -zv localhost 5432 && break + sleep 1 + done + - name: Run tests + run: pdm run pytest From 409004930dfba098fbbe287617eb3d066da37c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:08:08 +0200 Subject: [PATCH 06/18] fail if db is not available --- .github/workflows/ci.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8edc0ed..c84ac76 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,9 +33,16 @@ jobs: run: docker compose run --rm -d db - name: Wait for db run: | - for i in {1..30}; do - nc -zv localhost 5432 && break + docker ps -a + for i in {1..60}; do + if nc -zv localhost 5432; then + echo "Database is up!" + exit 0 + fi + echo "Waiting for database..." sleep 1 done + echo "Database is not available." + exit 1 - name: Run tests run: pdm run pytest From b041cdbeab3e41a53e7488655731f3ead9f97076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:10:55 +0200 Subject: [PATCH 07/18] look for packages in .local/bin --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c84ac76..cdc3977 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,7 @@ jobs: python -m pip install pdm pdm install ls -la ./ + ls -la ~HOME/.local/bin - name: Start db run: docker compose run --rm -d db - name: Wait for db @@ -43,6 +44,7 @@ jobs: sleep 1 done echo "Database is not available." + docker ps -a exit 1 - name: Run tests run: pdm run pytest From 8e521fd60c4bd0a93177ce45108c92c097b06f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:12:04 +0200 Subject: [PATCH 08/18] fix typo --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdc3977..74f2901 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: python -m pip install pdm pdm install ls -la ./ - ls -la ~HOME/.local/bin + ls -la $HOME/.local/bin - name: Start db run: docker compose run --rm -d db - name: Wait for db From 0875100f720f16f5ba184af65b51663d2a0c1a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:13:56 +0200 Subject: [PATCH 09/18] pdm info --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74f2901..f901eaf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,8 @@ jobs: python -m pip install pdm pdm install ls -la ./ - ls -la $HOME/.local/bin + pdm info + pdm list - name: Start db run: docker compose run --rm -d db - name: Wait for db From eaf1b35a249ff34cc10a6ad3b895fa2241993ea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:22:45 +0200 Subject: [PATCH 10/18] venv in cache --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f901eaf..c07d65a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: - name: Cache PDM packages uses: actions/cache@v4 with: - path: __pypackages__ + path: .venv key: ${{ runner.os }}-pdm-${{ hashFiles('pdm.lock') }} restore-keys: | ${{ runner.os }}-pdm- From 33ff57ff6139ab6aa04045f07ec127aff929fa38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Mon, 16 Sep 2024 15:11:05 +0200 Subject: [PATCH 11/18] use compose exec for checking if db is healthy --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c07d65a..19283c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: run: | docker ps -a for i in {1..60}; do - if nc -zv localhost 5432; then + if docker compose exec db pg_isready -U hcw -d hope_country_workspace; then echo "Database is up!" exit 0 fi From f5cf1e8937241d1c7f5681004d42cfb655c0a76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Tue, 17 Sep 2024 10:41:23 +0200 Subject: [PATCH 12/18] build docker image in ci --- .github/docker/compose.ci.yml | 60 +++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 47 +++++++-------------------- 2 files changed, 71 insertions(+), 36 deletions(-) create mode 100644 .github/docker/compose.ci.yml diff --git a/.github/docker/compose.ci.yml b/.github/docker/compose.ci.yml new file mode 100644 index 0000000..3116bf4 --- /dev/null +++ b/.github/docker/compose.ci.yml @@ -0,0 +1,60 @@ +services: + tests: + image: ${image} + environment: + - DEBUG=False + - ADMIN_EMAIL=adm@hcw.org + - ADMIN_PASSWORD=123 + - CACHE_URL=redis://redis:6379/1 + - CELERY_BROKER_URL=redis://redis:6379/9 + - CELERY_TASK_ALWAYS_EAGER=False + - DATABASE_URL=postgres://hcw:password@db:5432/hope_country_workspace + - DJANGO_SETTINGS_MODULE=hope_country_workspace.config.settings + - MEDIA_ROOT=/var/hope_country_workspace/media + - PYTHONPATH=/code/src/:/code/__pypackages__/3.12/lib/ + - SECRET_KEY=sensitive-secret-key + - STATIC_ROOT=/var/hope_country_workspace/static + restart: no + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + command: > + bash -c " + pytest + " + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8000/healthcheck"] + interval: 10s + timeout: 5s + retries: 5 + + db: + image: postgres:16 + network_mode: host + environment: + - POSTGRES_USER=hcw + - POSTGRES_PASSWORD=password + - POSTGRES_DB=hope_country_workspace + restart: always + healthcheck: + test: ["CMD", "pg_isready", "-U", "hcw", "-d", "hope_country_workspace"] + start_period: 5s + start_interval: 1s + interval: 5s + timeout: 4s + retries: 5 + + redis: + image: redis:7.2 + ports: + - 6379:6379 + restart: always + healthcheck: + test: ["CMD", "redis-cli", "ping"] + start_period: 5s + start_interval: 1s + interval: 5s + timeout: 4s + retries: 5 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19283c4..016e908 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,3 @@ -# This file will be used when creating PRs to the develop branch. -# It should run the tests without building the docker images. - name: CI on: pull_request: @@ -14,38 +11,16 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: actions/setup-python@v2 - with: - python-version: 3.12 - - name: Cache PDM packages - uses: actions/cache@v4 - with: - path: .venv - key: ${{ runner.os }}-pdm-${{ hashFiles('pdm.lock') }} - restore-keys: | - ${{ runner.os }}-pdm- - - name: Install dependencies + - name: Build the Docker image run: | - python -m pip install pdm - pdm install - ls -la ./ - pdm info - pdm list - - name: Start db - run: docker compose run --rm -d db - - name: Wait for db + docker buildx create --use + + docker buildx build \ + --tag unicef/hope_country_workspace:${{ github.sha }} \ + --load \ + --target python_dev_deps \ + -f ./docker/Dockerfile \ + ./ + - name: Run the tests run: | - docker ps -a - for i in {1..60}; do - if docker compose exec db pg_isready -U hcw -d hope_country_workspace; then - echo "Database is up!" - exit 0 - fi - echo "Waiting for database..." - sleep 1 - done - echo "Database is not available." - docker ps -a - exit 1 - - name: Run tests - run: pdm run pytest + docker compose -f ./.github/docker/compose.ci.yml up --exit-code-from tests \ No newline at end of file From b4a0e1d1be5fdff076e4de4259f54a88b2689e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Tue, 17 Sep 2024 10:46:09 +0200 Subject: [PATCH 13/18] add caching --- .github/workflows/ci.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 016e908..0919c5d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,10 +11,16 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- - name: Build the Docker image run: | docker buildx create --use - docker buildx build \ --tag unicef/hope_country_workspace:${{ github.sha }} \ --load \ @@ -23,4 +29,4 @@ jobs: ./ - name: Run the tests run: | - docker compose -f ./.github/docker/compose.ci.yml up --exit-code-from tests \ No newline at end of file + image=unicef/hope_country_workspace:${{ github.sha }} docker compose -f ./.github/docker/compose.ci.yml up --exit-code-from tests \ No newline at end of file From e7b79e1e3c4579cab5332e418953b0d835566fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:14:08 +0200 Subject: [PATCH 14/18] mount tests --- .github/docker/compose.ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/docker/compose.ci.yml b/.github/docker/compose.ci.yml index 3116bf4..afb2b81 100644 --- a/.github/docker/compose.ci.yml +++ b/.github/docker/compose.ci.yml @@ -22,8 +22,10 @@ services: condition: service_healthy command: > bash -c " - pytest + pytest -vvv ./tests " + volumes: + - ../../tests:/code/tests healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8000/healthcheck"] interval: 10s @@ -32,7 +34,6 @@ services: db: image: postgres:16 - network_mode: host environment: - POSTGRES_USER=hcw - POSTGRES_PASSWORD=password From 6cbe3a90f1d4fb0db7be853cc655c7b69e2a0abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:21:25 +0200 Subject: [PATCH 15/18] caching --- .github/workflows/ci.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0919c5d..c491b11 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Ensure cache directory exists + run: mkdir -p /tmp/.buildx-cache - name: Cache Docker layers uses: actions/cache@v3 with: @@ -25,8 +27,10 @@ jobs: --tag unicef/hope_country_workspace:${{ github.sha }} \ --load \ --target python_dev_deps \ + --cache-from=type=local,src=/tmp/.buildx-cache \ + --cache-to=type=local,dest=/tmp/.buildx-cache \ -f ./docker/Dockerfile \ ./ - name: Run the tests - run: | - image=unicef/hope_country_workspace:${{ github.sha }} docker compose -f ./.github/docker/compose.ci.yml up --exit-code-from tests \ No newline at end of file + run: |- + image=unicef/hope_country_workspace:${{ github.sha }} docker compose -f ./.github/docker/compose.ci.yml up --exit-code-from tests From 841d46ef3614eb6255a3da5209bee1d56b39ad28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:25:32 +0200 Subject: [PATCH 16/18] empty From ce8296b1e8f388c7b1a7da42e93ec6dc0d630060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:58:02 +0200 Subject: [PATCH 17/18] add on push to develop rule --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c491b11..2ad3a1c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,8 @@ on: pull_request: branches: [develop] # If more branches are needed, add them here. types: [synchronize, opened, reopened, ready_for_review] + push: + branches: [develop] # If more branches are needed, add them here. concurrency: group: "${{ github.workflow }}-${{ github.ref }}" cancel-in-progress: true From a92a798014e0f2d021a33b9a1d4960a883de0a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wo=C5=BAniak?= <17177420+wozniakpl@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:16:40 +0200 Subject: [PATCH 18/18] add coverage --- .github/docker/compose.ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/docker/compose.ci.yml b/.github/docker/compose.ci.yml index afb2b81..f0dcaad 100644 --- a/.github/docker/compose.ci.yml +++ b/.github/docker/compose.ci.yml @@ -22,7 +22,7 @@ services: condition: service_healthy command: > bash -c " - pytest -vvv ./tests + pytest -vvv --cov=src ./tests " volumes: - ../../tests:/code/tests