odoo-openupgrade-wizard/.gitlab-ci.yml
Rémy Taymans 72a5dcf434 New CI to auto build and publish
Using a commit message to trigger the build does not work when merging a
PR because last commit is the merge commit and not the commit edited
with the right name.

Given that, the jobs that will run, are defined at the creation of the
pipeline, publishing and creating a release cannot be done based on the
sate of the code.

A way to trigger publication and release is the git tags.

So with theses changes:

- linting is done only on a merge request
- testing and building are performed on a merge request and on the main
  branch

When a tag is pushed:

- check are done to ensure that the tag is the same as the version of
  the program, in order to not publish and release someting that is not
  coherent.
- the program is published on pypi.
- a release is created, but only if the tag is for a major, minor or
  patch version. No release created for an alpha, beta or pre-release
  version.

So all versions of the program are published on PyPI, but only the
important ones are published via the release mechanism. Because the
release mechanism will warn user for a new version. Version that are not
major, minor or patch are not intended to be used by end users.

The idea of auto publishing and releasing every time a commit is pushed
on the main branch does not work with semantic versioning. For doing
that maybe a calversioning will be better.

The idea of using the CI to push a tag for a new release lead to
security risk. Because the CI will contains credential for writing to
the repository, any contributor can read this token by editing the
gitlab-ci file and use token for bad purposes. Gitlab does not provide
token for writing to a repository owned by the project.

So for now, we control the publication and release of a new version with
two actions:

- updating the version on the pyproject.toml file.
- creating a tag with the same version as in the pyproject.toml file.
2024-02-21 11:50:27 +01:00

130 lines
3.3 KiB
YAML

stages:
- lint
- test
- build
- publish
- release
pre-commit:
image: python
stage: lint
rules:
# Run only if merge request
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
# Run if commit on default branch
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- pip install pre-commit
script:
- pre-commit run --all --show-diff-on-failure --verbose --color always
check_version:
stage: lint
image: python:latest
rules:
# Run if commit that start with a version number is pushed
- if: $CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+.*/
before_script:
- pip install poetry
- poetry --version
script:
# Ensure tag is the same as the program version
- test $(poetry version --short) = $CI_COMMIT_TAG
check_changelog:
stage: lint
image: python:latest
rules:
# Run if commit that start with a version number is pushed
- if: $CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/
before_script:
- pip install poetry
- poetry --version
script:
# Ensure change log is completed correctly
- cat CHANGES.rst | grep $CI_COMMIT_TAG
pytest:
image:
name: python:$PYTHON_VERSION
stage: test
tags:
- cie-oow-dind-runner
rules:
# Run if merge request
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
# Run if commit on default branch
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
services:
- name: docker:dind
alias: dind
variables:
ODOO_RPC_URL: dind
DOCKER_HOST: tcp://dind:2375/
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+)\%/'
before_script:
- pip install poetry
- export PATH="$HOME/.local/bin:$PATH"
- poetry install --all-extras
script:
- poetry run pytest -vv -x --cov=odoo_openupgrade_wizard
parallel:
matrix:
- PYTHON_VERSION:
- "3.7"
- "3.8"
- "3.9"
build:
stage: build
image: python:latest
rules:
# Run if merge request
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
# Run if commit on default branch
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# Run if commit that start with a version number is pushed
- if: $CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+.*/
before_script:
- pip install poetry
- poetry --version
script:
- poetry build
artifacts:
untracked: true
paths:
- dist/
publish:
stage: publish
image: python:latest
rules:
# Run if commit that start with a version number is pushed
- if: $CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+.*/
before_script:
- pip install poetry
- poetry --version
- ls -l dist
# Uncomment for testing build publication on test.pypi.org
#- poetry config repo.pypitest https://test.pypi.org/legacy/
script:
- poetry publish --skip-existing --username $PYPI_USER --password $PYPI_TOKEN
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
# Run only for a patch, minor or major release
# This avoid creating a release for alpha, beta, or other special
# releases
- if: $CI_COMMIT_TAG =~ /^[0-9]+\.[0-9]+\.[0-9]+$/
script:
- echo "running release_job for $CI_COMMIT_TAG"
release:
name: "$CI_COMMIT_TAG"
description: "Change log here: ${CI_PROJECT_URL}/-/blob/main/CHANGES.rst"
tag_name: "$CI_COMMIT_TAG"
ref: "$CI_COMMIT_SHA"