Skip to content

Commit

Permalink
ci: DRY with composite actions
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed Mar 24, 2024
1 parent 9ac132a commit 2fc5e78
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 60 deletions.
32 changes: 32 additions & 0 deletions .github/actions/bootstrap-poetry/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Bootstrap Poetry
description: Configure the environment with the specified Python and Poetry version.

inputs:
python-version:
description: Desired Python version
default: "3.12"
python-latest:
description: Use an uncached Python if a newer match is available
default: 'false'
poetry-spec:
description: pip-compatible installation specification to use for Poetry
default: 'poetry'

runs:
using: composite
steps:
# Enable handling long path names (+260 char) on the Windows platform
# https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
- id: git-config-longpaths
if: matrix.os == 'Windows'
run: git config --system core.longpaths true

- id: setup-python
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
check-latest: ${{ inputs.python-latest == 'true' }}
cache: poetry

- id: pipx-poetry
run: pipx install --python '${{ steps.setup-python.outputs.python-path }}' '${{ inputs.poetry-spec }}'
49 changes: 49 additions & 0 deletions .github/actions/poetry-install/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Poetry Install
description: Install the project using Poetry, optionally changing defaults

inputs:
in-project:
description: Set virtualenvs.in-project
default: 'true'
no-pip:
description: Set virtualenvs.options.no-pip
default: 'true'
no-setuptools:
description: Set virtualenvs.options.no-setuptools
default: 'true'
no-root:
description: Use `poetry install --no-root`
default: 'false'
only-root:
description: Use `poetry install --only-root`
default: 'false'
extras:
description: Extras for `poetry install --extras`
groups-with:
description: Groups for `poetry install --with`
groups-without:
description: Groups for `poetry install --without`
groups-only:
description: Groups for `poetry install --only`
directory:
description: Directory for `poetry install --directory`

runs:
using: composite
steps:
- id: poetry-config
run: |
poetry config virtualenvs.in-project ${{ inputs.in-project }}
poetry config virtualenvs.options.no-pip ${{ inputs.no-pip }}
poetry config virtualenvs.options.no-setuptools ${{ inputs.no-setuptools }}
- id: poetry-install
run: |
poetry install \
${{ inputs.no-root == 'true' && '--no-root' }} \
${{ inputs.only-root == 'true' && '--only-root' }} \
${{ inputs.extras && format("--extras='{0}'", inputs.extras) }} \
${{ inputs.groups-with && format("--with='{0}'", inputs.groups-with) }} \
${{ inputs.groups-without && format("--without='{0}'", inputs.groups-without) }} \
${{ inputs.groups-only && format("--only='{0}'", inputs.groups-only) }} \
${{ inputs.directory && format("--directory='{0}'", inputs.directory) }}
42 changes: 19 additions & 23 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,39 @@ jobs:
with:
repository: python-poetry/website

- name: Bootstrap poetry
run: pipx install poetry

- name: Checkout Poetry Source
uses: actions/checkout@v4
with:
path: poetry
ref: ${{ github.event.pull_request.head.sha }}

- name: Set up Python
uses: actions/setup-python@v5
- uses: actions/setup-node@v4
with:
python-version: "3.12"
cache: poetry
node-version: "18"

- name: Setup Node
uses: actions/setup-node@v4
- uses: peaceiris/actions-hugo@v2
with:
node-version: "18"
hugo-version: '0.83.1'

- name: Build Assets
run: npm ci && npm run prod
- uses: ./.github/actions/bootstrap-poetry

- name: Fetch Documentation
run: |
poetry install --no-root --only main
poetry run python bin/website build --local ./poetry
- name: Install Hugo
uses: peaceiris/actions-hugo@v2
- uses: ./.github/actions/poetry-install
with:
hugo-version: '0.83.1'
no-root: true
groups-only: main

- name: Build
run: hugo -v --minify
- name: Build Website
run: |
set -eux -o pipefail
# Rebuild the docs files from the PR checkout.
poetry run python bin/website build --local ./poetry
# Build website assets (CSS/JS).
npm ci && npm run prod
# Build the static website.
hugo -v --minify
- name: Deploy
- name: Deploy Website
uses: amondnet/vercel-action@v25
id: vercel-action
with:
Expand Down
44 changes: 7 additions & 37 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,11 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Bootstrap poetry
run: pipx install poetry
- uses: ./.github/actions/bootstrap-poetry

- name: Set up Python
uses: actions/setup-python@v5
- uses: ./.github/actions/poetry-install
with:
python-version: "3.12"
cache: poetry

- name: Configure poetry
run: |
poetry config virtualenvs.in-project true
poetry config virtualenvs.options.no-pip true
poetry config virtualenvs.options.no-setuptools true
- name: Install dependencies
run: poetry install --only main,test
groups-only: main,test

- name: Regenerate PyPI fixtures
run: PYTHONPATH="$PWD" poetry run python tests/repositories/fixtures/pypi.org/generate.py
Expand Down Expand Up @@ -92,38 +80,20 @@ jobs:
- uses: actions/checkout@v4
if: needs.changes.outputs.pytest == 'true'

- name: Bootstrap poetry
if: needs.changes.outputs.pytest == 'true'
run: pipx install poetry

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
- uses: ./.github/actions/bootstrap-poetry
if: needs.changes.outputs.pytest == 'true'
with:
python-version: ${{ matrix.python-version }}
cache: poetry

- name: Enable long paths for git on Windows
if: needs.changes.outputs.pytest == 'true' && matrix.os == 'Windows'
# Enable handling long path names (+260 char) on the Windows platform
# https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
run: git config --system core.longpaths true

- name: Configure poetry
- uses: ./.github/actions/poetry-install
if: needs.changes.outputs.pytest == 'true'
run: |
poetry config virtualenvs.in-project true
poetry config virtualenvs.options.no-pip true
poetry config virtualenvs.options.no-setuptools true
with:
groups-with: github-actions

- name: Check lock file
if: needs.changes.outputs.pytest == 'true'
run: poetry check --lock

- name: Install dependencies
if: needs.changes.outputs.pytest == 'true'
run: poetry install --with github-actions

- name: Run mypy
if: needs.changes.outputs.pytest == 'true'
run: poetry run mypy
Expand Down

0 comments on commit 2fc5e78

Please sign in to comment.