Skip to content

Commit

Permalink
chore: add CI
Browse files Browse the repository at this point in the history
Signed-off-by: Michele Dolfi <[email protected]>
  • Loading branch information
dolfim-ibm committed Aug 6, 2024
1 parent ccb7675 commit d117678
Show file tree
Hide file tree
Showing 9 changed files with 1,294 additions and 2 deletions.
19 changes: 19 additions & 0 deletions .github/actions/setup-poetry/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'Set up Poetry and install'
description: 'Set up a specific version of Poetry and install dependencies using caching.'
inputs:
python-version:
description: "Version range or exact version of Python or PyPy to use, using SemVer's version range syntax."
default: '3.11'
runs:
using: 'composite'
steps:
- name: Install poetry
run: pipx install poetry==1.8.3
shell: bash
- uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}
cache: 'poetry'
- name: Install dependencies
run: poetry install --all-extras
shell: bash
39 changes: 39 additions & 0 deletions .github/scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

set -e # trigger failure on error - do not remove!
set -x # display command on output

if [ -z "${TARGET_VERSION}" ]; then
>&2 echo "No TARGET_VERSION specified"
exit 1
fi
CHGLOG_FILE="${CHGLOG_FILE:-CHANGELOG.md}"

# update package version
poetry version "${TARGET_VERSION}"

# collect release notes
REL_NOTES=$(mktemp)
poetry run semantic-release changelog --unreleased >> "${REL_NOTES}"

# update changelog
TMP_CHGLOG=$(mktemp)
TARGET_TAG_NAME="v${TARGET_VERSION}"
RELEASE_URL="$(gh repo view --json url -q ".url")/releases/tag/${TARGET_TAG_NAME}"
printf "## [${TARGET_TAG_NAME}](${RELEASE_URL}) - $(date -Idate)\n\n" >> "${TMP_CHGLOG}"
cat "${REL_NOTES}" >> "${TMP_CHGLOG}"
if [ -f "${CHGLOG_FILE}" ]; then
printf "\n" | cat - "${CHGLOG_FILE}" >> "${TMP_CHGLOG}"
fi
mv "${TMP_CHGLOG}" "${CHGLOG_FILE}"

# push changes
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add pyproject.toml "${CHGLOG_FILE}"
COMMIT_MSG="chore: bump version to ${TARGET_VERSION} [skip ci]"
git commit -m "${COMMIT_MSG}"
git push origin main

# create GitHub release (incl. Git tag)
gh release create "${TARGET_TAG_NAME}" -F "${REL_NOTES}"
50 changes: 50 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: "Run CD"

on:
push:
branches:
- main

env:
# disable keyring (https://github.com/actions/runner-images/issues/6185):
PYTHON_KEYRING_BACKEND: keyring.backends.null.Keyring

jobs:
code-checks:
uses: ./.github/workflows/checks.yml
pre-release-check:
runs-on: ubuntu-latest
outputs:
TARGET_TAG_V: ${{ steps.version_check.outputs.TRGT_VERSION }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # for fetching tags, required for semantic-release
- uses: ./.github/actions/setup-poetry
- name: Check version of potential release
id: version_check
run: |
TRGT_VERSION=$(poetry run semantic-release print-version)
echo "TRGT_VERSION=${TRGT_VERSION}" >> $GITHUB_OUTPUT
echo "${TRGT_VERSION}"
- name: Check notes of potential release
run: poetry run semantic-release changelog --unreleased
release:
needs: [code-checks, pre-release-check]
if: needs.pre-release-check.outputs.TARGET_TAG_V != ''
environment: auto-release
runs-on: ubuntu-latest
concurrency: release
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GH_PAT }}
fetch-depth: 0 # for fetching tags, required for semantic-release
- uses: ./.github/actions/setup-poetry
- name: Run release script
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
TARGET_VERSION: ${{ needs.pre-release-check.outputs.TARGET_TAG_V }}
CHGLOG_FILE: CHANGELOG.md
run: ./.github/scripts/release.sh
shell: bash
23 changes: 23 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
on:
workflow_call:

jobs:
run-checks:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-poetry
with:
python-version: ${{ matrix.python-version }}
- name: Run styling check
run: poetry run pre-commit run --all-files
- name: Run styling check
run: poetry run pre-commit run --all-files
- name: Build with poetry
run: poetry build
- name: Testing
run: |
poetry run pytest -v tests
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "Run CI"

on:
schedule: # will run only on the default branch (main)
# Nightly build at 3:42 A.M.
- cron: "42 3 */1 * *"
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
push:
branches:
- "**"
- "!main"
- "!gh-pages"

env:
# disable keyring (https://github.com/actions/runner-images/issues/6185):
PYTHON_KEYRING_BACKEND: keyring.backends.null.Keyring
BUILD_TYPE: Release

jobs:
code-checks:
uses: ./.github/workflows/checks.yml
build-wheels:
uses: ./.github/workflows/wheels.yml

16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: "Build and publish package"

on:
release:
types: [published]

permissions:
contents: read

env:
# disable keyring (https://github.com/actions/runner-images/issues/6185):
PYTHON_KEYRING_BACKEND: keyring.backends.null.Keyring

jobs:
build-and-publish:
uses: ./.github/workflows/wheels.yml
181 changes: 181 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
on:
workflow_call:

jobs:
build_wheels:
name: Build wheel for py${{ matrix.python-version }} ${{ matrix.os.platform_id }} ${{ matrix.os.macos_version }}
runs-on: ${{ matrix.os.name }}

strategy:
# Ensure that a wheel builder finishes even if another fails
# list of github vm: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

os:
- name: "ubuntu-latest"
platform: "linux"
platform_id: "manylinux_x86_64"

- name: "macos-13"
platform: "macos"
macos_version: "13"
platform_id: "macosx_x86_64"

- name: "macos-13"
platform: "macos"
macos_version: "13"
platform_id: "macosx_arm64"

- name: "macos-14"
platform: "macos"
macos_version: "14"
platform_id: "macosx_x86_64"

- name: "macos-14"
platform: "macos"
macos_version: "14"
platform_id: "macosx_arm64"

steps:
- name: Checkout
uses: actions/checkout@v4

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

- name: Install poetry
run: pipx install poetry==1.8.3 --python $(which python3)
shell: bash

- name: Test poetry
run: |
poetry run python --version
- name: Convert python version to cpXYZ
run: |
version=${{ matrix.python-version }}
cp_version="cp${version//.}"
echo "python_cp_version=$cp_version" >> $GITHUB_ENV
- name: Build wheels [macos-x86_64]
if: ${{matrix.os.platform_id == 'macosx_x86_64'}}
env:
CIBW_BUILD: ${{ env.python_cp_version }}-${{ matrix.os.platform_id }}
CIBW_ARCHS: x86_64
CIBW_ARCHS_MACOS: x86_64
CIBW_PLATFORM: macos
CIBW_SKIP: "pp* *-musllinux_* *_i686* *_s390* *arm64*"
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.8"
CIBW_BUILD_VERBOSITY: 3
CMAKE_OSX_ARCHITECTURES: x86_64
CIBW_REPAIR_WHEEL_COMMAND_MACOS: "" # do not run delocate-wheel before the re-tag
CIBW_ENVIRONMENT: "MACOSX_DEPLOYMENT_TARGET=${{ matrix.os.macos_version }}.0"
ARCHFLAGS: -arch x86_64
run: |
echo "Building wheel ${CIBW_BUILD}"
poetry run python --version
poetry install
cat ./pyproject.toml
poetry run python -m cibuildwheel --output-dir wheelhouse
echo "step 1"
ls -l wheelhouse
poetry run wheel tags --platform-tag macosx_${{ matrix.os.macos_version }}_0_x86_64 ./wheelhouse/*.whl
rm -f ./wheelhouse/*arm64.whl
echo "step 2"
ls -l wheelhouse
poetry run delocate-wheel --require-archs x86_64 -v ./wheelhouse/*.whl
echo "step 3"
ls -l wheelhouse
poetry run python -m zipfile --list wheelhouse/*.whl
mkdir ./dist
cp wheelhouse/*.whl ./dist/
# there is an error with the tagging of wheels for macosx-arm64
# see note: https://cibuildwheel.readthedocs.io/en/stable/faq/
# see here: https://gist.github.com/anderssonjohan/49f07e33fc5cb2420515a8ac76dc0c95#file-build-pendulum-wheels-yml-L39-L53
- name: Build wheels [macos-arm64]
if: ${{matrix.os.platform_id == 'macosx_arm64'}}
env:
CIBW_BUILD: ${{ env.python_cp_version }}-${{ matrix.os.platform_id }}
CIBW_ARCHS: arm64
CIBW_ARCHS_MACOS: arm64
CIBW_PLATFORM: macos
CIBW_SKIP: "pp* *-musllinux_* *_i686* *x86_64* *_s390*"
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.8"
CIBW_BUILD_VERBOSITY: 3
CMAKE_OSX_ARCHITECTURES: arm64
CIBW_REPAIR_WHEEL_COMMAND_MACOS: "" # do not run delocate-wheel before the re-tag
CIBW_ENVIRONMENT: "MACOSX_DEPLOYMENT_TARGET=${{ matrix.os.macos_version }}.0"
ARCHFLAGS: -arch arm64
run: |
echo "Building wheel ${CIBW_BUILD}"
poetry run python --version
poetry install
cat ./pyproject.toml
poetry run python -m cibuildwheel --output-dir wheelhouse
echo "step 1"
ls -l wheelhouse
poetry run wheel tags --platform-tag macosx_${{ matrix.os.macos_version }}_0_arm64 ./wheelhouse/*.whl
rm -f ./wheelhouse/*x86_64.whl
echo "step 2"
ls -l wheelhouse
poetry run delocate-wheel --require-archs arm64 -v ./wheelhouse/*.whl
echo "step 3"
ls -l wheelhouse
poetry run python -m zipfile --list wheelhouse/*.whl
mkdir ./dist
cp wheelhouse/*.whl ./dist/
- name: publish wheels (dry run) [macos]
if: matrix.os.platform == 'macos'
run: |
ls -l ./
ls -l ./dist
poetry publish --dry-run --no-interaction -vvv --username=__token__ --password=${{ secrets.PYPI_TOKEN }}
- name: publish wheels (on publishing) [macos]
if: ${{ matrix.os.platform == 'macos' && startsWith(github.ref, 'refs/tags/') }}
run: |
ls -l ./
ls -l ./dist
poetry publish --no-interaction -vvv --username=__token__ --password=${{ secrets.PYPI_TOKEN }}
- name: Build wheels [linux]
if: matrix.os.name == 'ubuntu-latest'
env:
CIBW_BUILD: ${{ env.python_cp_version }}-${{ matrix.os.platform_id }}
CIBW_ARCHS: x86_64
CIBW_PLATFORM: linux
CIBW_SKIP: "pp* *musllinux_* *_i686* *_s390*"
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.8"
CIBW_BUILD_VERBOSITY: 3
run: |
echo "Building wheel ${CIBW_BUILD}"
poetry run python --version
poetry add cibuildwheel
# poetry install
cat ./pyproject.toml
poetry run python -m cibuildwheel --output-dir ./wheelhouse
ls -l ./wheelhouse
poetry run python -m zipfile --list ./wheelhouse/*.whl
mkdir ./dist
cp wheelhouse/*.whl ./dist/
- name: publish wheels (dry run) [linux]
if: matrix.os.platform == 'linux'
run: |
ls -l ./
ls -l ./dist
poetry publish --dry-run --no-interaction -vvv --username=__token__ --password=${{ secrets.PYPI_TOKEN }}
- name: publish wheels (on publishing) [linux]
if: ${{ matrix.os.platform == 'linux' && startsWith(github.ref, 'refs/tags/') }}
run: |
ls -l ./
ls -l ./dist
poetry publish --no-interaction -vvv --username=__token__ --password=${{ secrets.PYPI_TOKEN }}
Loading

0 comments on commit d117678

Please sign in to comment.