Tag migrations with build-provenance + WAR hash, not a bare git version

WEB-INF/VERSION only ever held a git short-hash, which claimed more than
it could prove: two builds of the same commit can still differ (local
library jars, anything outside git), so a git version alone doesn't
guarantee what's actually in the WAR on the server.

Replaced with WEB-INF/TAG (<build-timestamp>-<git-short-hash>[-dirty] -
build time, not commit time, so a rebuilt/redeployed same commit still
gets a unique value, and "-dirty" flags an uncommitted working tree at
build time) plus a sha256 of the sportrum.war actually sitting on the
server, computed here rather than trusted from the build since a file
can't hash itself and this also catches corruption/tampering in transit.
resolve-migration-tag now reads both and the migration tag becomes
<build-tag>-<war-hash>[-<label>] instead of <git-version>[-<label>].

README and build.xml's inline docs/target descriptions updated to match.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
This commit is contained in:
Claude 2026-10-01 09:37:59 +00:00
parent a34ea9a9bc
commit 80f18a9f2b
No known key found for this signature in database
2 changed files with 86 additions and 47 deletions

View file

@ -10,25 +10,30 @@ This project deliberately does NOT build or upload `sportrum.war`/
project needs to make so `ant "migrate(@host)"` here can tag a deploy by
the exact commit it shipped.
## Required: embedding WEB-INF/VERSION in the webapp build
## Required: embedding WEB-INF/TAG in the webapp build
`ant "migrate(@host)"` (see `build.xml`) tags every migration with the git
version of whatever `sportrum.war` is already sitting on the server —
read back off the WAR itself, not derived from this repo's git history
(there isn't a relationship between the two repos to derive it from).
That means the sportrum webapp project's own `build.xml` needs to embed a
`WEB-INF/VERSION` file into the WAR, containing a single line:
`ant "migrate(@host)"` (see `build.xml`) tags every migration with a build
tag read back off whatever `sportrum.war` is already sitting on the server
(not derived from this repo's git history — there isn't a relationship
between the two repos to derive it from), plus a hash of that `sportrum.war`
computed here. That means the sportrum webapp project's own `build.xml`
needs to embed a `WEB-INF/TAG` file into the WAR, containing a single line:
```
<build-timestamp>-<git-short-hash>
<build-timestamp>-<git-short-hash>[-dirty]
```
e.g. `20260930-184512-a3f9c21`. Timestamp first so tagged migration
folders on the server sort chronologically; build time (not commit time),
so the value stays unique even if the same commit is ever rebuilt and
redeployed again; `WEB-INF/`, not the WAR root, since anything outside
`WEB-INF/` is directly servable over HTTP by Tomcat and a commit hash
shouldn't be publicly fetchable.
e.g. `20260930-184512-a3f9c21` or `20260930-184512-a3f9c21-dirty`. This is
build provenance, not a guarantee of what the WAR actually contains (things
outside git — e.g. local library jars — can still differ between two builds
of the same commit), which is why `resolve-migration-tag` also hashes the
WAR itself rather than trusting this string alone. Timestamp first so
tagged migration folders on the server sort chronologically and so it's
easy to tell when a build happened; build time (not commit time), so the
value stays unique even if the same commit is ever rebuilt and redeployed
again; the `-dirty` suffix marks a build whose working tree had uncommitted
changes at build time. `WEB-INF/`, not the WAR root, since anything outside
`WEB-INF/` is directly servable over HTTP by Tomcat.
Add this near the top of the webapp project's `war` target, before the
`<war>` task itself runs:
@ -42,19 +47,34 @@ Add this near the top of the webapp project's `war` target, before the
<arg value="--short"/>
<arg value="HEAD"/>
</exec>
<property name="build.version" value="${build.timestamp}-${build.gitrev}"/>
<echo message="Building version ${build.version}"/>
<mkdir dir="${build}/version-stage"/>
<echo file="${build}/version-stage/VERSION" message="${build.version}"/>
<exec executable="git" outputproperty="git.dirty.files" failonerror="true">
<arg value="status"/>
<arg value="--porcelain"/>
</exec>
<condition property="git.dirty">
<not><equals arg1="${git.dirty.files}" arg2=""/></not>
</condition>
<condition property="dirty.suffix" value="-dirty">
<isset property="git.dirty"/>
</condition>
<property name="dirty.suffix" value=""/>
<property name="build.tag" value="${build.timestamp}-${build.gitrev}${dirty.suffix}"/>
<echo message="Building ${build.tag}"/>
<mkdir dir="${build}/tag-stage"/>
<echo file="${build}/tag-stage/TAG" message="${build.tag}"/>
<echo file="${build}/tag-stage/DIRTY-FILES" message="${git.dirty.files}" if="git.dirty"/>
```
Then add a `<webinf>` fileset to the existing `<war destfile="${build}/sportrum.war" ...>`
task call, alongside whatever filesets are already there:
```xml
<webinf dir="${build}/version-stage">
<include name="VERSION"/>
<webinf dir="${build}/tag-stage">
<include name="TAG"/>
<include name="DIRTY-FILES"/>
</webinf>
```

View file

@ -268,19 +268,27 @@ Preferences -> Ant -> Runtime -> Classpath -> Ant Home Entries — same fix.
</sequential>
</macrodef>
<!-- Resolves deploy.tag for migrate(@host): <git-version>[-<label>].
The git-version half is read back off the sportrum.war ALREADY
uploaded by the webapp project's own release-all - see this
project's README for the WEB-INF/VERSION snippet that project's
build.xml needs for this to work. WEB-INF/, not the WAR root,
since anything outside WEB-INF/ is directly servable over HTTP by
Tomcat and a commit hash shouldn't be publicly fetchable.
<!-- Resolves deploy.tag for migrate(@host): <build-tag>-<war-hash>[-<label>].
build-tag is read back off the sportrum.war ALREADY uploaded by the
webapp project's own release-all - see this project's README for
the WEB-INF/TAG snippet that project's build.xml needs for this to
work. It's a build-provenance string (timestamp-githash, plus a
"-dirty" suffix if the working tree had uncommitted changes at
build time) - NOT a guarantee of what the WAR actually contains,
since things outside git (e.g. local library jars) can still
differ between two builds of the same commit. That's what
war-hash is for: a hash of the actual sportrum.war sitting on the
server, computed here rather than embedded at build time, since a
file can't contain a hash of itself and this also catches any
corruption/tampering in transit. WEB-INF/, not the WAR root, since
anything outside WEB-INF/ is directly servable over HTTP by Tomcat
and this shouldn't be publicly fetchable.
The label half is optional and interactive by design (Ant's
<input> task, confirmed empirically: it skips prompting entirely
if deploy.label is already set, e.g. via -Ddeploy.label=... for a
scripted run - and confirmed working from a plain piped/typed
stdin). Hit enter to skip it; the git version alone is already a
stdin). Hit enter to skip it; build-tag plus war-hash is already a
complete, unique, non-blank tag on its own. -->
<macrodef name="resolve-migration-tag">
<attribute name="host"/>
@ -291,27 +299,37 @@ Preferences -> Ant -> Runtime -> Classpath -> Ant Home Entries — same fix.
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="unzip -p /home/sportrum/webapp/sportrum.war WEB-INF/VERSION | tr -d '\n'"
outputproperty="deploy.gitversion"
command="unzip -p /home/sportrum/webapp/sportrum.war WEB-INF/TAG | tr -d '\n'"
outputproperty="deploy.buildtag"
failonerror="true"/>
<!-- tr -d strips any trailing newline the webapp build's echo/
file-write may have left in WEB-INF/VERSION - whether or
not it did isn't this project's business to assume either
way, and a stray newline landing in a directory name would
be a much stranger failure to debug than this one-liner is
to write. -->
file-write may have left in WEB-INF/TAG - whether or not it
did isn't this project's business to assume either way,
and a stray newline landing in a directory name would be a
much stranger failure to debug than this one-liner is to
write. -->
<condition property="deploy.gitversion.blank">
<equals arg1="${deploy.gitversion}" arg2=""/>
<condition property="deploy.buildtag.blank">
<equals arg1="${deploy.buildtag}" arg2=""/>
</condition>
<fail if="deploy.gitversion.blank">
<fail if="deploy.buildtag.blank">
Couldn't read WEB-INF/VERSION out of the sportrum.war on @{host}. Either
Couldn't read WEB-INF/TAG out of the sportrum.war on @{host}. Either
"ant release-all" hasn't been run against this server yet, or the webapp
project's build.xml doesn't embed WEB-INF/VERSION yet - see this project's
project's build.xml doesn't embed WEB-INF/TAG yet - see this project's
README for the snippet it needs.
</fail>
<sshexec host="@{host}"
port="${deploy.ssh.port}"
username="${deploy.user}"
keyfile="${deploy.keyfile}"
passphrase="${deploy.key.passphrase}"
knownhosts="${deploy.knownhosts}"
command="sha256sum /home/sportrum/webapp/sportrum.war | cut -c1-12"
outputproperty="deploy.warhash"
failonerror="true"/>
<input message="Optional short label for this migration (e.g. 'invoice-table') - press Enter to skip:" addproperty="deploy.label"/>
<condition property="deploy.label.unsafe">
@ -335,10 +353,10 @@ on the server. Run again without spaces or other punctuation.
override a value the condition above already set.
Confirmed both branches empirically before relying on
this. -->
<condition property="deploy.tag" value="${deploy.gitversion}-${deploy.label}">
<condition property="deploy.tag" value="${deploy.buildtag}-${deploy.warhash}-${deploy.label}">
<not><equals arg1="${deploy.label}" arg2=""/></not>
</condition>
<property name="deploy.tag" value="${deploy.gitversion}"/>
<property name="deploy.tag" value="${deploy.buildtag}-${deploy.warhash}"/>
<echo message="Migration tag: ${deploy.tag}"/>
</sequential>
@ -386,10 +404,11 @@ rationale:
state. Then: WAR+logs backup only (no SQL dump - nothing to protect
against), install the new sportrum.war, restart Tomcat (still needed;
class files may have changed). No manager-exec.sh runs at all.
- migrate(@server): for a change that DOES touch the DB. Prompts for a
tag - the git version already embedded in sportrum.war's
WEB-INF/VERSION (see this project's README for the webapp project's
own build.xml needs), plus an optional short label - then runs the
- migrate(@server): for a change that DOES touch the DB. Resolves a tag
from the build-provenance string already embedded in sportrum.war's
WEB-INF/TAG (see this project's README for the webapp project's own
build.xml needs) plus a hash of the sportrum.war itself, plus an
optional short label - then runs the
same schema check as above but only logs the result rather than
blocking (a diff here is expected, not a sign anything's wrong). Then:
full SQL+WAR+logs backup (the regular short-lived tier AND an
@ -445,7 +464,7 @@ else to configure here.</echo>
</target>
<target name="migrate(@local.sportrum.se)" depends="install-scripts(@local.sportrum.se)"
description="For a change that DOES migrate the DB. Resolves a tag (git version from sportrum.war's WEB-INF/VERSION + optional label), logs the pending schema diff, then runs upgrade.sh migrate on local.sportrum.se (tagged before/after backup kept indefinitely, DB migration with saved transcript), then confirms https://local.sportrum.se/ responds.">
description="For a change that DOES migrate the DB. Resolves a tag (build-provenance string from sportrum.war's WEB-INF/TAG + a hash of sportrum.war itself + optional label), logs the pending schema diff, then runs upgrade.sh migrate on local.sportrum.se (tagged before/after backup kept indefinitely, DB migration with saved transcript), then confirms https://local.sportrum.se/ responds.">
<resolve-migration-tag host="local.sportrum.se"/>
<run-schema-check host="local.sportrum.se" haltonerror="false"/>
<run-migrate host="local.sportrum.se" tag="${deploy.tag}"/>