Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add unit tests & release workflows #24

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
# Dependency update will be run each week on monday
interval: "weekly"
labels:
- "dependencies"
ignore:
- dependency-name: "*"
update-types:
- "version-update:semver-major" # Ignore major updates

34 changes: 34 additions & 0 deletions .github/workflows/dependabot_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Dependabot PR Auto Approve and Merge

on:
pull_request:
types:
- opened

jobs:
tests:
if: ${{ github.actor == 'dependabot[bot]' && github.event.label.name == 'dependencies' }}
uses: ./.github/workflows/tests.yml
dependabot:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' && github.event.label.name == 'dependencies' }}
needs: [tests]

steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

- name: Approve a PR
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Enable auto merge
run: gh pr merge --auto --rebase "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
54 changes: 54 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: GHCR Release CI

on:
push:
tags:
- "[0-9]+.[0-9]+.[0-9]+"

jobs:
tests:
name: Release new Docker Image in GHCR
strategy:
matrix:
python-version: [3.12]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 60

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Upgrade pip version
run: |
python -m pip install -U pip

- run: echo "${HOME}/.local/bin" >> ${GITHUB_PATH}

- name: Install CI dependencies
run: python3 -m pip install --user --requirement=ci/requirements.txt

- name: Environment information
run: c2cciutils-env
env:
GITHUB_EVENT: ${{ toJson(github) }}

- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{github.repository_owner}}
password: ${{secrets.GITHUB_TOKEN}}

- name: Build Docker images for GHCR
run: make build_ghcr

- name: Push Docker images to GHCR
run: make push_ghcr
57 changes: 57 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Python unit tests

on:
push:
pull_request:
workflow_call:

jobs:
tests:
name: Execute unit tests
concurrency: ci-${{ github.ref }}
strategy:
matrix:
python-version: [3.12]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 60

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Upgrade pip version
run: |
python -m pip install -U pip

- run: echo "${HOME}/.local/bin" >> ${GITHUB_PATH}

- name: Install CI dependencies
run: python3 -m pip install --user --requirement=ci/requirements.txt

- name: Environment information
run: c2cciutils-env
env:
GITHUB_EVENT: ${{ toJson(github) }}

- name: Copy .env file
run: cp .env.sample .env

- name: Prepare Docker images and start Compose environment
run: make prepare_env

- name: Run tests
run: make test

- name: Docker logs
run: c2cciutils-docker-logs

- name: Stop Compose environment
run: make destroy_env
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
VERSION ?= $(shell git describe --always --tags)
DOCKER_TAG ?= latest

export DOCKER_BUILDKIT=1

.DEFAULT_GOAL := help

.PHONY: build
build: ## Build docker image
docker build --tag=camptocamp/geoshop-api:$(VERSION) \
--build-arg=VERSION=$(VERSION) .
docker tag camptocamp/geoshop-api:$(VERSION) camptocamp/geoshop-api:$(DOCKER_TAG)

.PHONY: build_ghcr
build_ghcr: ## Build docker image tagged for GHCR
docker build --tag=ghcr.io/camptocamp/geoshop-api:$(VERSION) \
--build-arg=VERSION=$(VERSION) .
docker tag ghcr.io/camptocamp/geoshop-api:$(VERSION) ghcr.io/camptocamp/geoshop-api:$(DOCKER_TAG)

.PHONY: push_ghcr
push_ghcr: ## Push docker image to GHCR
docker push ghcr.io/camptocamp/geoshop-api:$(VERSION)
docker push ghcr.io/camptocamp/geoshop-api:$(DOCKER_TAG)

.PHONY: test
test: ## Run tests
docker compose exec -T api python manage.py test -v 2 --force-color --noinput

.PHONY: prepare_env
prepare_env: destroy_env build ## Prepare Docker environment
docker compose up -d
until [ "$$(docker inspect -f '{{.State.Health.Status}}' geoshop-back-api-1)" = "healthy" ]; do \
echo "Waiting for api..."; \
sleep 1; \
done;

.PHONY: destroy_env
destroy_env: ## Destroy Docker environment
docker compose down --remove-orphans

.PHONY: help
help: ## Display this help
@echo "Usage: make <target>"
@echo
@echo "Available targets:"
@grep --extended-regexp --no-filename '^[a-zA-Z_-]+:.*## ' $(MAKEFILE_LIST) | sort | \
awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s%s\n", $$1, $$2}'
1 change: 1 addition & 0 deletions ci/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c2cciutils[checks,publish]==1.6.21
19 changes: 14 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ services:
environment:
PGHOST: "db"
command: >
bash -c "
python3 manage.py migrate &&
python3 manage.py collectstatic --noinput &&
python3 manage.py compilemessages --locale=fr &&
python3 manage.py fixturize"
bash -c "
python3 manage.py migrate &&
python3 manage.py collectstatic --noinput &&
python3 manage.py compilemessages --locale=fr &&
python3 manage.py fixturize"
volumes:
- "static-files:/app/geoshop_back/static:rw"
networks:
Expand All @@ -62,6 +62,15 @@ services:
PGHOST: "db"
command: gunicorn wsgi -b :8000 --timeout 90
restart: unless-stopped
healthcheck:
test:
[
"CMD-SHELL",
"curl -f http://127.0.0.1:8000/health/readiness || exit 1",
]
interval: 10s
timeout: 5s
retries: 5
volumes:
- "static-files:/app/geoshop_back/static:ro"
ports:
Expand Down
Loading