From 2fc5e7887097252fc5ee633ac2459e6c20e34567 Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Sun, 24 Mar 2024 17:26:55 -0600 Subject: [PATCH] ci: DRY with composite actions --- .github/actions/bootstrap-poetry/action.yml | 32 ++++++++++++++ .github/actions/poetry-install/action.yml | 49 +++++++++++++++++++++ .github/workflows/docs.yaml | 42 ++++++++---------- .github/workflows/tests.yaml | 44 +++--------------- 4 files changed, 107 insertions(+), 60 deletions(-) create mode 100644 .github/actions/bootstrap-poetry/action.yml create mode 100644 .github/actions/poetry-install/action.yml diff --git a/.github/actions/bootstrap-poetry/action.yml b/.github/actions/bootstrap-poetry/action.yml new file mode 100644 index 00000000000..9971cac6816 --- /dev/null +++ b/.github/actions/bootstrap-poetry/action.yml @@ -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 }}' diff --git a/.github/actions/poetry-install/action.yml b/.github/actions/poetry-install/action.yml new file mode 100644 index 00000000000..9fceeed7534 --- /dev/null +++ b/.github/actions/poetry-install/action.yml @@ -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) }} diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index 5ab22316e6f..4142a375e91 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -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: diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 07b1d0f58ab..ef6c29fbae0 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -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 @@ -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