Skip to content

Commit

Permalink
Update internal CI workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Nov 21, 2023
1 parent ac0ed6d commit 4a8babc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 33 deletions.
64 changes: 31 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Run CI

on:
push:
branches: main
branches: "*"
tags: v*

permissions:
Expand All @@ -14,37 +14,30 @@ concurrency:

env:
PYPI_PACKAGE: hexdoc
PYTHON_VERSION: '3.11'
HEXDOC_RELEASE: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags') }}

jobs:
build:
runs-on: ubuntu-latest

permissions:
contents: write
pages: read

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

outputs:
release: ${{ env.HEXDOC_RELEASE }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: ${{ env.PYTHON_VERSION }}

- name: Install dependencies
run: pip install . build

- name: Get Pages url
run: |
url=$(gh api "repos/$GITHUB_REPOSITORY/pages" --jq '.html_url')
echo "GITHUB_PAGES_URL=$url" >> "$GITHUB_ENV"
run: pip install . hatch

- name: Export hexdoc resources
run: hexdoc export

- name: Build package
run: python -m build
run: hexdoc ci export

- name: Upload package artifact
uses: actions/upload-artifact@v3
Expand All @@ -60,7 +53,7 @@ jobs:
submodules: true
- uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: ${{ env.PYTHON_VERSION }}

- name: Run pre-commit hooks
env:
Expand All @@ -73,34 +66,39 @@ jobs:
- name: Run Nox
run: nox

publish-pypi:
update-tags:
runs-on: ubuntu-latest
needs: [build, test]
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
if: needs.build.outputs.release
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Nox
run: pip install nox

- name: Run Nox
run: nox --session tag

- name: Push new tags
run: git push --tags --force-with-lease

publish-pypi:
runs-on: ubuntu-latest
needs: [build, test]
if: needs.build.outputs.release
environment:
name: pypi
url: https://pypi.org/p/${{ env.PYPI_PACKAGE }}

permissions:
id-token: write
contents: write

steps:
- uses: actions/checkout@v4

# convert v1!0.1.0.dev0 to v0.1.0
- name: Parse version tag
id: parse
run: |
set +H
echo "version=v$(echo "$GITHUB_REF" | grep -Eo '![0-9]+\.[0-9]+\.[0-9]+' | sed 's/!//')" >> "$GITHUB_OUTPUT"
- name: Update release tags
uses: haya14busa/action-update-semver@v1
with:
tag: ${{ steps.parse.outputs.version }}

- name: Download package artifact
uses: actions/download-artifact@v3
with:
Expand Down
45 changes: 45 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import shutil
from pathlib import Path
from typing import Mapping
Expand Down Expand Up @@ -57,6 +59,34 @@ def pdoc(session: nox.Session):
)


@nox.session
def tag(session: nox.Session):
session.install("hatch", "packaging")

from packaging.version import Version

message = "Automatic PEP 440 release tag"

# validate some assumptions to make this simpler
version = Version(run_silent(session, "hatch", "version"))
assert version.epoch != 0
assert len(version.release) == 3

major, minor, _ = version.release
tag = f"v{version.epoch}!{major}"

# always update the prerelease tag, and also update the release tag if needed
update_git_tag(session, tag=tag + ".dev", message=message)
if version.dev is None:
update_git_tag(session, tag=tag, message=message)

tag += f".{minor}"

update_git_tag(session, tag=tag + ".dev", message=message)
if version.dev is None:
update_git_tag(session, tag=tag, message=message)


@nox.session
def hexdoc(session: nox.Session):
session.install(".")
Expand Down Expand Up @@ -129,3 +159,18 @@ def run_silent(
)
assert output
return output.strip()


def update_git_tag(session: nox.Session, *, tag: str, message: str):
return session.run(
"git",
"tag",
"-fam",
message,
tag,
external=True,
env=dict(
GIT_COMMITTER_NAME="github-actions",
GIT_COMMITTER_EMAIL="[email protected]",
),
)

0 comments on commit 4a8babc

Please sign in to comment.