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
160 changes: 160 additions & 0 deletions .github/workflows/version-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
name: Version check

on:
workflow_call:
inputs:
config:
description: File that lists the versions to check
required: false
type: string
default: .github/version-check.yml

permissions: {}

jobs:
check:
runs-on: ubuntu-24.04
permissions:
contents: read
issues: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Compare pinned versions with the latest ones
id: compare
env:
CONFIG: ${{ inputs.config }}
GH_TOKEN: ${{ github.token }}
run: |
yq -o=json . "$CONFIG" > "$RUNNER_TEMP/version-check.json"
python3 - "$RUNNER_TEMP/version-check.json" "$RUNNER_TEMP/version-check.md" <<'PY'
import json
import os
import re
import subprocess
import sys
import urllib.request

config_path, report_path = sys.argv[1], sys.argv[2]
checks = json.load(open(config_path))["checks"]
token = os.environ.get("GH_TOKEN", "")


def get(url, github=False):
headers = {"Accept": "application/json", "User-Agent": "think-cube-version-check"}
if github and token:
headers["Authorization"] = f"Bearer {token}"
with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=30) as response:
return json.load(response)


def version_key(version):
return tuple(int(n) for n in re.findall(r"\d+", version))


def pinned(check):
# A yq path reads a value from YAML; a pattern then picks the version out of it, or out of the whole file.
if "path" in check:
text = subprocess.run(["yq", check["path"], check["file"]],
check=True, capture_output=True, text=True).stdout.strip()
else:
text = open(check["file"]).read()
pattern = check.get("pattern", "(.+)")
match = re.search(pattern, text, re.MULTILINE)
if not match:
raise ValueError(f"'{pattern}' matches nothing in {check['file']}")
return match.group(1)


def latest(check):
source, repository = check["source"], check["repository"]
if source == "github-release":
release = get(f"https://api.github.com/repos/{repository}/releases/latest", github=True)
return release["tag_name"].removeprefix("v"), release["html_url"]
if source == "github-commit":
commit = get(f"https://api.github.com/repos/{repository}/commits/HEAD", github=True)
return commit["sha"], commit["html_url"]
if source == "docker-hub":
tag_pattern = re.compile(check["tag-pattern"])
url = f"https://hub.docker.com/v2/repositories/{repository}/tags?page_size=100&ordering=last_updated"
versions = []
for _ in range(3):
page = get(url)
for tag in page["results"]:
match = tag_pattern.fullmatch(tag["name"])
if match:
versions.append(match.group(1) if match.groups() else tag["name"])
url = page.get("next")
if not url:
break
if not versions:
raise ValueError(f"no tag of {repository} matches '{check['tag-pattern']}'")
namespace, name = repository.split("/")
link = f"https://hub.docker.com/_/{name}" if namespace == "library" else f"https://hub.docker.com/r/{repository}"
return max(versions, key=version_key), link
raise ValueError(f"unknown source '{source}'")


def short(value):
return value[:7] if re.fullmatch(r"[0-9a-f]{40}", value) else value


outdated, notes, errors = [], [], []
for check in checks:
try:
current = pinned(check)
newest, link = latest(check)
if check["source"] == "github-commit":
is_newer = current != newest
else:
is_newer = version_key(newest) > version_key(current)
print(f"{check['name']}: pinned {short(current)}, latest {short(newest)}")
if is_newer:
outdated.append(f"| {check['name']} | `{check['file']}` | `{short(current)}` | [`{short(newest)}`]({link}) |")
if check.get("note"):
notes.append(f"- **{check['name']}:** {check['note']}")
except Exception as error:
errors.append(f"{check['name']}: {error}")
print(f"::error::{check['name']}: {error}")

lines = [
"The weekly version check found newer versions of pinned dependencies.",
"",
"| Dependency | File | Pinned | Latest |",
"|---|---|---|---|",
*outdated,
]
if notes:
lines += ["", "### Notes", "", *notes]
lines += ["", "This issue is updated by the weekly version check and closes itself once everything is up to date."]
with open(report_path, "w") as report:
report.write("\n".join(lines) + "\n")

with open(os.environ.get("GITHUB_OUTPUT", os.devnull), "a") as output:
output.write(f"outdated={len(outdated)}\nerrors={len(errors)}\n")
PY
- name: Open, update or close the issue
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
OUTDATED: ${{ steps.compare.outputs.outdated }}
ERRORS: ${{ steps.compare.outputs.errors }}
run: |
label=version-check
gh label create "$label" --color 0E8A16 --description "Opened by the weekly version check" --force > /dev/null
number=$(gh issue list --label "$label" --state open --json number --jq '.[0].number // empty')
if [ "$OUTDATED" -gt 0 ]; then
if [ -n "$number" ]; then
gh issue edit "$number" --body-file "$RUNNER_TEMP/version-check.md"
else
gh issue create --title "Newer versions of pinned dependencies are available" \
--label "$label" --body-file "$RUNNER_TEMP/version-check.md"
fi
elif [ -n "$number" ]; then
gh issue close "$number" --comment "Everything is up to date."
fi
if [ "$ERRORS" -gt 0 ]; then
echo "::error::$ERRORS check(s) could not run; see the errors above"
exit 1
fi
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Reusable GitHub Actions workflows for Think-Cube repositories.
|---|---|
| [`container-image.yml`](.github/workflows/container-image.yml) | Lint, test, scan, sign and publish a multi-arch container image |
| [`helm-chart.yml`](.github/workflows/helm-chart.yml) | Lint, validate, install, sign and publish a Helm chart as an OCI artifact |
| [`version-check.yml`](.github/workflows/version-check.yml) | Open an issue when versions Dependabot does not track have newer releases |

## container-image

Expand Down Expand Up @@ -142,6 +143,64 @@ cosign verify ghcr.io/think-cube/charts/<chart>:<version> \
--certificate-identity-regexp '^https://github\.com/Think-Cube/workflows/\.github/workflows/helm-chart\.yml@refs/tags/v.+$'
```

## version-check

Dependabot keeps base images, packages and actions current, but it cannot see versions pinned in build arguments, Helm values or Git commits, and it deliberately skips updates such as a new RabbitMQ minor. This workflow compares those pins with the latest releases and keeps a single issue, labelled `version-check`, up to date: it opens the issue when something is outdated, updates it as findings change and closes it once everything is current. It does not change any files, because these updates usually need a manual step.

### Usage

```yaml
name: Version check

on:
schedule:
- cron: "23 5 * * 1"
workflow_dispatch:

permissions: {}

jobs:
check:
permissions:
contents: read
issues: write
uses: Think-Cube/workflows/.github/workflows/version-check.yml@v1.3.0
```

### Configuration

List the checks in `.github/version-check.yml`:

```yaml
checks:
- name: Azure Pipelines agent
file: Dockerfile
pattern: 'ARG AGENT_VERSION=(\S+)'
source: github-release
repository: microsoft/azure-pipelines-agent
note: Update both checksums too.
- name: curl used by helm test
file: values.yaml
path: .test.image.tag
pattern: '^([^@]+)'
source: docker-hub
repository: curlimages/curl
tag-pattern: '(\d+\.\d+\.\d+)'
```

| Key | Description |
|---|---|
| `name` | Shown in the issue |
| `file` | File that pins the version |
| `path` | Optional yq path to the value in a YAML file |
| `pattern` | Regular expression whose first group is the pinned version, applied to the `path` value or to the whole file |
| `source` | `github-release` (latest release tag), `github-commit` (head of the default branch) or `docker-hub` (highest matching tag) |
| `repository` | `owner/repo` on GitHub, or `namespace/name` on Docker Hub (`library/<name>` for official images) |
| `tag-pattern` | For `docker-hub`: tags to consider; its first group, if any, is the version compared |
| `note` | Optional instruction shown in the issue |

Versions are compared numerically, and commits by identity. A check that cannot run fails the workflow without affecting the others.

## Versioning

Releases are tagged `vMAJOR.MINOR.PATCH`. Callers pin an exact tag, and Dependabot in the calling repository proposes upgrades. A new major version means callers must change their inputs, secrets or permissions.
Expand Down