Skip to content

Commit

Permalink
Use hatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Jul 11, 2024
1 parent b1d82b5 commit 0d2917f
Show file tree
Hide file tree
Showing 22 changed files with 3,290 additions and 278 deletions.
138 changes: 75 additions & 63 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
name: Backend CI

on:
push:
paths:
- "backend/**"
- ".github/workflows/backend.yml"
workflow_call:
workflow_dispatch:

env:
IMAGE_NAME_PREFIX: ghcr.io/collective/volto-form-block
IMAGE_NAME_SUFFIX: backend
PYTHON_VERSION: "3.11"

defaults:
run:
working-directory: ./backend

jobs:

meta:
config:
runs-on: ubuntu-latest
outputs:
BASE_TAG: ${{ steps.vars.outputs.BASE_TAG }}
PLONE_VERSION: ${{ steps.vars.outputs.PLONE_VERSION }}
steps:
- name: Checkout
Expand All @@ -25,91 +27,70 @@ jobs:
- name: Set Env Vars
id: vars
run: |
echo "PLONE_VERSION=$(cat backend/version.txt)" >> $GITHUB_OUTPUT
echo "PLONE_VERSION=$(cat version.txt)" >> $GITHUB_OUTPUT
echo 'BASE_TAG=sha-'$(git rev-parse --short HEAD) >> $GITHUB_OUTPUT
- name: Test vars
run: |
echo 'BASE_TAG=${{ steps.vars.outputs.BASE_TAG }}'
echo 'PLONE_VERSION=${{ steps.vars.outputs.PLONE_VERSION }}'
black:
code-quality:
runs-on: ubuntu-latest
needs:
- config
steps:
- name: Checkout codebase
uses: actions/checkout@v4

- name: Run check
uses: plone/code-analysis-action@v2
with:
base_dir: 'backend'
check: 'black'
- name: Install hatch
run: pipx install hatch

flake8:
runs-on: ubuntu-latest
steps:
- name: Checkout codebase
uses: actions/checkout@v4
- name: Generate Constraints file
run: pipx run mxdev -c mx.ini

- name: Run check
uses: plone/code-analysis-action@v2
- uses: actions/setup-python@v5
with:
base_dir: 'backend'
check: 'flake8'
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'

isort:
runs-on: ubuntu-latest
steps:
- name: Checkout codebase
uses: actions/checkout@v4
- name: "Install Environment"
run: hatch env create

- name: Run check
uses: plone/code-analysis-action@v2
with:
base_dir: 'backend'
check: 'isort'
- name: "Run checks"
run: hatch run lint

zpretty:
test:
runs-on: ubuntu-latest
needs:
- config
steps:
- name: Checkout codebase
uses: actions/checkout@v4

- name: Run check
uses: plone/code-analysis-action@v2
with:
base_dir: 'backend'
check: 'zpretty'
- name: Install hatch
run: pipx install hatch

tests:
runs-on: ubuntu-latest
needs:
- meta
defaults:
run:
working-directory: ./backend

steps:
- uses: actions/checkout@v4
- name: Generate Constraints file
run: pipx run mxdev -c mx.ini

- name: Setup Plone ${{ needs.meta.outputs.PLONE_VERSION }} with Python ${{ env.PYTHON_VERSION }}
uses: plone/[email protected]
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
plone-version: ${{ needs.meta.outputs.PLONE_VERSION }}
cache: 'pip'

- name: Install package
run: |
pip install mxdev
mxdev -c mx.ini
pip install -r requirements-mxdev.txt
- name: "Install Environment"
run: hatch env create

- name: Run tests
run: |
pytest --disable-warnings src/collective.voltoformblock/tests
- name: "Test Codebase"
run: hatch run test

release:
runs-on: ubuntu-latest
needs:
- black
- flake8
- isort
- zpretty
- tests
- config
- code-quality
- test
permissions:
contents: read
packages: write
Expand Down Expand Up @@ -156,3 +137,34 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
PLONE_VERSION=${{ needs.config.outputs.PLONE_VERSION }}
- name: Docker meta
id: meta-demo
uses: docker/metadata-action@v5
with:
images: |
${{ env.IMAGE_NAME_PREFIX }}-demo
labels: |
org.label-schema.docker.cmd=docker run -d -p 8080:8080 ${{ env.IMAGE_NAME_PREFIX }}-demo:latest
flavor:
latest=false
tags: |
type=ref,event=branch
type=sha
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push demo
uses: docker/build-push-action@v5
with:
platforms: linux/amd64
context: backend
file: backend/Dockerfile.demo
load: true
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-demo.outputs.tags }}
labels: ${{ steps.meta-demo.outputs.labels }}
build-args: |
SEED=${{ github.run_id }}
IMAGE_TAG=${{ needs.config.outputs.BASE_TAG }}
7 changes: 2 additions & 5 deletions .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
name: Frontend CI

on:
push:
paths:
- "frontend/**"
- ".github/workflows/frontend.yml"
workflow_call:
workflow_dispatch:

env:
Expand Down Expand Up @@ -88,7 +85,7 @@ jobs:
echo '| --- | --- |' >> $GITHUB_STEP_SUMMARY
echo '| Lint | ${{ steps.lint.conclusion == 'failure' && '❌' || ' ✅' }} |' >> $GITHUB_STEP_SUMMARY
echo '| i18n | ${{ steps.i18n.conclusion == 'failure' && '❌' || ' ✅' }} |' >> $GITHUB_STEP_SUMMARY
echo '| Unit Tests | ${{ steps.unit.conclusion == 'failure' && '❌' || ' ✅' }} |' >> $GITHUB_STEP_SUMMARY
echo '| Unit Tests | ${{ steps.unit.conclusion == 'failure' && '❌' || ' ✅' }} |' >> $GITHUB_STEP_SUMMARY
release:
runs-on: ubuntu-latest
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: 'volto-form-block'

on:
push:
paths:
- "backend/**"
- "frontend/**"
- ".github/workflows/backend.yml"
- ".github/workflows/frontend.yml"
- ".github/workflows/main.yml"
workflow_dispatch:

jobs:
config:
runs-on: ubuntu-latest
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'backend/**'
- '.github/workflows/backend.yml'
frontend:
- 'frontend/**'
- '.github/workflows/frontend.yml'
backend:
uses: ./.github/workflows/backend.yml
needs: config
if: ${{ needs.config.outputs.backend == 'true' }}
permissions:
contents: read
packages: write

frontend:
uses: ./.github/workflows/frontend.yml
needs: config
if: ${{ needs.config.outputs.frontend == 'true' }}
permissions:
contents: read
packages: write
64 changes: 34 additions & 30 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ GIT_FOLDER=$(BACKEND_FOLDER)/.git
VENV_FOLDER=$(BACKEND_FOLDER)/.venv
BIN_FOLDER=$(VENV_FOLDER)/bin

SEED=$(shell date +'%Y%m%d-%H%M%S')

all: build

Expand All @@ -51,24 +52,24 @@ all: build
help: ## This help message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

$(BIN_FOLDER)/pip $(BIN_FOLDER)/tox $(BIN_FOLDER)/pipx $(BIN_FOLDER)/uv $(BIN_FOLDER)/mxdev:
@echo "$(GREEN)==> Setup Virtual Env$(RESET)"
$(PYTHON) -m venv $(VENV_FOLDER)
$(BIN_FOLDER)/pip install -U "pip" "uv" "wheel" "pipx" "mxdev" "tox" "pre-commit"
if [ -d $(GIT_FOLDER) ]; then $(BIN_FOLDER)/pre-commit install; else echo "$(RED) Not installing pre-commit$(RESET)";fi
$(BIN_FOLDER)/hatch: ## Setup virtual env
@echo "$(GREEN)==> Install dependencies$(RESET)"
@pipx run hatch env create

instance/etc/zope.ini: $(BIN_FOLDER)/pip ## Create instance configuration
instance/etc/zope.ini: ## Create instance configuration
@echo "$(GREEN)==> Create instance configuration$(RESET)"
$(BIN_FOLDER)/pipx run cookiecutter -f --no-input --config-file instance.yaml gh:plone/cookiecutter-zope-instance
@pipx run cookiecutter -f --no-input --config-file instance.yaml gh:plone/cookiecutter-zope-instance

.PHONY: config
config: instance/etc/zope.ini

constraints-mxdev.txt: ## Generate constraints file
@echo "$(GREEN)==> Generate constraints file$(RESET)"
@echo '-c https://dist.plone.org/release/$(PLONE_VERSION)/constraints.txt' > requirements.txt
@pipx run mxdev -c mx.ini

.PHONY: build-dev
build-dev: config ## Install Plone packages
@echo "$(GREEN)==> Setup Build$(RESET)"
$(BIN_FOLDER)/mxdev -c mx.ini
$(BIN_FOLDER)/uv pip install -r requirements-mxdev.txt
build-dev: constraints-mxdev.txt config $(BIN_FOLDER)/hatch ## Install Plone packages

.PHONY: install
install: build-dev ## Install Plone
Expand All @@ -79,19 +80,19 @@ build: build-dev ## Install Plone
.PHONY: clean
clean: ## Clean environment
@echo "$(RED)==> Cleaning environment and build$(RESET)"
rm -rf $(VENV_FOLDER) pyvenv.cfg .installed.cfg instance .tox .venv .pytest_cache
rm -rf $(VENV_FOLDER) pyvenv.cfg dist instance .venv .pytest_cache requirements-mxdev.txt constraints-mxdev.txt requirements.lock

.PHONY: start
start: ## Start a Plone instance on localhost:8080
PYTHONWARNINGS=ignore $(BIN_FOLDER)/runwsgi instance/etc/zope.ini
ALLOWED_DISTRIBUTIONS=portalbrasil-intranet PYTHONWARNINGS=ignore $(BIN_FOLDER)/runwsgi instance/etc/zope.ini

.PHONY: console
console: instance/etc/zope.ini ## Start a console into a Plone instance
PYTHONWARNINGS=ignore $(BIN_FOLDER)/zconsole debug instance/etc/zope.conf
ALLOWED_DISTRIBUTIONS=portalbrasil-intranet PYTHONWARNINGS=ignore $(BIN_FOLDER)/zconsole debug instance/etc/zope.conf

.PHONY: create-site
create-site: instance/etc/zope.ini ## Create a new site from scratch
PYTHONWARNINGS=ignore $(BIN_FOLDER)/zconsole run instance/etc/zope.conf ./scripts/create_site.py
ALLOWED_DISTRIBUTIONS=portalbrasil-intranet PYTHONWARNINGS=ignore $(BIN_FOLDER)/zconsole run instance/etc/zope.conf ./scripts/create_site.py

# Example Content
.PHONY: update-example-content
Expand All @@ -100,39 +101,42 @@ update-example-content: $(BIN_FOLDER)/tox ## Export example content inside packa
if [ -d $(EXAMPLE_CONTENT_FOLDER)/content ]; then rm -r $(EXAMPLE_CONTENT_FOLDER)/* ;fi
$(BIN_FOLDER)/plone-exporter instance/etc/zope.conf $(PLONE_SITE_ID) $(EXAMPLE_CONTENT_FOLDER)

.PHONY: check
check: $(BIN_FOLDER)/tox ## Check and fix code base according to Plone standards
@echo "$(GREEN)==> Format codebase$(RESET)"
$(BIN_FOLDER)/tox -e lint
.PHONY: lint
lint: $(BIN_FOLDER)/hatch ## Check and fix code base according to Plone standards
@echo "$(GREEN)==> Lint codebase$(RESET)"
$(BIN_FOLDER)/hatch run lint

# i18n
$(BIN_FOLDER)/i18ndude: $(BIN_FOLDER)/pip
@echo "$(GREEN)==> Install translation tools$(RESET)"
$(BIN_FOLDER)/uv pip install i18ndude
.PHONY: format
format: $(BIN_FOLDER)/hatch ## Check and fix code base according to Plone standards
@echo "$(GREEN)==> Format codebase$(RESET)"
$(BIN_FOLDER)/hatch run format

.PHONY: i18n
i18n: $(BIN_FOLDER)/i18ndude ## Update locales
i18n: ## Update locales
@echo "$(GREEN)==> Updating locales$(RESET)"
$(BIN_FOLDER)/update_locale
$(BIN_FOLDER)/hatch run i18n

# Tests
.PHONY: test
test: $(BIN_FOLDER)/tox ## run tests
$(BIN_FOLDER)/tox -e test
test: $(BIN_FOLDER)/pytest ## run tests
$(BIN_FOLDER)/hatch run test

.PHONY: test-coverage
test-coverage: $(BIN_FOLDER)/tox ## run tests with coverage
$(BIN_FOLDER)/tox -e coverage
test-coverage: $(BIN_FOLDER)/pytest ## run tests with coverage
$(BIN_FOLDER)/hatch run cov

# Build Docker images
.PHONY: build-image
build-image: ## Build Docker Images
@DOCKER_BUILDKIT=1 docker build . -t $(IMAGE_NAME_PREFIX)-backend:$(IMAGE_TAG) -f Dockerfile --build-arg PLONE_VERSION=$(PLONE_VERSION)
@echo "$(GREEN)==> Building $(IMAGE_NAME_PREFIX)-demo:$(IMAGE_TAG) $(RESET)"
@DOCKER_BUILDKIT=1 docker build . --pull=false -t $(IMAGE_NAME_PREFIX)-demo:$(IMAGE_TAG) -f Dockerfile.demo --build-arg IMAGE_TAG=$(IMAGE_TAG) --build-arg SEED=$(SEED)


# Acceptance tests
.PHONY: acceptance-backend-start
acceptance-backend-start: ## Start backend acceptance server
ZSERVER_HOST=0.0.0.0 ZSERVER_PORT=55001 LISTEN_PORT=55001 APPLY_PROFILES="collective/voltoformblock:default" CONFIGURE_PACKAGES="plone.restapi,plone.volto,plone.volto.cors,collective/voltoformblock" $(BIN_FOLDER)/robot-server plone.app.robotframework.testing.VOLTO_ROBOT_TESTING
ZSERVER_HOST=0.0.0.0 ZSERVER_PORT=55001 LISTEN_PORT=55001 APPLY_PROFILES="plonegovbr/intranet:default" CONFIGURE_PACKAGES="plone.restapi,plone.volto,plone.volto.cors,plonegovbr/intranet" $(BIN_FOLDER)/robot-server plone.app.robotframework.testing.VOLTO_ROBOT_TESTING

.PHONY: acceptance-image-build
acceptance-image-build: ## Build Docker Images
Expand Down
Loading

0 comments on commit 0d2917f

Please sign in to comment.