Skip to content
Snippets Groups Projects
Python.yaml 8.44 KiB
Newer Older
# -*- coding: utf-8 -*-
# vim: ft=yaml
#
# Hidden template jobs to be used in `.gitlab-ci.yml`
#
# - `.python:black:check`: verify formatting of code with `black`
#
# - `.python:build`: build the `sdist` and `wheel` packages
#
# - `.python:build:sdist`: build the `sdist` package
#
# - `.python:build:wheel`: build the `wheel` package
#
---
#
# .python:black:check
# ===================
#
# The Python code must match `black` standard.
#
# USAGE
# =====
#
# include:
#   - project: EOLE/Infra/ci-tools
#     ref: stable
#     file: /templates/Python.yaml
#
# stages:
#   - lint
#
# python:black: {extends: '.python:black:check'}
#
# REQUIREMENTS
# ============
#
# - a `lint` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.not-on-stable` rules templates
#
# OPTIONAL VARIABLES
# ==================
#
# - `PYTHON_BLACK_OPTS`: additional options to pass to `black`
#
# - `PYTHON_SOURCE_DIR`: top level directory where to run `black`,
#   defaults to `.`
#
# - `PYTHON_BLACK_IMAGE`: name of the black docker image to use
#
.python:black:check:
  stage: lint
  extends: .not-on-stable
  image: "${PYTHON_BLACK_IMAGE}"
  variables:
    PYTHON_BLACK_IMAGE: "hub.eole.education/proxyhub/pyfound/black:latest_release"
    PYTHON_BLACK_OPTS: ''
    PYTHON_SOURCE_DIR: '.'
  script:
    - echo -e "\e[0Ksection_start:`date +%s`:python-black\r\e[0KExecute 'black --check ${PYTHON_BLACK_OPTS} ${PYTHON_SOURCE_DIR}'"
    - black --check ${PYTHON_BLACK_OPTS} ${PYTHON_SOURCE_DIR}
    - echo -e "\e[0Ksection_end:`date +%s`:python-black\r\e[0K"
#
# .python:build
# =============
#
# Build the python `sdist` and `wheel` packages with the `build` module
#
# USAGE
# =====
#
# include:
#   - project: EOLE/Infra/ci-tools
#     ref: stable
#     file: /templates/Python.yaml
#
# stages:
#   - build
#
# python:build {extends: '.python:build'}
#
# REQUIREMENTS
# ============
#
# - a `build` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.rules-map` rules templates
#
# - a `pyproject.toml` or `setup.py` file
#
# OPTIONAL VARIABLES
# ==================
#
# - `PYTHON_DIST_FILES`: files to upload, defaults to `dist/*'
#
# - `PYTHON_SOURCE_DIR`: top level directory of python source where to
#   find `pyproject.toml` or `setup.py`, defaults to `.'
#
# - `PYTHON_BUILD_OPTS`: additional options to pass to the
#   `pyproject-build` command line
#
# - `PYTHON_IMAGE`: name of the python docker image to use
#
# SEE ALSO
# ========
#
# - https://pypi.org/project/build/
#
.python:build:
  stage: build
  extends: .python:base
  variables:
    PYTHON_DIST_FILES: dist/*
  artifacts:
    paths:
      - ${PYTHON_SOURCE_DIR}/${PYTHON_DIST_FILES}
  script:
    - echo -e "\e[0Ksection_start:`date +%s`:python-build-deps[collapsed=true]\r\e[0KInstall 'build' module"
    - pip install build
    - echo -e "\e[0Ksection_end:`date +%s`:python-build-deps\r\e[0K"
    - echo -e "\e[0Ksection_start:`date +%s`:python-build\r\e[0KBuild distribution `sdist` and `wheel` from '${PYTHON_SOURCE_DIR}'"
    - pyproject-build ${PYTHON_BUILD_OPTS} "${PYTHON_SOURCE_DIR}"
    - echo -e "\e[0Ksection_end:`date +%s`:python-build\r\e[0K"


#
# .python:build:sdist
# ===================
#
# Build the python source `.tar.gz` with the `build` module
#
# USAGE
# =====
#
# include:
#   - project: EOLE/Infra/ci-tools
#     ref: stable
#     file: /templates/Python.yaml
#
# stages:
#   - build
#
# python:build:source {extends: '.python:build:sdist'}
#
# REQUIREMENTS
# ============
#
# - a `build` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.rules-map` rules templates
# - a `pyproject.toml` or `setup.py` file
#
# OPTIONAL VARIABLES
# ==================
#
# - `PYTHON_DIST_FILES`: files to upload, defaults to `dist/*.tar.gz'
#
# - `PYTHON_SOURCE_DIR`: top level directory of python source where to
#   find `pyproject.toml` or `setup.py`, defaults to `.'
# - `PYTHON_BUILD_OPTS`: additional options to pass to the
#   `pyproject-build` command line
#
# - `PYTHON_IMAGE`: name of the python docker image to use
#
# SEE ALSO
# ========
#
# - https://pypi.org/project/build/
#
.python:build:sdist:
  variables:
    PYTHON_DIST_FILES: dist/*.tar.gz
  script:
    - echo -e "\e[0Ksection_start:`date +%s`:python-build-sdist-deps[collapsed=true]\r\e[0KInstall 'build' module"
    - pip install build
    - echo -e "\e[0Ksection_end:`date +%s`:python-build-sdist-deps\r\e[0K"
    - echo -e "\e[0Ksection_start:`date +%s`:python-build-sdist\r\e[0KBuild distribution 'sdist` from '${PYTHON_SOURCE_DIR}'"
    - pyproject-build --sdist ${PYTHON_BUILD_OPTS} "${PYTHON_SOURCE_DIR}"
    - echo -e "\e[0Ksection_end:`date +%s`:python-build-sdist\r\e[0K"


#
# .python:build:wheel
# ===================
#
# Build the python source `.tar.gz` with the `build` module
#
# USAGE
# =====
#
# include:
#   - project: EOLE/Infra/ci-tools
#     ref: stable
#     file: /templates/Python.yaml
#
# stages:
#   - build
#
# python:build:source {extends: '.python:build:wheel'}
#
# REQUIREMENTS
# ============
#
# - a `build` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.rules-map` rules templates
# - a `pyproject.toml` or `setup.py` file
#
# OPTIONAL VARIABLES
# ==================
#
# - `PYTHON_DIST_FILES`: files to upload, defaults to `dist/*.whl'
#
# - `PYTHON_SOURCE_DIR`: top level directory of python source where to
#   find `pyproject.toml` or `setup.py`, defaults to `.'
# - `PYTHON_BUILD_OPTS`: additional options to pass to the
#   `pyproject-build` command line
#
# - `PYTHON_IMAGE`: name of the python docker image to use
#
# SEE ALSO
# ========
#
# - https://pypi.org/project/build/
#
.python:build:wheel:
  variables:
    PYTHON_DIST_FILES: dist/*.whl
  script:
    - echo -e "\e[0Ksection_start:`date +%s`:python-build-wheel-deps[collapsed=true]\r\e[0KInstall 'build' module"
    - pip install build
    - echo -e "\e[0Ksection_end:`date +%s`:python-build-wheel-deps\r\e[0K"
    - echo -e "\e[0Ksection_start:`date +%s`:python-build-wheel\r\e[0KBuild distribution 'wheel` from '${PYTHON_SOURCE_DIR}'"
    - pyproject-build --wheel ${PYTHON_BUILD_OPTS} "${PYTHON_SOURCE_DIR}"
    - echo -e "\e[0Ksection_end:`date +%s`:python-build-wheel\r\e[0K"


#
# .python:upload
# ==============
#
# Upload python package files to the registry
#
# USAGE
# =====
#
# include:
#   - project: EOLE/Infra/ci-tools
#     ref: stable
#     file: /templates/Python.yaml
#
# stages:
#   - build
#
# python:upload {extends: '.python:upload'}
#
# REQUIREMENTS
# ============
#
# - a `build` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.on-release-tag` rules templates
#
# OPTIONAL VARIABLES
# ==================
#
# - `PYTHON_DIST_FILES`: files to upload, defaults to `dist/*'
#
# - `PYTHON_SOURCE_DIR`: top level directory of python source where to
#   find `setup.py`, defaults to `.`
#
# - `PYTHON_REPOSITORY_URL`: pypi repository URL, defaults to
#   `${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi`
#
# - `PYTHON_IMAGE`: name of the python docker image to use
#
.python:upload:
  stage: release
  variables:
    PYTHON_REPOSITORY_URL: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi
  script:
    - echo -e "\e[0Ksection_start:`date +%s`:python-twine-upload\r\e[0KUpload '${PYTHON_SOURCE_DIR}/${PYTHON_DIST_FILES}' to repository '${PYTHON_REPOSITORY_URL}'"
    - cd ${PYTHON_SOURCE_DIR}
    - pip install twine
    - 'TWINE_PASSWORD=${CI_JOB_TOKEN}
       TWINE_USERNAME=gitlab-ci-token
       python -m twine upload
       --repository-url ${PYTHON_REPOSITORY_URL}
       ${PYTHON_DIST_FILES}'
    - echo -e "\e[0Ksection_end:`date +%s`:python-twine-upload\r\e[0K"


#
# .python:base
# ============
#
# Base template for build and upload
# Run on all branches, release and prerelease tags
  rules:
    - !reference [.rules-map, not-on-schedule]
    - !reference [.rules-map, not-on-draft]
    - !reference [.rules-map, on-release-tag]
    - !reference [.rules-map, on-testing-tag]
    - !reference [.rules-map, on-dev-tag]
    - !reference [.rules-map, not-on-semantic-release-commit]
    - !reference [.rules-map, on-branch]
  image: "${PYTHON_IMAGE}"
  variables:
    PYTHON_IMAGE: "hub.eole.education/proxyhub/library/python:latest"
    PYTHON_SOURCE_DIR: '.'
    PYTHON_DIST_FILES: 'dist/*'