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
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ updates:
multi-ecosystem-group: "tooling"
cooldown:
default-days: 7

- package-ecosystem: "terraform"
directories:
- "/"
- "/modules/*"
schedule:
interval: "monthly"
groups:
monthly-batch:
patterns:
- "*"
pull-request-branch-name:
separator: "/"
commit-message:
prefix: "chore"
include: "scope"
cooldown:
default-days: 7
142 changes: 142 additions & 0 deletions .github/workflows/new-repos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
name: new-repos

# Opens a pull request that adds any repository not yet listed in repos.yaml.
# See scripts/new_repos.py. While that pull request is open, runs leave it
# alone so edits pushed to it aren't overwritten; repositories created in the
# meantime are picked up after it merges.

on:
schedule:
- cron: "17 9 * * *"
workflow_dispatch:

permissions: {}

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

env:
OWNER: horrell-consulting
BRANCH: bot/new-repos

jobs:
propose:
# Skip until the GitHub App is set up (see README).
if: vars.NEW_REPOS_APP_CLIENT_ID != ''
runs-on: ubuntu-slim
permissions:
contents: read
steps:
# Read-only access to every repository the app is installed on, to list
# private repositories too.
- name: Read token
id: read-token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.NEW_REPOS_APP_CLIENT_ID }}
private-key: ${{ secrets.NEW_REPOS_APP_PRIVATE_KEY }}
owner: ${{ env.OWNER }} # zizmor: ignore[github-app] metadata: read only, and it must see every repository
permission-metadata: read

# Write access to this repository only, to push the branch and open the
# pull request. Pull requests opened with an app token trigger CI.
- name: Write token
id: write-token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.NEW_REPOS_APP_CLIENT_ID }}
private-key: ${{ secrets.NEW_REPOS_APP_PRIVATE_KEY }}
owner: ${{ env.OWNER }}
repositories: github-settings
permission-contents: write
permission-pull-requests: write

- name: Check for an open pull request
id: open-pr
env:
GH_TOKEN: ${{ steps.write-token.outputs.token }}
REPO: ${{ github.repository }}
run: |
pr=$(gh pr list --repo "$REPO" --head "$BRANCH" --state open --json url --jq '.[0].url // empty')
if [ -n "$pr" ]; then
echo "A pull request is already open, leaving it alone: $pr"
echo "open=true" >> "$GITHUB_OUTPUT"
fi

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
if: steps.open-pr.outputs.open != 'true'
with:
persist-credentials: false

- name: Find new repositories
id: find
if: steps.open-pr.outputs.open != 'true'
env:
GH_TOKEN: ${{ steps.read-token.outputs.token }}
run: |
python3 scripts/new_repos.py --owner "$OWNER" > "$RUNNER_TEMP/added.txt"
if [ -s "$RUNNER_TEMP/added.txt" ]; then
echo "found=true" >> "$GITHUB_OUTPUT"
cat "$RUNNER_TEMP/added.txt"
else
echo "No new repositories."
fi

# Commit through the API so the commit is signed by the app.
- name: Open the pull request
if: steps.find.outputs.found == 'true'
env:
GH_TOKEN: ${{ steps.write-token.outputs.token }}
REPO: ${{ github.repository }}
BASE: main
run: |
set -euo pipefail
names=$(paste -sd ' ' "$RUNNER_TEMP/added.txt")
list=$(while read -r name; do printf -- '- %s\n' "\`$name\`"; done < "$RUNNER_TEMP/added.txt")
base_sha=$(git rev-parse HEAD)

# Point the bot branch at the current default branch.
if gh api "repos/$REPO/git/ref/heads/$BRANCH" > /dev/null 2>&1; then
gh api -X PATCH "repos/$REPO/git/refs/heads/$BRANCH" \
-f sha="$base_sha" -F force=true > /dev/null
else
gh api "repos/$REPO/git/refs" \
-f ref="refs/heads/$BRANCH" -f sha="$base_sha" > /dev/null
fi

file_sha=$(gh api "repos/$REPO/contents/repos.yaml?ref=$BRANCH" --jq .sha)
gh api -X PUT "repos/$REPO/contents/repos.yaml" \
-f message="feat: manage $names" \
-f content="$(base64 -w0 repos.yaml)" \
-f sha="$file_sha" \
-f branch="$BRANCH" > /dev/null

body=$(cat <<EOF
Adds repositories that aren't managed yet:

$list

Each entry copies the repository's current settings, so applying should
only change the baseline. Before merging:

- [ ] Read the HCP plan (\`Terraform Cloud/horrell/horrell-consulting-github-settings\`)
and check it only imports these repositories and changes
baseline settings. Merging applies it.
- [ ] Add \`required_status_checks\` where the repository has CI.
- [ ] If a repository shouldn't be managed, archive it and drop its
entry; otherwise the next run proposes it again.
EOF
)

gh pr create --repo "$REPO" --base "$BASE" --head "$BRANCH" \
--title "feat: manage new repositories" --body "$body"

alert:
needs: [propose]
if: ${{ !cancelled() }}
permissions:
issues: write # open, comment on and close the alert issue
uses: chorrell/workflows/.github/workflows/scheduled-failure-issue.yml@v1
with:
failed: ${{ contains(needs.*.result, 'failure') }}
3 changes: 3 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ concurrency:
jobs:
pre-commit:
uses: chorrell/workflows/.github/workflows/pre-commit.yml@v1
with:
# Terraform hooks need terraform and tflint, which the runner lacks; terraform.yml covers them
skip: terraform_fmt,terraform_validate,terraform_tflint
47 changes: 47 additions & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
name: terraform

on:
pull_request:
branches:
- main
merge_group:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
# fmt, init, validate and tflint from the shared workflow
terraform:
uses: chorrell/workflows/.github/workflows/terraform-validate.yml@v1
with:
terraform-version: latest
# app_auth {} reads these as required arguments, even though validate
# never authenticates. The real values are on the HCP workspace.
validate-env: >-
{"GITHUB_APP_ID": "0", "GITHUB_APP_INSTALLATION_ID": "0",
"GITHUB_APP_PEM_FILE": "placeholder"}

# The required check is `validate`, and a called workflow's check is
# prefixed with the caller's job id, so this gate reports under that name.
# Add every new job to needs.
validate:
if: always()
needs: [terraform]
runs-on: ubuntu-slim
steps:
- name: Require every job to succeed
env:
RESULTS: ${{ join(needs.*.result, ' ') }}
run: |
echo "Job results: ${RESULTS}"
for result in ${RESULTS}; do
if [ "${result}" != "success" ]; then
echo "::error::A required job did not succeed (${result})"
exit 1
fi
done
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ Thumbs.db

# Claude Code local settings
.claude/settings.local.json

# Terraform / OpenTofu
.terraform/
*.tfstate
*.tfstate.*
*.tfplan
crash.log
crash.*.log
override.tf
override.tf.json
*_override.tf
*_override.tf.json
.terraformrc
terraform.rc

# Lock files that terraform_validate creates in modules; only the root lock file is committed
modules/*/.terraform.lock.hcl

# Python
__pycache__/
.ruff_cache/
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ repos:
rev: v1.31.1
hooks:
- id: zizmor

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.17.2
hooks:
- id: ruff-check
- id: ruff-format

- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.109.3
hooks:
- id: terraform_fmt
- id: terraform_validate
# Placeholders for app_auth {}; see .github/workflows/terraform.yml.
args:
- --env-vars=GITHUB_APP_ID=0
- --env-vars=GITHUB_APP_INSTALLATION_ID=0
- --env-vars=GITHUB_APP_PEM_FILE=placeholder
- id: terraform_tflint
25 changes: 25 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading