Skip to content

build: cut the backend CI build from 11:54 to about 5 minutes - #4060

Open
Macbet wants to merge 12 commits into
refactor/api-restclientfrom
build/speed-up-backend-build
Open

Macbet wants to merge 12 commits into
refactor/api-restclientfrom
build/speed-up-backend-build

Conversation

@Macbet

@Macbet Macbet commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

The backend CI build took 11:54. It now takes about 5:15 (5:07 and 5:26 in two runs of the same commit). All 4836 tests pass, and Sonar coverage is unchanged (76.9% vs 77.0% on #4058). Locally, mvn -T 1C verify takes 3:04 instead of 6:42.

CI: tests split across parallel jobs

  • The tests are CPU-bound, and a runner has 4 vCPUs, so they now run in five jobs: the api tests in four shards picked by scripts/ci/test-shard.py, and common, registry and executor in a fifth. build-jammy waits for them, merges their JaCoCo data and runs the Sonar analysis without running the tests again. Only build-jammy has the secrets.
  • Each job uses 2 surefire forks. A 4-CPU container measured 2 forks as fastest: a shard took 118s with 2 forks, 125s with 1 and 148s with 3.
  • The JaCoCo agent now instruments only io.terrakube.*. Instrumenting every Spring, Hibernate and Elide class added about a third to each JVM's first Spring context start (14.1s vs 10.4s locally), and every fork pays that start.
  • Maven dependencies are cached separately for each kind of job. setup-java's single cache key could be saved only by the first job to finish, so the api shards downloaded about 2000 artifacts on every run.
  • Test output goes to report files that are uploaded as a test-results-* artifact. The job log keeps the build, the test summaries and the failures. The test profile no longer logs Spring Security at DEBUG or each Liquibase changeset.

Build

  • Surefire forks 3 JVMs for api and executor locally (terrakube.test.forkCount), and -T 1C builds registry and executor while the api tests run.
  • The bound goal is now build-image-no-fork, so packaging no longer recompiles and retests every module. The release workflows and terrakubeBuild.sh run package, which builds each image once (spring-boot:build-image on the command line would now build it twice).

Test fixes that parallel runs exposed

  • Each api fork gets its own user.home. Local storage keys files by job id, and every fork's H2 database numbers jobs from 1, so forks read each other's files.
  • registry's WireMock used the fixed port 9999. LocalTerraformStateImplTest deleted ~/.terraform-spring-boot while other modules' tests were using it. TerraformHttpBackendIntegrationTest shared the seeded workspace's state file. ReferenceScopeTests relied on a permission that CollectionTests granted earlier in the same JVM.
  • JobReconciliationSweepIntegrationTest calls the sweep directly instead of waiting for its 30s cron. More test classes share a cached Spring context.

Worth checking

  • build-jammy keeps its name, so a required check on it still works.
  • The first release after merge builds images through package. I verified locally that this builds one image per module, but did not run the release workflow.
  • api test classes still share one H2 database within a fork. A class that relies on state left by another can fail in one shard but not when the whole suite runs in one JVM, as ReferenceScopeTests did.

@Macbet
Macbet added this pull request to stack #4056 November 29, 2026 11:18
@github-actions github-actions Bot added documentation Improvements or additions to documentation dependencies Pull requests that update a dependency file terrakube-api terrakube-registry terrakube-executor area/scheduler Touches job scheduling, queueing or locking area/ci Touches CI, containers or dev environment author/contributor Authored by a returning contributor size/L 250-999 lines changed type/chore Tests, CI, build or maintenance labels Nov 29, 2026
@Macbet Macbet changed the title build: cut the backend build from 6:42 to about 3:10 build: cut the backend CI build from 11:54 to about 5 minutes Nov 29, 2026
@Macbet
Macbet force-pushed the build/speed-up-backend-build branch from 1ed46ba to 864cc89 Compare November 29, 2026 12:58
@alfespa17 alfespa17 self-assigned this Nov 29, 2026
@Macbet
Macbet force-pushed the build/speed-up-backend-build branch from 864cc89 to 3843e77 Compare November 29, 2026 19:29
The API suite ran in one JVM after every other module and took 5 of the
build's 6:42. Surefire now forks 3 JVMs for api and executor
(terrakube.test.forkCount, default 1), and CI builds modules with -T 1C so
registry and executor run beside api. Measured locally: 6:42 -> ~3:10.

Parallel JVMs exposed shared state that a single JVM had hidden:
- registry tests bound WireMock to fixed port 9999; it now uses a free port
  passed in with @DynamicPropertySource
- LocalTerraformStateImplTest deleted ~/.terraform-spring-boot recursively
  while api and registry tests used it; it now runs with a private user.home
- TerraformHttpBackendIntegrationTest shared the seeded workspace's backend
  state file with WorkspaceTests; it now creates its own workspace
The bound build-image goal forks the lifecycle to package, so after the tests
every app module compiled, tested and repackaged a second time, even with
-Dspring-boot.build-image.skip=true. build-image-no-fork is the goal Spring
Boot provides for bound executions.

Invoking spring-boot:build-image on the command line would now build each
image twice (its own fork runs the bound no-fork execution), so the release
workflows, terrakubeBuild.sh and the minikube notes run package instead.
Two tests waited for the sweep's */30 cron trigger, up to 30s each. They now
call sweep.execute(null), as the third test in the class already does, and
keep polling only for ScheduleJob's asynchronous pickup. The class drops
from 44s to about 8s.
Each distinct set of bean overrides and test properties boots its own API
context.
- The three login broker test classes now take their properties and the
  DexExchangeClient mock from LoginBrokerEnabledTestBase. Its api-url is a
  loopback address that differs from the test profile's default, so the
  well-known test still proves the configured value is used.
- JobPlanArtifactsPermissionTest mocks StorageTypeService instead of spying
  on it (deleteJobArtifacts is void), joining the other storage-mock tests.
- WorkspaceUnlockWakesQueueTests mocks ScheduleJobService with Quartz off,
  the same overrides as the RemoteTfe*Tests. It only verifies calls.
setup-java's cache: maven replaces the hand-written actions/cache steps. Its
key hashes every pom.xml (the release workflows hashed only the root one, so
module changes restored a stale cache) and includes the runner architecture,
so the arm release job no longer shares an x86 cache. It caches only
~/.m2/repository. It has no restore-keys: the first run after a pom change
downloads dependencies again.
The test profile set Spring Security and the organization checks to DEBUG,
and every context start printed each Liquibase changeset and Hibernate
deprecation notice: most of the 15 MB CI log. No test asserts on those
loggers; the tests that capture logs attach to their own classes.
On a 4-vCPU runner the tests are CPU-bound: more surefire forks only made
them compete for cores (the first Spring context took 108s per fork instead
of 35s). The api tests now run in three jobs, each taking a third of the
classes from scripts/ci/test-shard.py, and common, registry and executor in a
fourth. Each job uses 2 forks, the fastest setting measured on a 4-CPU
container. build-jammy waits for them, merges their JaCoCo data and runs the
Sonar analysis without running tests again.

Test output goes to report files that are uploaded with the coverage, so the
job logs keep only the build, the test summaries and the failures.
Local storage keys files by job id (the job context lives in
~/.terraform-spring-boot/local/output/context/<jobId>/context.json), and
each fork's in-memory H2 database numbers jobs from 1. Two forks therefore
wrote the same files: in CI StructuredOutputSpoolRecoveryIntegrationTest read
a context that ContextPatchIntegrationTest had written in the other fork.
Each fork now gets user.home under target/test-home/fork-N. This also keeps
api tests out of the developer's real home directory.
It created references as TERRAKUBE_DEVELOPERS and passed only because
CollectionTests had already given that team manageCollection earlier in the
same JVM. In a test shard without CollectionTests every request got 403.
With three shards the slowest test job took 3:54 and bounded the run; about
half of each job is setup and compilation, so a fourth shard trims the test
part further.
setup-java's maven cache has one key per pom hash, and only the first job to
finish can save it. After the last pom change that was the common, registry,
executor job, so the cache held none of the api dependencies: each api shard
downloaded about 2000 artifacts and build-jammy 576, and no later run could
fix the entry. The api shards, the other-modules job and build-jammy now each
cache under their own key, with restore-keys so a pom change starts from the
previous cache instead of an empty one.
The agent instrumented every class the test JVMs loaded, including the
thousands from Spring, Hibernate and Elide, while the coverage report only
covers io.terrakube classes. Instrumenting just those cuts the first Spring
context start in a test JVM from 14.1s to 10.4s locally, the same as running
without the agent. Each CI shard pays that start once per fork.
@Macbet
Macbet force-pushed the build/speed-up-backend-build branch from 3843e77 to e46485f Compare November 29, 2026 19:34
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ci Touches CI, containers or dev environment area/scheduler Touches job scheduling, queueing or locking author/contributor Authored by a returning contributor dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation size/L 250-999 lines changed terrakube-api terrakube-executor terrakube-registry type/chore Tests, CI, build or maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants