Skip to content

wrapping-lint-actions

golangci-lint, ruff, shellcheck and actionlint, each run only when its kind of file changed.

Wrapping the linting actions: golangci/golangci-lint-action, astral-sh/ruff-action, ludeeus/action-shellcheck, rhysd/actionlint. Each is a tool and a command, so each wrapper is an image and the flags the action would have passed.

flowchart LR
    s0["golangci-lint"]
    s1["ruff"]
    s2["shellcheck"]
    s3["actionlint"]

The steps this script plans for a push about which nothing is known. An arrow is a needs.

# Wrapping the linting actions: golangci/golangci-lint-action, astral-sh/ruff-action,
# ludeeus/action-shellcheck, rhysd/actionlint. Each is a tool and a command, so
# each wrapper is an image and the flags the action would have passed.
#
# Like the actions, each lints only what it is about: a step's inputs are the
# files of its kind, so a commit that touches none of them skips it.

def golangci_lint(version = "v2.1", args = "", working_directory = "."):
    """golangci/golangci-lint-action."""
    return step(
        "golangci-lint",
        image = "golangci/golangci-lint:{}".format(version),
        run = "cd {} && golangci-lint run {}".format(working_directory, args),
        inputs = [working_directory + "/**/*.go", working_directory + "/go.mod", working_directory + "/go.sum", working_directory + "/.golangci.y*ml"],
        caches = {"golangci-lint": "/root/.cache/golangci-lint", "go-build": "/root/.cache/go-build"},
    )

def ruff(args = "check", src = "."):
    """astral-sh/ruff-action."""
    return step(
        "ruff",
        image = "ghcr.io/astral-sh/ruff:latest",
        run = "ruff {} {}".format(args, src),
        inputs = ["**/*.py", "pyproject.toml", "ruff.toml"],
    )

def shellcheck(severity = "warning", ignore_paths = []):
    """ludeeus/action-shellcheck."""
    skip = " ".join(["-not -path './{}/*'".format(p) for p in ignore_paths])
    return step(
        "shellcheck",
        image = "koalaman/shellcheck-alpine:stable",
        run = "find . -name '*.sh' {} -exec shellcheck --severity={} {{}} +".format(skip, severity),
        inputs = ["**/*.sh"],
    )

def actionlint():
    """rhysd/actionlint, for the workflows this repository still has."""
    return step(
        "actionlint",
        image = "rhysd/actionlint:latest",
        run = "actionlint",
        inputs = [".github/workflows"],
    )

# What each linter is about, for deciding from the changed files which to run.
ABOUT = {
    "golangci-lint": ["**/*.go", "go.mod", "go.sum", ".golangci.y*ml"],
    "ruff": ["**/*.py", "pyproject.toml", "ruff.toml"],
    "shellcheck": ["**/*.sh"],
    "actionlint": [".github/workflows/**"],
}

def ci(ctx):
    linters = {
        "golangci-lint": golangci_lint(args = "--timeout 5m"),
        "ruff": ruff(),
        "shellcheck": shellcheck(ignore_paths = ["vendor"]),
        "actionlint": actionlint(),
    }
    # With no list of changes to go on, run them all.
    if not ctx.changed_files:
        return list(linters.values())
    hit = paths_filter(ctx.changed_files, ABOUT)
    return [linters[name] for name in linters if hit[name]]
# Generated by `starci compile` from wrapping-lint-actions.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - The script decides by which files a commit changed. A workflow file
#    cannot, so it does what the script does when nothing is known about the
#    changes, on every run.
#  - Passes are remembered in this repository's Actions cache, which GitHub
#    scopes by branch and evicts when old. starci's own store has neither
#    limit.

name: CI

on:
  push:
    branches: ['**']
    tags: ['**']
  pull_request:
  workflow_dispatch:

jobs:
  golangci-lint:
    name: "golangci-lint"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /**/*.go
            /.golangci.y*ml
            /go.mod
            /go.sum
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-golangci-lint-e0cd4dcdf1513864-${{ hashFiles('**/*.go', '.golangci.y*ml', 'go.mod', 'go.mod/**', 'go.sum', 'go.sum/**') }}
      - name: "Cache: go-build"
        if: steps.passed.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-cache/go-build
          key: starci-cache-go-build-${{ github.run_id }}
          restore-keys: starci-cache-go-build-
      - name: "Cache: golangci-lint"
        if: steps.passed.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-cache/golangci-lint
          key: starci-cache-golangci-lint-${{ github.run_id }}
          restore-keys: starci-cache-golangci-lint-
      - name: "golangci-lint"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            cd . && golangci-lint run --timeout 5m
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            -v "$RUNNER_TEMP/starci-cache/go-build:/root/.cache/go-build" \
            -v "$RUNNER_TEMP/starci-cache/golangci-lint:/root/.cache/golangci-lint" \
            golangci/golangci-lint:v2.1 sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  ruff:
    name: "ruff"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /**/*.py
            /pyproject.toml
            /ruff.toml
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-ruff-8d0fef5441d3e890-${{ hashFiles('**/*.py', 'pyproject.toml', 'pyproject.toml/**', 'ruff.toml', 'ruff.toml/**') }}
      - name: "ruff"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            ruff check .
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            ghcr.io/astral-sh/ruff:latest sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  shellcheck:
    name: "shellcheck"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /**/*.sh
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-shellcheck-c2c1f7958bbf3a1a-${{ hashFiles('**/*.sh') }}
      - name: "shellcheck"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            find . -name '*.sh' -not -path './vendor/*' -exec shellcheck --severity=warning {} +
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            koalaman/shellcheck-alpine:stable sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  actionlint:
    name: "actionlint"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /.github/workflows
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-actionlint-fd7c0e54f7782df5-${{ hashFiles('.github/workflows', '.github/workflows/**') }}
      - name: "actionlint"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            actionlint
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            rhysd/actionlint:latest sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"
# Generated by `starci compile --target circleci` from wrapping-lint-actions.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - The script reads ctx.changed_files, so its plan depends on the commit.
#    This file is the plan for a commit about which nothing is known, and is
#    the same for every push: steps the script would add or drop for a
#    particular branch or set of changed files are not represented.
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  golangci-lint:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/**/*.go' '/.golangci.y*ml' '/go.mod' '/go.sum'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)**/*.go' ':(top,glob).golangci.y*ml' ':(top,literal)go.mod' ':(top,literal)go.sum' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-golangci-lint-e0cd4dcdf1513864-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - restore_cache:
          name: "Cache: go-build"
          keys:
            - starci-cache-go-build-
      - restore_cache:
          name: "Cache: golangci-lint"
          keys:
            - starci-cache-golangci-lint-
      - run:
          name: "golangci-lint"
          environment:
            STARCI_RUN: |-
              cd . && golangci-lint run --timeout 5m
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/go-build:/root/.cache/go-build" \
              -v "$HOME/starci-cache/golangci-lint:/root/.cache/golangci-lint" \
              golangci/golangci-lint:v2.1 sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: go-build"
          key: starci-cache-go-build-{{ .Revision }}
          paths:
            - ~/starci-cache/go-build
      - save_cache:
          name: "Save cache: golangci-lint"
          key: starci-cache-golangci-lint-{{ .Revision }}
          paths:
            - ~/starci-cache/golangci-lint
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-golangci-lint-e0cd4dcdf1513864-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  ruff:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/**/*.py' '/pyproject.toml' '/ruff.toml'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)**/*.py' ':(top,literal)pyproject.toml' ':(top,literal)ruff.toml' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-ruff-8d0fef5441d3e890-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "ruff"
          environment:
            STARCI_RUN: |-
              ruff check .
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              ghcr.io/astral-sh/ruff:latest sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-ruff-8d0fef5441d3e890-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  shellcheck:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/**/*.sh'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)**/*.sh' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-shellcheck-c2c1f7958bbf3a1a-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "shellcheck"
          environment:
            STARCI_RUN: |-
              find . -name '*.sh' -not -path './vendor/*' -exec shellcheck --severity=warning {} +
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              koalaman/shellcheck-alpine:stable sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-shellcheck-c2c1f7958bbf3a1a-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  actionlint:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/.github/workflows'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal).github/workflows' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-actionlint-fd7c0e54f7782df5-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "actionlint"
          environment:
            STARCI_RUN: |-
              actionlint
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              rhysd/actionlint:latest sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-actionlint-fd7c0e54f7782df5-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - golangci-lint
      - ruff
      - shellcheck
      - actionlint

Download: wrapping-lint-actions.ci.star, GitHub Actions, CircleCI