Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dependabot version updates; patch and minor updates are auto-merged by dependabot-auto-merge.yml
version: 2
updates:
# Actions in this repo's workflows and in the workflows rendered into new projects
- package-ecosystem: github-actions
directories:
- /
- /template
schedule:
interval: weekly
# Wait a week after a release, so broken or malicious versions are more likely to be caught first
cooldown:
default-days: 7
groups:
github-actions:
patterns:
- "*"
9 changes: 9 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Summary

<!-- What does this change and why? -->

## Checklist

- [ ] `just lint` passes
- [ ] `just test-template` passes
- [ ] Docs updated if needed (`README.md`, `template/README.md.jinja`, `template/CLAUDE.md`)
30 changes: 30 additions & 0 deletions .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Dependabot auto-merge

# pull_request (not pull_request_target) is enough: Dependabot PRs come from a branch in this
# repository, so the token gets the permissions granted below
on: pull_request

permissions: {}

jobs:
auto-merge:
runs-on: ubuntu-latest
# Check the PR author rather than github.actor, which can be someone re-running the workflow
if: github.event.pull_request.user.login == 'dependabot[bot]'
permissions:
contents: write
pull-requests: write

steps:
- name: Fetch Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98 # v3.1.0

# Patch and minor updates merge once the required checks pass; major updates wait for review.
# Auto-merge must be allowed in the repository settings, with required status checks on main.
- name: Enable auto-merge for patch and minor updates
if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39 changes: 36 additions & 3 deletions .github/workflows/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,53 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v7
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false

# Installs the toolchain and components pinned in the template's rust-toolchain.toml
- name: Install Rust
working-directory: template
run: rustup toolchain install

- name: Install just
uses: taiki-e/install-action@v2
uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2
with:
tool: just
tool: just,zizmor,uv,cargo-deny,cargo-nextest,cargo-shear,cargo-llvm-cov,typos,tombi

# actionlint isn't available from install-action; install it from PyPI with uv
- name: Install actionlint
run: |
uv tool install actionlint-py==1.7.12.25
uv tool dir --bin >> "$GITHUB_PATH"

- name: Lint workflows
run: just lint

- name: Install Copier
run: pipx install copier

- name: Generate and test project
run: just test-template

gitleaks:
runs-on: ubuntu-latest
# Job-level permissions replace the workflow-level ones, so contents: read is repeated here.
# pull-requests: read lets the action list a PR's commits, which private repos require.
permissions:
contents: read
pull-requests: read

steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
# Full history so every commit is scanned for secrets
fetch-depth: 0
persist-credentials: false

- name: Scan for secrets
uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e # v3.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# PR comments would need the pull-requests: write permission
GITLEAKS_ENABLE_COMMENTS: false
17 changes: 17 additions & 0 deletions Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@
# Project template tool (see copier.yml)
brew "copier"
brew "just"

# Rust toolchain manager; `just setup` uses it to install cargo-binstall and the Rust tools
# (see `cargo_tools` in the justfile), and `just test-template` builds the generated project
brew "rustup"

# TOML formatter (run by `just lint`)
brew "tombi"

# GitHub Actions linters
brew "actionlint"
brew "zizmor"

# Git hook manager (see prek.toml); run `prek install` once to enable hooks
brew "prek"

# Secret scanner (prek hook and `just gitleaks`)
brew "gitleaks"
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,37 @@ Rust project template for [Copier](https://copier.readthedocs.io/).
- Create a new project - `copier copy gh:<owner>/rust-template path/to/new-project`
- Pull in template updates from inside a generated project - `copier update`

## Development

- Install `just` - `brew install just`
- Install Copier, rustup, tombi, actionlint, zizmor, prek and gitleaks with Homebrew, then cargo-binstall and the pinned Rust tools (cargo-deny, cargo-nextest, cargo-shear, cargo-llvm-cov, typos) with cargo - `just setup`
- Change tool versions in `cargo_tools` at the top of the `justfile`; reinstall with `just install-tools`
- Bump them all to the latest releases and install them - `just update`
- Enable the Git hooks in `prek.toml` - `prek install`
- Before each commit they run `just lint` and scan the staged changes for secrets with gitleaks

Commands are defined in the `justfile` and run with [just](https://github.com/casey/just).

- List available commands - `just`
- Format TOML files with tombi - `just fmt`
- Check spelling with typos, TOML formatting with tombi, and GitHub Actions workflows with actionlint and zizmor - `just lint`
- Render the template into a temp directory, then lint, test, check coverage and check dependencies of the generated project - `just test-template`
- Scan the Git history for committed secrets - `just gitleaks`

CI runs `just lint` and `just test-template`, and scans the Git history for secrets with gitleaks.

## Dependency Updates

- Dependabot (`.github/dependabot.yml`) opens weekly PRs for GitHub Actions in this repo's workflows and in `template/`
- Updates wait 7 days after a release before a PR is opened
- Patch and minor updates are auto-merged once CI passes (`.github/workflows/dependabot-auto-merge.yml`); major updates wait for review
- Auto-merge needs "Allow auto-merge" turned on in the repository settings and a branch protection rule or ruleset on `main` that requires the CI checks
- On GitHub Free, branch protection and auto-merge are only available for public repos
- Dependabot doesn't update the actionlint version in the workflows and dev container, the gitleaks version and image in the dev container, or anything in `.jinja` files

## Layout

- `copier.yml` - template questions and settings
- `template/` - files rendered into new projects; files ending in `.jinja` are templated
- `prek.toml` - Git hooks for this repo
- `.github/pull_request_template.md` - checklist filled into new PR descriptions
14 changes: 9 additions & 5 deletions copier.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
_min_copier_version: "9.0.0"
_min_copier_version: "9.6.0"
_subdirectory: template

# Starter code is only created by `copier copy`. `copier update` skips these files, so a project
# can change, move or delete them without an update recreating or overwriting them.
# (`_copier_operation` needs Copier 9.6.0 or later.)
_exclude:
- "{% if _copier_operation == 'update' %}src/lib.rs{% endif %}"
- "{% if _copier_operation == 'update' %}src/main.rs{% endif %}"
- "{% if _copier_operation == 'update' %}tests/greeting.rs{% endif %}"

project_name:
type: str
help: Cargo package name (lowercase letters, digits, hyphens and underscores)
Expand All @@ -19,10 +27,6 @@ description:
help: Short description of the project
default: ""

author_name:
type: str
help: Name used in the LICENSE copyright line

copyright_year:
type: str
help: Year used in the LICENSE copyright line
63 changes: 62 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,62 @@
default:
@just --list

# Render the template into a temp directory, then lint and test the generated project
# Homebrew's rustup is keg-only, so neither rustup nor its cargo/rustc proxies
# are on PATH. Prepend its bin directory for all recipes when Homebrew is available,
# plus Cargo's bin directory, where `just install-tools` puts the Rust tools.
rustup_prefix := `brew --prefix rustup 2>/dev/null || true`
cargo_bin := env("CARGO_HOME", home_directory() / ".cargo") / "bin"
export PATH := cargo_bin + ":" + if rustup_prefix != "" { rustup_prefix / "bin" + ":" + env("PATH") } else { env("PATH") }

# Rust tools installed by `just install-tools` as prebuilt binaries with cargo-binstall; bump versions here
binstall_version := "1.26.2"
cargo_tools := "cargo-deny@0.20.2 cargo-nextest@0.9.151 cargo-shear@1.14.0 cargo-llvm-cov@0.9.1 typos-cli@1.52.0"

# Install Homebrew dependencies and the Rust tools that `just lint` and `just test-template` need
setup:
brew bundle install
just install-tools

# Install the pinned Rust tools (`cargo_tools`), installing cargo-binstall from source first if it's missing
install-tools:
command -v cargo-binstall >/dev/null || cargo install --locked cargo-binstall@{{binstall_version}}
cargo binstall --no-confirm {{cargo_tools}}

# Bump `binstall_version` and the `cargo_tools` versions in this justfile to their latest releases, then install them
update:
#!/usr/bin/env bash
set -euo pipefail
latest() {
local version
version="$(cargo info --registry crates-io "$1" 2>/dev/null | sed -n 's/^version: \([^ ]*\).*/\1/p' | head -1)"
[[ -n "$version" ]] || { echo "could not find the latest version of $1" >&2; exit 1; }
echo "$version"
}
binstall="$(latest cargo-binstall)"
tools=""
for tool in {{cargo_tools}}; do
name="${tool%@*}"
tools+="${tools:+ }$name@$(latest "$name")"
done
# perl rather than sed -i, which takes different arguments on macOS and Linux; the versions are
# passed through the environment so perl doesn't treat the `@` in `name@version` as an array
BINSTALL="$binstall" TOOLS="$tools" perl -pi -e 's/^binstall_version := ".*"/binstall_version := "$ENV{BINSTALL}"/; s/^cargo_tools := ".*"/cargo_tools := "$ENV{TOOLS}"/' "{{justfile()}}"
echo "binstall_version := \"$binstall\""
echo "cargo_tools := \"$tools\""
just install-tools

# Format TOML files
fmt:
tombi format

# Check spelling with typos and TOML formatting with tombi, check GitHub Actions workflows with actionlint and audit them for security issues with zizmor
lint:
typos
tombi format --check
actionlint .github/workflows/*.yml
zizmor .

# Render the template into a temp directory, then lint, test, check coverage and check dependencies of the generated project
test-template:
#!/usr/bin/env bash
set -euo pipefail
Expand All @@ -18,3 +73,9 @@ test-template:
cd "$tmp/test-project"
just lint
just test
just coverage
just deny

# Scan the Git history for committed secrets (pass extra flags, e.g. `just gitleaks --log-opts=-5`)
gitleaks *args:
gitleaks git --redact {{args}}
10 changes: 10 additions & 0 deletions prek.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Git hooks managed by prek (https://prek.j178.dev). Install with: prek install

[[repos]]
repo = "local"
hooks = [
# Run the same checks as CI before each commit
{ id = "just-lint", name = "just lint", entry = "just lint", language = "system", pass_filenames = false, always_run = true },
# Block commits that add secrets (scans only the staged changes)
{ id = "gitleaks", name = "gitleaks", entry = "gitleaks git --pre-commit --staged --redact", language = "system", pass_filenames = false, always_run = true },
]
7 changes: 5 additions & 2 deletions template/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"capAdd": ["SYS_PTRACE"],
"securityOpt": ["seccomp=unconfined"],

// Install the toolchain pinned in rust-toolchain.toml and the `just` command runner
"postCreateCommand": "rustup toolchain install && sudo apt-get update && sudo apt-get install -y --no-install-recommends just",
// Install the toolchain pinned in rust-toolchain.toml, the `just` command runner, the zizmor workflow linter, the Rust tools in the justfile's
// `cargo_tools` (cargo-deny, cargo-nextest, cargo-shear, cargo-llvm-cov, typos) with cargo-binstall via `just install-tools`,
// uv, actionlint and the tombi TOML formatter (from PyPI, since Debian doesn't package them), the prek Git hook manager and the gitleaks secret scanner (a checksum-verified GitHub
// release, since Debian's package is too old), then install the hooks if the workspace is a Git repository
"postCreateCommand": "rustup toolchain install && sudo apt-get update && sudo apt-get install -y --no-install-recommends just && cargo install --locked zizmor prek uv && just install-tools && uv tool install actionlint-py==1.7.12.25 && uv tool install tombi && uv tool update-shell && v=8.30.1 && a=$(dpkg --print-architecture | sed s/amd64/x64/) && cd /tmp && curl -fsSLO https://github.com/gitleaks/gitleaks/releases/download/v$v/gitleaks_${v}_linux_$a.tar.gz && curl -fsSL https://github.com/gitleaks/gitleaks/releases/download/v$v/gitleaks_${v}_checksums.txt | sha256sum --check --ignore-missing && sudo tar -xzf gitleaks_${v}_linux_$a.tar.gz -C /usr/local/bin gitleaks && rm gitleaks_${v}_linux_$a.tar.gz && cd - && if [ -e .git ]; then prek install; fi",

"customizations": {
"vscode": {
Expand Down
26 changes: 26 additions & 0 deletions template/.github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dependabot version updates; patch and minor updates are auto-merged by dependabot-auto-merge.yml
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
# Wait a week after a release, so broken or malicious versions are more likely to be caught first
cooldown:
default-days: 7
groups:
github-actions:
patterns:
- "*"

- package-ecosystem: cargo
directory: /
schedule:
interval: weekly
# Wait a week after a release, so broken or malicious versions are more likely to be caught first
cooldown:
default-days: 7
groups:
cargo:
patterns:
- "*"
11 changes: 11 additions & 0 deletions template/.github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Summary

<!-- What does this change and why? -->

## Checklist

- [ ] `just fmt` has been run
- [ ] `just lint` passes
- [ ] `just test` passes
- [ ] Tests added or updated for new behaviour
- [ ] Docs updated if needed (`README.md`, `CLAUDE.md`)
30 changes: 30 additions & 0 deletions template/.github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Dependabot auto-merge

# pull_request (not pull_request_target) is enough: Dependabot PRs come from a branch in this
# repository, so the token gets the permissions granted below
on: pull_request

permissions: {}

jobs:
auto-merge:
runs-on: ubuntu-latest
# Check the PR author rather than github.actor, which can be someone re-running the workflow
if: github.event.pull_request.user.login == 'dependabot[bot]'
permissions:
contents: write
pull-requests: write

steps:
- name: Fetch Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@25dd0e34f4fe68f24cc83900b1fe3fe149efef98 # v3.1.0

# Patch and minor updates merge once the required checks pass; major updates wait for review.
# Auto-merge must be allowed in the repository settings, with required status checks on main.
- name: Enable auto-merge for patch and minor updates
if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Loading
Loading