Coder Social home page Coder Social logo

kdeldycke / workflows Goto Github PK

View Code? Open in Web Editor NEW
18.0 4.0 5.0 2.94 MB

⚙️ CLI helpers for GitHub Action + reuseable workflows

License: GNU General Public License v2.0

Python 100.00%
ci-cd github-actions yaml labels linting release-automation python changelog-formatter typo sponsorship cli sphinx-doc workflow-reusable build-automation packaging pypi formatting markdown nuitka mypy

workflows's Introduction

gha-utils CLI + reusable workflows

Last release Python versions Type checked with mypy Unittests status

Thanks to this project, I am able to release Python packages multiple times a day with only 2-clicks.

This repository contains a collection of reusable workflows and its companion CLI called gha-utils (which stands for GitHub action workflows utilities).

It is designed for uv-based Python projects (and Awesome List projects as a bonus).

It takes care of:

  • Version bumping
  • Formatting autofix for: Python, Markdown, JSON, typos
  • Linting: Python types with mypy, YAML, zsh, GitHub actions, links, Awesome lists, secrets
  • Compiling of Python binaries for Linux / macOS / Windows on x86_64 & arm64
  • Building of Python packages and upload to PyPi
  • Git version tagging and GitHub release creation
  • Synchronization of: uv.lock, .gitignore, .mailmap and Mermaid dependency graph
  • Auto-locking of inactive closed issues
  • Static image optimization
  • Sphinx documentation building & deployment, and autodoc updates
  • Label management, with file-based and content-based rules

Nothing is done behind your back. A PR is created everytime a change is proposed, so you can inspect it, ala dependabot.

gha-utils CLI

Executables

Standalone executables of gha-utils's latest version are available as direct downloads for several platforms and architectures:

Platform x86_64 arm64
Linux Download gha-utils-linux-x64.bin
macOS Download gha-utils-macos-x64.bin Download gha-utils-macos-arm64.bin
Windows Download gha-utils-windows-x64.exe

Run dev version

$ git clone https://github.com/kdeldycke/workflows
$ cd workflows
$ python -m pip install uv
$ uv venv
$ source .venv/bin/activate
$ uv pip install .
$ uv run -- gha-utils

Reusable workflows collection

This repository contains workflows to automate most of the boring tasks.

These workflows are mostly used for Python projects and their documentation, but not only. They're all reusable GitHub actions workflows.

Reasons for a centralized workflow repository:

  • reusability of course: no need to update dozens of repository where 95% of workflows are the same
  • centralize all dependencies pertaining to automation: think of the point-release of an action that triggers dependabot upgrade to all your repositories depending on it

Guidelines

I don't want to copy-n-past, keep in sync and maintain another Nth CI/CD file at the root of my repositories.

So my policy is: move every repository-specific config in a pyproject.toml file, or hide the gory details in a reused workflow.

.github/workflows/docs.yaml jobs

  • Autofix typos

  • Optimize images

  • Keep .mailmap up to date

  • Update dependency graph of Python projects

    • Requires:
      • Python package with a pyproject.toml file
  • Build Sphinx-based documentation and publish it to GitHub Pages

    • Requires:
      • Python package with a pyproject.toml file
      • All Sphinx dependencies in a docs extra dependency group:
        [project.optional-dependencies]
        docs = [
            "furo == 2024.1.29",
            "myst-parser ~= 3.0.0",
            "sphinx >= 6",
            ...
        ]
      • Sphinx configuration file at docs/conf.py
  • Sync awesome projects from awesome-template repository

Why all these requirements/*.txt files?

Let's look for example at the lint-yaml job from .github/workflows/lint.yaml. Here we only need the yamllint CLI. This CLI is distributed on PyPi. So before executing it, we could have simply run the following step:

  - name: Install yamllint
    run: |
      pip install yamllint

Instead, we install it via the requirements/yamllint.txt file.

Why? Because I want the version of yamllint to be pinned. By pinning it, I make the workflow stable, predictable and reproducible.

So why use a dedicated requirements file? Why don't we simply add the version? Like this:

  - name: Install yamllint
    run: |
      pip install yamllint==1.35.1

That would indeed pin the version. But it requires the maintainer (me) to keep track of new release and update manually the version string. That's a lot of work. And I'm lazy. So this should be automated.

To automate that, the only practical way I found was to rely on dependabot. But dependabot cannot update arbitrary versions in run: YAML blocks. It only supports requirements.txt and pyproject.toml files for Python projects.

So to keep track of new versions of dependencies while keeping them stable, we've hard-coded all Python libraries and CLIs in the requirements/*.txt files. All with pinned versions.

And for the case we need to install all dependencies in one go, we have a requirements.txt file at the root that is referencing all files from the requirements/ subfolder.

Permissions and token

This repository updates itself via GitHub actions. It particularly updates its own YAML files in .github/workflows. That's forbidden by default. So we need extra permissions.

Usually, to grant special permissions to some jobs, you use the permissions parameter in workflow files. It looks like this:

on: (...)

jobs:

  my-job:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps: (...)

But the contents: write permission doesn't allow write access to the workflow files in the .github subfolder. There is actions: write, but it only covers workflow runs, not their YAML source file. Even a permissions: write-all doesn't work. So you cannot use the permissions parameter to allow a repository's workflow update its own workflow files.

You will always end up with this kind or errors:

   ! [remote rejected] branch_xxx -> branch_xxx (refusing to allow a GitHub App to create or update workflow `.github/workflows/my_workflow.yaml` without `workflows` permission)

  error: failed to push some refs to 'https://github.com/kdeldycke/my-repo'

Note

That's also why the Settings > Actions > General > Workflow permissions parameter on your repository has no effect on this issue, even with the Read and write permissions set:

To bypass the limitation, we rely on a custom access token. By convention, we call it WORKFLOW_UPDATE_GITHUB_PAT. It will be used, in place of the default secrets.GITHUB_TOKEN, in steps in which we need to change the workflow YAML files.

To create this custom WORKFLOW_UPDATE_GITHUB_PAT:

  • From your GitHub user, go to Settings > Developer Settings > Personal Access Tokens > Fine-grained tokens
  • Click on the Generate new token button
  • Choose a good token name like workflow-self-update to make your intention clear
  • Choose Only select repositories and the list the repositories in needs of updating their workflow YAML files
  • In the Repository permissions drop-down, sets:
    • Contents: Access: **Read and Write**
    • Metadata (mandatory): Access: **Read-only**
    • Pull Requests: Access: **Read and Write**
    • Workflows: Access: **Read and Write**

      [!NOTE] This is the only place where I can have control over the Workflows permission, which is not supported by the permissions: parameter in YAML files.

  • Now save these parameters and copy the github_pat_XXXX secret token
  • Got to your repo > Settings > Security > Secrets and variables > Actions > Secrets > Repository secrets and click New repository secrets
  • Name your secret WORKFLOW_UPDATE_GITHUB_PAT and copy the github_pat_XXXX token in the Secret field

Now re-run your actions and they should be able to update the workflow files in .github folder without the refusing to allow a GitHub App to create or update workflow error.

Release management

It turns out Release Engineering is a full-time job, and full of edge-cases.

Rust has cargo-dist. Go has... ? But there is no equivalent for Python.

So I made up a release.yaml workflow, which:

  1. Extracts project metadata from pyproject.toml
  2. Generates a build matrix of all commits / os / arch / CLI entry points
  3. Build Python wheel with Twine
  4. Compile binaries of all CLI with Nuitka
  5. Tag the release commit in Git
  6. Publish new version to PyPi
  7. Publish a GitHub release
  8. Attach and rename build artifacts to it

Changelog

A detailed changelog is available.

Used in

Check these projects to get real-life examples of usage and inspiration:

Feel free to send a PR to add your project in this list if you are relying on these scripts.

Release process

All steps of the release process and version management are automated in the changelog.yaml and release.yaml workflows.

All there's left to do is to:

  • check the open draft prepare-release PR and its changes,
  • click the Ready for review button,
  • click the Rebase and merge button,
  • let the workflows tag the release and set back the main branch into a development state.

workflows's People

Contributors

github-actions[bot] avatar kdeldycke avatar rasa avatar web-flow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

workflows's Issues

Deduplicate `is-poetry` detection step

An is-poetry job has been copied-n-pasted in 3 places:

  • autofix.yaml
  • docs.yaml
  • release.yaml

That is an ugly workaround to the fact that you cannot have steps of a workflow depends on the steps of another (let alone reuseable ones).

Broken links

Summary

Status Count
🔍 Total 240
✅ Successful 237
⏳ Timeouts 0
🔀 Redirected 0
👻 Excluded 2
❓ Unknown 0
🚫 Errors 1

Errors per input

Errors in changelog.md

Broken links

Summary

Status Count
🔍 Total 231
✅ Successful 230
⏳ Timeouts 0
🔀 Redirected 0
👻 Excluded 0
❓ Unknown 0
🚫 Errors 1

Errors per input

Errors in changelog.md

Broken links

Summary

Status Count
🔍 Total 305
✅ Successful 299
⏳ Timeouts 0
🔀 Redirected 0
👻 Excluded 2
❓ Unknown 0
🚫 Errors 4

Errors per input

Errors in readme.md

PyPi trusted publishing: not working with reuseable workflows

Attempt has been made in v2.14.0 (see 3571c06 and 1583a6f) but it doesn't work with reuseable workflow.

The publishing ends up with the following error:

2023-05-04T10:23:05.4290038Z ##[group]Run pypa/[email protected]
2023-05-04T10:23:05.4290334Z with:
2023-05-04T10:23:05.4290619Z   packages-dir: /home/runner/work/mail-deduplicate/mail-deduplicate
2023-05-04T10:23:05.4290908Z   user: __token__
2023-05-04T10:23:05.4291165Z   repository_url: https://upload.pypi.org/legacy/
2023-05-04T10:23:05.4291431Z   packages_dir: dist
2023-05-04T10:23:05.4291653Z   verify_metadata: true
2023-05-04T10:23:05.4291859Z   skip_existing: false
2023-05-04T10:23:05.4292069Z   verbose: false
2023-05-04T10:23:05.4292274Z   print_hash: false
2023-05-04T10:23:05.4292462Z ##[endgroup]
2023-05-04T10:23:05.4549470Z ##[command]/usr/bin/docker run --name ed866e85ca8b0c42a84a0aa2284859693d6a22_8005b6 --label ed866e --workdir /github/workspace --rm -e "INPUT_PACKAGES-DIR" -e "INPUT_USER" -e "INPUT_PASSWORD" -e "INPUT_REPOSITORY-URL" -e "INPUT_REPOSITORY_URL" -e "INPUT_PACKAGES_DIR" -e "INPUT_VERIFY-METADATA" -e "INPUT_VERIFY_METADATA" -e "INPUT_SKIP-EXISTING" -e "INPUT_SKIP_EXISTING" -e "INPUT_VERBOSE" -e "INPUT_PRINT-HASH" -e "INPUT_PRINT_HASH" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e "ACTIONS_ID_TOKEN_REQUEST_URL" -e "ACTIONS_ID_TOKEN_REQUEST_TOKEN" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/mail-deduplicate/mail-deduplicate":"/github/workspace" ed866e:85ca8b0c42a84a0aa2284859693d6a22  "__token__" "" "" "/home/runner/work/mail-deduplicate/mail-deduplicate" "" "" "false" ""
2023-05-04T10:23:05.9204477Z ##[notice]Attempting to perform trusted publishing exchange to retrieve a temporary short-lived API token for authentication against https://upload.pypi.org/legacy/ due to __token__ username with no supplied password field
2023-05-04T10:23:06.7818733Z ##[error]Trusted publishing exchange failure: 
2023-05-04T10:23:06.7820182Z Token request failed: the server refused the request for the following reasons:
2023-05-04T10:23:06.7909041Z 
2023-05-04T10:23:06.7909646Z * `invalid-publisher`: valid token, but no corresponding publisher
2023-05-04T10:23:06.7909902Z 
2023-05-04T10:23:06.9498873Z Evaluate and set environment url
2023-05-04T10:23:06.9503492Z Evaluated environment url: https://pypi.org/p/mail-deduplicate

For other info, see:

Add a `dev` suffix to version

While a poetry project sits at the top of the main branch and hasn't been released yet, it would be great to have its version with a -dev or .dev suffix.

That way, when users are playing with the bleeding edge version of a project, there is a hint in the version number.

Better: append the git revision if we're in the context of a clone.

Let `ruff` picks its configuration from `pyproject.toml`

Currently, all ruff parameters are hard-coded in the workflow steps:

I would like to have ruff's configuration written in the [pyproject.toml](https://github.com/kdeldycke/workflows/blob/main/pyproject.toml).

The problem is in reconciliating the reuseable workflow config and the config from the repository the reuseable workflow is used in. Ideally, both config should be able to merged. The difficulty is in hard-coding the target version of the reuseable workflow config, and fetch it remotely.

Broken links

Summary

Status Count
🔍 Total 283
✅ Successful 274
⏳ Timeouts 0
🔀 Redirected 0
👻 Excluded 2
❓ Unknown 0
🚫 Errors 7

Errors per input

Errors in changelog.md

Document workflows

All reuseable workflows should be, in readme.md:

  • listed
  • explained
  • have an example

Broken links

Summary

Status Count
🔍 Total 320
✅ Successful 317
⏳ Timeouts 0
🔀 Redirected 0
👻 Excluded 2
❓ Unknown 0
🚫 Errors 1

Errors per input

Errors in changelog.md

Replace unmaintained `bump2version`

As per this comment, Poetry v1.2 supports plugins, and some of them can make a perfect (and more robust, and maintained) replacement for bump2version (whose usage in the release and changelog workflows is quite kludgy).

Broken links

Summary

Status Count
🔍 Total 212
✅ Successful 211
⏳ Timeouts 0
🔀 Redirected 0
👻 Excluded 0
❓ Unknown 0
🚫 Errors 1

Errors per input

Errors in changelog.md

Use Town Crier to manage changelog

It seems there is a way to produce Markdown changelogs with towncrier. See: twisted/towncrier#128

This might be helpful to manage version fragments, and have them in sync between:

  • Sphinx docs
  • GitHub releases
  • Copies in Readme showing delta-changes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.