Add gitea-actions: Spring Boot 4.2.0 CI with surefire/failsafe, Testcontainers, CycloneDX SBOM and Jib
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b69H5rTewUKPyhGS8r
This commit is contained in:
@@ -8,5 +8,6 @@ Runnable companion projects for build, release and DevOps posts on [ankurm.com](
|
||||
| [vuln-scan](vuln-scan/) | Dependency Vulnerability Scanning for Java: OWASP Dependency-Check vs Trivy vs OSV-Scanner | The same project scanned by Dependency-Check 13.0.0, Trivy 0.76.2 and OSV-Scanner 2.7.1: false positives, suppression with expiry, CI gating, SBOM-based scanning |
|
||||
| [jbang](jbang/) | JBang: Run Java Like a Script in 2026 (Dependencies, Templates, Spring Boot and Native Images) | JBang 0.132.1 on JDK 25: `//DEPS`, compact source files, `//SOURCES`, a single-file Spring Boot 4.2 app, templates and aliases, remote scripts and `jbang trust`, `--native` with GraalVM |
|
||||
| [jib](jib/) | Jib for Spring Boot: Container Images Without a Dockerfile (vs Buildpacks) | Spring Boot 4.2.0 built into an image with Jib 3.5.2 and with Buildpacks: layers, reproducible digests, registry push, failure modes, memory behaviour |
|
||||
| [gitea-actions](gitea-actions/) | Gitea Actions CI for Spring Boot: Build, Test, SBOM and Image Push | Spring Boot 4.2.0 pipeline: surefire/failsafe split, Testcontainers, Maven cache, CycloneDX SBOM, Jib push, and what breaks when Gitea runs under a URL sub-path |
|
||||
|
||||
MIT licensed.
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
# Gitea Actions pipeline for the gitea-actions-demo service.
|
||||
#
|
||||
# IMPORTANT: Gitea only looks for workflows in <repository root>/.gitea/workflows/.
|
||||
# This file sits inside a module of a shared repository, so Gitea will NOT run it here.
|
||||
# Copy it to the root of your own project (and drop the working-directory lines).
|
||||
# It was NOT executed by a Gitea runner while writing the post; every command in it was
|
||||
# executed by scripts/*.sh instead. See the post for what that does and does not prove.
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
tags: ['v*']
|
||||
pull_request:
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ gitea.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
defaults:
|
||||
run:
|
||||
working-directory: gitea-actions
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: '25'
|
||||
cache: maven # caches ~/.m2/repository, keyed on pom.xml
|
||||
|
||||
# Unit tests (surefire), jar, SBOM, then integration tests (failsafe + Testcontainers).
|
||||
- name: Build, test, SBOM
|
||||
run: mvn -B -ntp verify
|
||||
|
||||
# GITEA_TOKEN cannot publish packages, so a personal access token with
|
||||
# write:package lives in a repository secret named PACKAGES_TOKEN.
|
||||
- name: Publish SBOM to the generic package registry
|
||||
if: gitea.event_name == 'push'
|
||||
env:
|
||||
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN }}
|
||||
run: |
|
||||
VERSION="$(mvn -q -ntp help:evaluate -Dexpression=project.version -DforceStdout)"
|
||||
curl --fail --user "${{ gitea.repository_owner }}:${PACKAGES_TOKEN}" \
|
||||
--upload-file target/classes/META-INF/sbom/application.cdx.json \
|
||||
"${{ gitea.server_url }}/api/packages/${{ gitea.repository_owner }}/generic/gitea-actions-demo-sbom/${VERSION}-${{ gitea.run_number }}/application.cdx.json"
|
||||
|
||||
image:
|
||||
needs: build
|
||||
if: gitea.event_name == 'push' && startsWith(gitea.ref, 'refs/tags/v')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: '25'
|
||||
cache: maven
|
||||
|
||||
# Jib builds and pushes the image straight to the registry: no Docker daemon needed.
|
||||
# vars.IMAGE_NAME is e.g. registry.example.com/owner/gitea-actions-demo
|
||||
- name: Build and push image with Jib
|
||||
env:
|
||||
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
|
||||
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
run: >
|
||||
mvn -B -ntp -DskipTests jib:build
|
||||
-Dimage.name=${{ vars.IMAGE_NAME }}
|
||||
-Djib.to.auth.username="$REGISTRY_USER"
|
||||
-Djib.to.auth.password="$REGISTRY_PASSWORD"
|
||||
@@ -0,0 +1 @@
|
||||
target/
|
||||
@@ -0,0 +1,52 @@
|
||||
# gitea-actions
|
||||
|
||||
Companion module for the ankurm.com post **Gitea Actions CI for Spring Boot: Build, Test, SBOM and Image Push**.
|
||||
All the explanation lives in the post; this module holds the code and the captured output the post quotes.
|
||||
|
||||
| Piece | Version |
|
||||
|---|---|
|
||||
| Spring Boot | 4.2.0 |
|
||||
| JDK | Temurin 25.0.5.1+2 |
|
||||
| Maven | 3.9.11 |
|
||||
| Jib | 3.5.2 |
|
||||
| cyclonedx-maven-plugin | 2.9.3 (managed by the Spring Boot parent) |
|
||||
| Testcontainers PostgreSQL | `postgres:17-alpine` |
|
||||
| Gitea server checked | 1.27.3 |
|
||||
|
||||
## Quickstart
|
||||
|
||||
```bash
|
||||
export JAVA_HOME=/path/to/jdk-25 # a JDK 25
|
||||
mvn -B -ntp verify # unit tests, jar, SBOM, integration tests (needs Docker)
|
||||
mvn -B -ntp -DskipTests -Plate-sbom package # the SBOM mistake the post explains
|
||||
./scripts/run-all.sh # regenerates output/ (06 needs GITEA_TOKEN)
|
||||
```
|
||||
|
||||
## What is here
|
||||
|
||||
| Path | What it is |
|
||||
|---|---|
|
||||
| `src/main`, `src/test` | A tiny ticket service: `PriorityRulesTest` (unit, surefire), `TicketApiIT` (PostgreSQL via Testcontainers, failsafe) |
|
||||
| `.gitea/workflows/ci.yaml` | The pipeline. Inert here (Gitea reads only the repository root). Copy it to the root of your project |
|
||||
| `scripts/` | One script per scenario the post shows |
|
||||
| `output/` | Real captured output of those scripts |
|
||||
|
||||
## Output index
|
||||
|
||||
| File | Shows |
|
||||
|---|---|
|
||||
| `output/01-versions.txt` | Toolchain and pinned versions |
|
||||
| `output/02-cold-vs-warm-cache.txt` | Maven downloads with an empty and a populated local repository |
|
||||
| `output/03-build-test.txt` | The pipeline's build command, then the same command with Docker unreachable |
|
||||
| `output/04-sbom.txt` | SBOM contents, and the `-Plate-sbom` mistake |
|
||||
| `output/05-image-local-registry.txt` | Jib push to a throwaway `registry:2` and read-back |
|
||||
| `output/06-gitea-registries.txt` | Docker push to Gitea under a URL sub-path (fails), generic package upload (works) |
|
||||
| `output/07-workflow-lint.txt` | Static checks of the workflow |
|
||||
|
||||
## Honest limits
|
||||
|
||||
- The workflow file was **not** executed by a Gitea runner. Each command in it was executed by the scripts above, in the same order, on a machine with Docker. That proves the commands, not Gitea's handling of the YAML.
|
||||
- `06-gitea-registries.sh` needs a personal access token in `GITEA_TOKEN`. The token is never written to a file.
|
||||
- Timings in `02` depend on the network; the download counts do not.
|
||||
|
||||
MIT licensed.
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
=== Toolchain ===
|
||||
openjdk version "25.0.5.1" 2026-11-17 LTS
|
||||
OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS)
|
||||
OpenJDK 64-Bit Server VM Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS, mixed mode, sharing)
|
||||
Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
|
||||
Java version: 25.0.5.1, vendor: Eclipse Adoptium, runtime: /tmp/tools/jdk-25.0.5.1+2
|
||||
Docker engine 29.9.3
|
||||
|
||||
=== Versions pinned by the module ===
|
||||
<version>4.2.0</version>
|
||||
<java.version>25</java.version>
|
||||
<jib.version>3.5.2</jib.version>
|
||||
|
||||
=== Images used ===
|
||||
postgres:17-alpine
|
||||
eclipse-temurin:25-jre
|
||||
|
||||
=== Gitea server this was checked against ===
|
||||
{"version":"1.27.3"}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
=== mvn verify -DskipITs, local repository = /tmp/m2-cold-demo ===
|
||||
cold (empty local repository) downloads: 599 seconds: 44
|
||||
warm (repository restored) downloads: 0 seconds: 11
|
||||
local repository size after the run: 110M
|
||||
(seconds depend on the network; the download counts are the point)
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
=== mvn -B -ntp verify (key lines) ===
|
||||
[INFO] --- clean:3.5.0:clean (default-clean) ---
|
||||
[INFO] --- cyclonedx:2.9.3:makeAggregateBom (default) ---
|
||||
[INFO] --- resources:3.5.0:resources (default-resources) ---
|
||||
[INFO] --- compiler:3.16.0:compile (default-compile) ---
|
||||
[INFO] --- resources:3.5.0:testResources (default-testResources) ---
|
||||
[INFO] --- compiler:3.16.0:testCompile (default-testCompile) ---
|
||||
[INFO] --- surefire:3.6.1:test (default-test) ---
|
||||
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 -- in <class>
|
||||
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
|
||||
[INFO] --- jar:3.5.1:jar (default-jar) ---
|
||||
[INFO] --- spring-boot:4.2.0:repackage (repackage) ---
|
||||
[INFO] Replacing main artifact /tmp/dd/gitea-actions/target/gitea-actions-demo-1.0.0.jar with repackaged archive, adding nested dependencies in BOOT-INF/.
|
||||
[INFO] --- failsafe:3.6.1:integration-test (default) ---
|
||||
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 -- in <class>
|
||||
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
|
||||
[INFO] --- failsafe:3.6.1:verify (default) ---
|
||||
[INFO] BUILD SUCCESS
|
||||
|
||||
=== target/ after the build ===
|
||||
gitea-actions-demo-1.0.0.jar
|
||||
gitea-actions-demo-1.0.0.jar.original
|
||||
|
||||
=== Same command where Docker is really unreachable (docker.sock masked in a private mount namespace) ===
|
||||
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 -- in <class>
|
||||
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
|
||||
23:57:14.999 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy -- Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
|
||||
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 <<< FAILURE! -- in <class>
|
||||
java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
|
||||
[ERROR] TicketApiIT.initializationError ? IllegalState Could not find a valid Docker environment. Please see logs and check configuration
|
||||
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
|
||||
[INFO] BUILD FAILURE
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
=== mvn -DskipTests package (default: Spring Boot parent wiring) ===
|
||||
[INFO] --- cyclonedx:2.9.3:makeAggregateBom (default) ---
|
||||
[INFO] CycloneDX: Creating BOM version 1.6 with 59 component(s)
|
||||
[INFO] CycloneDX: Writing and validating BOM (JSON): target/classes/META-INF/sbom/application.cdx.json
|
||||
[INFO] --- jar:3.5.1:jar (default-jar) ---
|
||||
[INFO] --- spring-boot:4.2.0:repackage (repackage) ---
|
||||
jar contains: META-INF/sbom/application.cdx.json
|
||||
|
||||
=== Inside target/classes/META-INF/sbom/application.cdx.json ===
|
||||
bomFormat : CycloneDX
|
||||
specVersion : 1.6
|
||||
subject : com.ankurm.ci:gitea-actions-demo:1.0.0 (application)
|
||||
components : 59
|
||||
by scope : {'required': 59}
|
||||
all have purl : True
|
||||
all have hash : True
|
||||
spring-boot : pkg:maven/org.springframework.boot/[email protected]?type=jar
|
||||
postgresql : pkg:maven/org.postgresql/[email protected]?type=jar
|
||||
tomcat-embed-core : pkg:maven/org.apache.tomcat.embed/[email protected]?type=jar
|
||||
jackson-databind : pkg:maven/tools.jackson.core/[email protected]?type=jar
|
||||
testcontainers : (not in SBOM)
|
||||
dependency edges: 60
|
||||
|
||||
=== The mistake: -Plate-sbom (re-declares the execution with phase=package) ===
|
||||
[INFO] --- jar:3.5.1:jar (default-jar) ---
|
||||
[INFO] --- spring-boot:4.2.0:repackage (repackage) ---
|
||||
[INFO] --- cyclonedx:2.9.3:makeAggregateBom (default) ---
|
||||
jar contains:
|
||||
(empty above = the SBOM is not in the jar)
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
=== 1. mvn -DskipTests jib:build, base image pulled from Docker Hub (non-deterministic: depends on the rate limit) ===
|
||||
[INFO] Built and pushed image as localhost:5000/gitea-actions-demo, localhost:5000/gitea-actions-demo:1.0.0
|
||||
[INFO] BUILD SUCCESS
|
||||
|
||||
=== 2. same, base image read from the runner's local Docker daemon (-Djib.from.image=docker://...) ===
|
||||
[INFO] Container entrypoint set to [java, -cp, @/app/jib-classpath-file, com.ankurm.ci.Application]
|
||||
[INFO] Built and pushed image as localhost:5000/gitea-actions-demo, localhost:5000/gitea-actions-demo:1.0.0
|
||||
[INFO] BUILD SUCCESS
|
||||
|
||||
=== Registry catalog and tags ===
|
||||
{"repositories":["gitea-actions-demo"]}
|
||||
|
||||
{"name":"gitea-actions-demo","tags":["latest","1.0.0"]}
|
||||
|
||||
|
||||
=== Layers in the pushed manifest ===
|
||||
42166579 sha256:517b871c26ea...
|
||||
389 sha256:96aae9022e0e...
|
||||
18461849 sha256:5050cb6f0906...
|
||||
63112449 sha256:dbda3fb9544c...
|
||||
32 sha256:4f4fb700ef54...
|
||||
2464 sha256:fa06fe8ddd50...
|
||||
22267190 sha256:5c4c7fb44e21...
|
||||
33240 sha256:79d4cb011a48...
|
||||
3760 sha256:97981627ec22...
|
||||
824 sha256:11c50fd61d93...
|
||||
layers: 10
|
||||
|
||||
=== Image config (user, ports, entrypoint) ===
|
||||
user=1000 ports=map[8080/tcp:{}] entrypoint=[java -cp @/app/jib-classpath-file com.ankurm.ci.Application]
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
=== 1. Where does the container registry answer? ===
|
||||
https://ankurm.com/v2/ -> HTTP 404
|
||||
https://ankurm.com/git.app/v2/ -> HTTP 200
|
||||
|
||||
=== 2. docker login and docker push the way every tutorial writes it ===
|
||||
$ docker login ankurm.com
|
||||
Error response from daemon: login attempt to https://ankurm.com/v2/ failed with status: 404 Not Found
|
||||
$ docker push ankurm.com/asmhatre/gitea-actions-probe:1
|
||||
unknown: unexpected status from POST request to https://ankurm.com/v2/asmhatre/gitea-actions-probe/blobs/uploads/: 404 Not Found
|
||||
|
||||
=== 3. The generic package registry works under the sub-path: upload the SBOM ===
|
||||
$ curl --upload-file target/classes/META-INF/sbom/application.cdx.json https://ankurm.com/git.app/api/packages/asmhatre/generic/gitea-actions-demo-sbom/1.0.0-demo/application.cdx.json
|
||||
PUT first time -> HTTP 201
|
||||
PUT second time -> HTTP 409 (a file name can be published once per version)
|
||||
downloaded back: CycloneDX 1.6 59 components
|
||||
DELETE version -> HTTP 204
|
||||
|
||||
=== 4. Bad file name ===
|
||||
PUT file with a space in its name -> HTTP 201
|
||||
DELETE version (cleanup) -> HTTP 204
|
||||
packages left named gitea-actions-demo-sbom: 0
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
=== Parse .gitea/workflows/ci.yaml ===
|
||||
name : ci
|
||||
triggers : ['push', 'pull_request']
|
||||
job build : runs-on=ubuntu-latest needs=- steps=4
|
||||
- actions/checkout@v4
|
||||
- actions/setup-java@v4
|
||||
- Build, test, SBOM
|
||||
- Publish SBOM to the generic package registry
|
||||
job image : runs-on=ubuntu-latest needs=build steps=3
|
||||
- actions/checkout@v4
|
||||
- actions/setup-java@v4
|
||||
- Build and push image with Jib
|
||||
|
||||
=== Secrets are only ever referenced through env:, never inside a run: line ===
|
||||
ok: no secret interpolated into a shell command
|
||||
|
||||
=== Secret names obey Gitea's rules (no GITHUB_/GITEA_ prefix) ===
|
||||
secrets.PACKAGES_TOKEN
|
||||
secrets.REGISTRY_PASSWORD
|
||||
secrets.REGISTRY_USER
|
||||
@@ -0,0 +1,151 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>4.2.0</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>com.ankurm.ci</groupId>
|
||||
<artifactId>gitea-actions-demo</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>gitea-actions-demo: a small Spring Boot service with a CI pipeline</name>
|
||||
|
||||
<properties>
|
||||
<java.version>25</java.version>
|
||||
<jib.version>3.5.2</jib.version>
|
||||
<!-- Where `mvn jib:build` pushes. Override with -Dimage.name=registry.example.com/owner/gitea-actions-demo -->
|
||||
<image.name>localhost:5000/gitea-actions-demo</image.name>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-testcontainers</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers-postgresql</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers-junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<!-- *Test = unit tests (surefire, no Docker). *IT = integration tests (failsafe, needs Docker). -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- SBOM. spring-boot-starter-parent already binds makeAggregateBom to generate-resources and writes
|
||||
target/classes/META-INF/sbom/application.cdx.json, so it ends up INSIDE the jar and the actuator
|
||||
can serve it. Declaring the plugin is all that is needed; the version is managed by Spring Boot. -->
|
||||
<plugin>
|
||||
<groupId>org.cyclonedx</groupId>
|
||||
<artifactId>cyclonedx-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<!-- Image without a Dockerfile and without a Docker daemon in the build step. -->
|
||||
<plugin>
|
||||
<groupId>com.google.cloud.tools</groupId>
|
||||
<artifactId>jib-maven-plugin</artifactId>
|
||||
<version>${jib.version}</version>
|
||||
<configuration>
|
||||
<from>
|
||||
<image>eclipse-temurin:25-jre</image>
|
||||
</from>
|
||||
<to>
|
||||
<image>${image.name}</image>
|
||||
<tags>
|
||||
<tag>${project.version}</tag>
|
||||
</tags>
|
||||
</to>
|
||||
<container>
|
||||
<ports>
|
||||
<port>8080</port>
|
||||
</ports>
|
||||
<user>1000</user>
|
||||
<labels>
|
||||
<org.opencontainers.image.title>gitea-actions-demo</org.opencontainers.image.title>
|
||||
<org.opencontainers.image.source>https://ankurm.com/git.app/asmhatre/java-devops-demo</org.opencontainers.image.source>
|
||||
</labels>
|
||||
</container>
|
||||
<allowInsecureRegistries>true</allowInsecureRegistries>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<!-- The mistake from the first draft of this module, kept so the post can show its effect:
|
||||
re-declaring the parent's execution (same id "default") with phase=package moves SBOM generation
|
||||
AFTER spring-boot:repackage, so the jar no longer contains the SBOM. -->
|
||||
<profile>
|
||||
<id>late-sbom</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.cyclonedx</groupId>
|
||||
<artifactId>cyclonedx-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>makeAggregateBom</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
{
|
||||
banner "Toolchain"
|
||||
java -version 2>&1 | grep -v '^Picked up'
|
||||
"${MVN[@]}" -v 2>&1 | grep -E '^Apache Maven|^Java version' || true
|
||||
echo "Docker engine $(docker version --format '{{.Server.Version}}')"
|
||||
banner "Versions pinned by the module"
|
||||
grep -E '<version>4\.2\.0|<java.version>|<jib.version>|<cyclonedx.version>' pom.xml | sed 's/^ *//'
|
||||
banner "Images used"
|
||||
grep -o 'postgres:[0-9a-z.-]*' src/test/java/com/ankurm/ci/TicketApiIT.java
|
||||
grep -o 'eclipse-temurin:[0-9a-z.-]*' pom.xml
|
||||
banner "Gitea server this was checked against"
|
||||
curl -s https://ankurm.com/git.app/api/v1/version; echo
|
||||
} 2>&1 | tee "$OUT/01-versions.txt"
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
# What a Maven cache is worth: the same build against an empty and a populated local repository.
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
REPO="${COLD_REPO:-/tmp/m2-cold-demo}"
|
||||
rm -rf "$REPO"
|
||||
run() { # label
|
||||
local start end n
|
||||
start=$(date +%s)
|
||||
mvn -B -Dmaven.repo.local="$REPO" -DskipITs verify > /tmp/cache-run.log 2>&1 || { tail -20 /tmp/cache-run.log; exit 1; }
|
||||
end=$(date +%s)
|
||||
n=$(grep -c 'Downloaded from' /tmp/cache-run.log || true)
|
||||
printf '%-34s downloads: %-5s seconds: %s\n' "$1" "$n" "$((end-start))"
|
||||
}
|
||||
{
|
||||
banner "mvn verify -DskipITs, local repository = $REPO"
|
||||
run "cold (empty local repository)"
|
||||
run "warm (repository restored)"
|
||||
printf 'local repository size after the run: %s\n' "$(du -sh "$REPO" | cut -f1)"
|
||||
echo "(seconds depend on the network; the download counts are the point)"
|
||||
} 2>&1 | tee "$OUT/02-cold-vs-warm-cache.txt"
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# The command the pipeline runs, and what it produces. Then the same command with no Docker.
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
{
|
||||
banner "mvn -B -ntp verify (key lines)"
|
||||
"${MVN[@]}" clean verify 2>&1 | clean | grep -E '^\[INFO\] --- |Tests run:|BUILD|cyclonedx.*attaching|Replacing main' | sed -E 's/ @ gitea-actions-demo//; s/ in com.ankurm.ci.*/ in <class>/; s/, Time elapsed: [0-9.]+ s//'
|
||||
banner "target/ after the build"
|
||||
ls target | grep -E '\.(jar|original)$' | sort
|
||||
# Why the socket is masked instead of just setting DOCKER_HOST to a bad value: Testcontainers tries the
|
||||
# DOCKER_HOST strategy first and then falls back to the default /var/run/docker.sock. A first draft of
|
||||
# this script only set DOCKER_HOST=unix:///nonexistent.sock and the build still passed.
|
||||
banner "Same command where Docker is really unreachable (docker.sock masked in a private mount namespace)"
|
||||
unshare -m bash -c 'mount --bind /dev/null /var/run/docker.sock && DOCKER_HOST=unix:///var/run/docker.sock mvn -B -ntp verify' 2>&1 | clean \
|
||||
| grep -E 'Tests run:|Could not find a valid Docker|BUILD|ERROR\] +(TicketApiIT|Tests run)' | sed -E 's/, Time elapsed: [0-9.]+ s//; s/ in com.ankurm.ci.*/ in <class>/' | cut -c1-200 | awk '!seen[$0]++'
|
||||
} 2>&1 | tee "$OUT/03-build-test.txt"
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
# Look inside the CycloneDX SBOM the build produced, and show the one-line mistake that drops it from the jar.
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
SBOM=target/classes/META-INF/sbom/application.cdx.json
|
||||
JAR=target/gitea-actions-demo-1.0.0.jar
|
||||
{
|
||||
banner "mvn -DskipTests package (default: Spring Boot parent wiring)"
|
||||
"${MVN[@]}" -DskipTests clean package 2>&1 | clean | grep -E '^\[INFO\] --- (spring-boot|cyclonedx|jar)|CycloneDX: (Creating|Writing)' | sed -E 's/ @ gitea-actions-demo//; s#/.*/target#target#'
|
||||
echo "jar contains: $(unzip -l $JAR | awk '/sbom\/application/ {print $4}')"
|
||||
banner "Inside $SBOM"
|
||||
python3 - <<'PY'
|
||||
import json, collections
|
||||
b = json.load(open("target/classes/META-INF/sbom/application.cdx.json"))
|
||||
print("bomFormat :", b["bomFormat"])
|
||||
print("specVersion :", b["specVersion"])
|
||||
m = b["metadata"]["component"]
|
||||
print("subject :", m["group"] + ":" + m["name"] + ":" + m["version"], "(" + m["type"] + ")")
|
||||
c = b["components"]
|
||||
print("components :", len(c))
|
||||
print("by scope :", dict(collections.Counter(x.get("scope", "-") for x in c)))
|
||||
print("all have purl :", all("purl" in x for x in c))
|
||||
print("all have hash :", all(x.get("hashes") for x in c))
|
||||
for want in ("spring-boot", "postgresql", "tomcat-embed-core", "jackson-databind", "testcontainers"):
|
||||
hit = [x for x in c if x["name"] == want]
|
||||
print(f"{want:18s}:", hit[0]["purl"] if hit else "(not in SBOM)")
|
||||
print("dependency edges:", len(b.get("dependencies", [])))
|
||||
PY
|
||||
banner "The mistake: -Plate-sbom (re-declares the execution with phase=package)"
|
||||
"${MVN[@]}" -DskipTests -Plate-sbom clean package 2>&1 | clean | grep -E '^\[INFO\] --- (spring-boot|cyclonedx|jar)' | sed -E 's/ @ gitea-actions-demo//'
|
||||
echo "jar contains: $(unzip -l $JAR | awk '/sbom\/application/ {print $4}' | head -1)"
|
||||
echo "(empty above = the SBOM is not in the jar)"
|
||||
"${MVN[@]}" -q -DskipTests clean package >/dev/null 2>&1
|
||||
} 2>&1 | tee "$OUT/04-sbom.txt"
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build and push the image with Jib to a throwaway registry:2 container, then read it back.
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
docker rm -f ci-demo-registry >/dev/null 2>&1 || true
|
||||
docker run -d --name ci-demo-registry -p 5000:5000 registry:2 >/dev/null
|
||||
for i in $(seq 1 30); do curl -sf localhost:5000/v2/ >/dev/null && break; sleep 1; done
|
||||
{
|
||||
banner "1. mvn -DskipTests jib:build, base image pulled from Docker Hub (non-deterministic: depends on the rate limit)"
|
||||
"${MVN[@]}" -DskipTests jib:build 2>&1 | clean | grep -E 'Built and pushed|BUILD|ERROR\] (Failed|GET|\{)' | cut -c1-210 || true
|
||||
banner "2. same, base image read from the runner's local Docker daemon (-Djib.from.image=docker://...)"
|
||||
"${MVN[@]}" -DskipTests jib:build -Djib.from.image=docker://eclipse-temurin:25-jre 2>&1 | clean | grep -E 'Built and pushed|Container entrypoint|BUILD|ERROR' | cut -c1-200
|
||||
banner "Registry catalog and tags"
|
||||
curl -s localhost:5000/v2/_catalog; echo
|
||||
curl -s localhost:5000/v2/gitea-actions-demo/tags/list; echo
|
||||
banner "Layers in the pushed manifest"
|
||||
curl -s -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' localhost:5000/v2/gitea-actions-demo/manifests/1.0.0 \
|
||||
| python3 -c "import sys,json; m=json.load(sys.stdin); [print(f'{l[\"size\"]:>10} {l[\"digest\"][:19]}...') for l in m['layers']]; print('layers:', len(m['layers']))"
|
||||
banner "Image config (user, ports, entrypoint)"
|
||||
docker pull -q localhost:5000/gitea-actions-demo:1.0.0 >/dev/null
|
||||
docker inspect localhost:5000/gitea-actions-demo:1.0.0 --format 'user={{.Config.User}} ports={{.Config.ExposedPorts}} entrypoint={{.Config.Entrypoint}}'
|
||||
} 2>&1 | tee "$OUT/05-image-local-registry.txt"
|
||||
docker rm -f ci-demo-registry >/dev/null 2>&1 || true
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
# What actually happens when the pipeline pushes to a Gitea that lives under a URL sub-path.
|
||||
# Needs GITEA_TOKEN (a personal access token with write:package). Never printed, never committed.
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
: "${GITEA_TOKEN:?export GITEA_TOKEN first}"
|
||||
BASE="${GITEA_BASE:-https://ankurm.com/git.app}"; HOST="${GITEA_HOST:-ankurm.com}"; OWNER="${GITEA_OWNER:-asmhatre}"
|
||||
VER="1.0.0-demo"
|
||||
{
|
||||
banner "1. Where does the container registry answer?"
|
||||
printf '%s/v2/ -> HTTP %s\n' "https://$HOST" "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" "https://$HOST/v2/")"
|
||||
printf '%s/v2/ -> HTTP %s\n' "$BASE" "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" "$BASE/v2/")"
|
||||
banner "2. docker login and docker push the way every tutorial writes it"
|
||||
echo "\$ docker login $HOST"
|
||||
echo "$GITEA_TOKEN" | docker login "$HOST" -u "$OWNER" --password-stdin 2>&1 | grep -v '^$' || true
|
||||
docker tag postgres:17-alpine "$HOST/$OWNER/gitea-actions-probe:1"
|
||||
echo "\$ docker push $HOST/$OWNER/gitea-actions-probe:1"
|
||||
docker push "$HOST/$OWNER/gitea-actions-probe:1" 2>&1 | tail -1 || true
|
||||
docker rmi "$HOST/$OWNER/gitea-actions-probe:1" >/dev/null 2>&1 || true
|
||||
banner "3. The generic package registry works under the sub-path: upload the SBOM"
|
||||
URL="$BASE/api/packages/$OWNER/generic/gitea-actions-demo-sbom/$VER/application.cdx.json"
|
||||
echo "\$ curl --upload-file target/classes/META-INF/sbom/application.cdx.json $BASE/api/packages/$OWNER/generic/gitea-actions-demo-sbom/$VER/application.cdx.json"
|
||||
printf 'PUT first time -> HTTP %s\n' "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" --upload-file target/classes/META-INF/sbom/application.cdx.json "$URL")"
|
||||
printf 'PUT second time -> HTTP %s (a file name can be published once per version)\n' "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" --upload-file target/classes/META-INF/sbom/application.cdx.json "$URL")"
|
||||
curl -s -u "$OWNER:$GITEA_TOKEN" "$URL" | python3 -c "import sys,json; b=json.load(sys.stdin); print('downloaded back:', b['bomFormat'], b['specVersion'], len(b['components']), 'components')"
|
||||
printf 'DELETE version -> HTTP %s\n' "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" -X DELETE "$BASE/api/packages/$OWNER/generic/gitea-actions-demo-sbom/$VER")"
|
||||
banner "4. Bad file name"
|
||||
printf 'PUT file with a space in its name -> HTTP %s\n' "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" --upload-file target/classes/META-INF/sbom/application.cdx.json "$BASE/api/packages/$OWNER/generic/gitea-actions-demo-sbom/$VER/application%20x.cdx.json")"
|
||||
printf 'DELETE version (cleanup) -> HTTP %s\n' "$(curl -s -o /dev/null -w '%{http_code}' -u "$OWNER:$GITEA_TOKEN" -X DELETE "$BASE/api/packages/$OWNER/generic/gitea-actions-demo-sbom/$VER")"
|
||||
printf 'packages left named gitea-actions-demo-sbom: %s\n' "$(curl -s -u "$OWNER:$GITEA_TOKEN" "$BASE/api/v1/packages/$OWNER?q=gitea-actions-demo-sbom" | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))')"
|
||||
} 2>&1 | sed "s/$GITEA_TOKEN/***/g" | tee "$OUT/06-gitea-registries.txt"
|
||||
docker logout "$HOST" >/dev/null 2>&1 || true
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
# Static checks on the workflow. This is NOT a runner: it proves the YAML parses and that every
|
||||
# command the workflow runs is one the other scripts executed. It cannot prove Gitea accepts it.
|
||||
source "$(dirname "$0")/lib.sh"
|
||||
{
|
||||
banner "Parse .gitea/workflows/ci.yaml"
|
||||
python3 - <<'PY'
|
||||
import yaml
|
||||
w = yaml.safe_load(open(".gitea/workflows/ci.yaml"))
|
||||
print("name :", w["name"])
|
||||
print("triggers :", list(w[True].keys()) if True in w else list(w["on"].keys()))
|
||||
for j, d in w["jobs"].items():
|
||||
print(f"job {j:6s}: runs-on={d['runs-on']} needs={d.get('needs','-')} steps={len(d['steps'])}")
|
||||
for s in d["steps"]:
|
||||
print(" -", s.get("uses") or s.get("name") or "run")
|
||||
PY
|
||||
banner "Secrets are only ever referenced through env:, never inside a run: line"
|
||||
if grep -nE '^\s+(run|-).*\$\{\{ *secrets\.' .gitea/workflows/ci.yaml | grep -v 'env:' | grep -E 'run:|mvn|curl'; then echo "FOUND inline secret"; else echo "ok: no secret interpolated into a shell command"; fi
|
||||
banner "Secret names obey Gitea's rules (no GITHUB_/GITEA_ prefix)"
|
||||
grep -oE 'secrets\.[A-Za-z0-9_]+' .gitea/workflows/ci.yaml | sort -u
|
||||
} 2>&1 | tee "$OUT/07-workflow-lint.txt"
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
# Shared setup for the scenario scripts. Source it; do not run it.
|
||||
set -euo pipefail
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
MODULE="$(cd "$HERE/.." && pwd)"
|
||||
OUT="$MODULE/output"
|
||||
mkdir -p "$OUT"
|
||||
cd "$MODULE"
|
||||
# The post was tested with Temurin 25.0.5.1+2. Set JAVA_HOME to use another JDK 25.
|
||||
export PATH="${JAVA_HOME:+$JAVA_HOME/bin:}$PATH"
|
||||
MVN=(mvn -B -ntp)
|
||||
# Strip JVM-launcher noise and wall-clock lines so reruns diff cleanly.
|
||||
clean() { grep -v -E '^Picked up|^WARNING:|Finished at:|^\[INFO\] *$|^\[INFO\] Total time' || true; }
|
||||
banner() { printf '\n=== %s ===\n' "$*"; }
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regenerates every file in output/. 06 needs GITEA_TOKEN; it is skipped when unset.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
./01-versions.sh >/dev/null
|
||||
./02-cold-vs-warm-cache.sh >/dev/null
|
||||
./03-build-test.sh >/dev/null
|
||||
./04-sbom.sh >/dev/null
|
||||
./05-image-local-registry.sh >/dev/null
|
||||
if [ -n "${GITEA_TOKEN:-}" ]; then ./06-gitea-registries.sh >/dev/null; else echo "skipping 06 (no GITEA_TOKEN)"; fi
|
||||
./07-workflow-lint.sh >/dev/null
|
||||
ls -1 ../output
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/** Entry point. The service itself is deliberately tiny: the pipeline is the subject, not the app. */
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
/**
|
||||
* Pure logic with no framework and no database: the kind of code a fast, Docker-free
|
||||
* unit test (surefire) should cover.
|
||||
*/
|
||||
public final class PriorityRules {
|
||||
|
||||
private PriorityRules() {
|
||||
}
|
||||
|
||||
public static String priorityFor(String title) {
|
||||
String t = title == null ? "" : title.toLowerCase();
|
||||
if (t.contains("outage") || t.contains("down") || t.contains("data loss")) {
|
||||
return "HIGH";
|
||||
}
|
||||
if (t.contains("error") || t.contains("slow")) {
|
||||
return "MEDIUM";
|
||||
}
|
||||
return "LOW";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
/** A support ticket. {@code priority} is derived, see {@link PriorityRules}. */
|
||||
public record Ticket(Long id, String title, String priority) {
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class TicketController {
|
||||
|
||||
public record NewTicket(String title) {
|
||||
}
|
||||
|
||||
private final TicketRepository repository;
|
||||
|
||||
public TicketController(TicketRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@GetMapping("/tickets")
|
||||
public List<Ticket> list() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/tickets")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Ticket create(@RequestBody NewTicket body) {
|
||||
return repository.save(body.title());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.jdbc.core.simple.JdbcClient;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/** Talks to PostgreSQL. Its test needs a real database, so it runs in failsafe with Testcontainers. */
|
||||
@Repository
|
||||
public class TicketRepository {
|
||||
|
||||
private final JdbcClient jdbc;
|
||||
|
||||
public TicketRepository(JdbcClient jdbc) {
|
||||
this.jdbc = jdbc;
|
||||
}
|
||||
|
||||
public Ticket save(String title) {
|
||||
String priority = PriorityRules.priorityFor(title);
|
||||
Long id = jdbc.sql("insert into ticket(title, priority) values (:t, :p) returning id")
|
||||
.param("t", title).param("p", priority)
|
||||
.query(Long.class).single();
|
||||
return new Ticket(id, title, priority);
|
||||
}
|
||||
|
||||
public List<Ticket> findAll() {
|
||||
return jdbc.sql("select id, title, priority from ticket order by id")
|
||||
.query((rs, n) -> new Ticket(rs.getLong("id"), rs.getString("title"), rs.getString("priority")))
|
||||
.list();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.application.name=gitea-actions-demo
|
||||
spring.sql.init.mode=always
|
||||
management.endpoints.web.exposure.include=health,info,sbom
|
||||
@@ -0,0 +1,5 @@
|
||||
create table if not exists ticket (
|
||||
id bigserial primary key,
|
||||
title text not null,
|
||||
priority text not null
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit test: runs in surefire, needs no Docker, finishes in milliseconds. */
|
||||
class PriorityRulesTest {
|
||||
|
||||
@Test
|
||||
void outageIsHigh() {
|
||||
assertThat(PriorityRules.priorityFor("Payment outage in Mumbai")).isEqualTo("HIGH");
|
||||
}
|
||||
|
||||
@Test
|
||||
void slowIsMedium() {
|
||||
assertThat(PriorityRules.priorityFor("Dashboard is slow")).isEqualTo("MEDIUM");
|
||||
}
|
||||
|
||||
@Test
|
||||
void everythingElseIsLow() {
|
||||
assertThat(PriorityRules.priorityFor("Please add dark mode")).isEqualTo("LOW");
|
||||
assertThat(PriorityRules.priorityFor(null)).isEqualTo("LOW");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.ankurm.ci;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.postgresql.PostgreSQLContainer;
|
||||
|
||||
/**
|
||||
* Integration test: a real PostgreSQL in a real container. Named *IT so failsafe runs it in the
|
||||
* {@code integration-test} phase, after the unit tests and after the jar exists.
|
||||
*/
|
||||
@Testcontainers
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class TicketApiIT {
|
||||
|
||||
@Container
|
||||
@ServiceConnection
|
||||
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:17-alpine");
|
||||
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Test
|
||||
void createsAndListsTickets() {
|
||||
RestClient http = RestClient.create("http://localhost:" + port);
|
||||
|
||||
ResponseEntity<Ticket> created = http.post().uri("/tickets")
|
||||
.body(new TicketController.NewTicket("Checkout outage"))
|
||||
.retrieve().toEntity(Ticket.class);
|
||||
assertThat(created.getStatusCode().value()).isEqualTo(201);
|
||||
assertThat(created.getBody().priority()).isEqualTo("HIGH");
|
||||
|
||||
Ticket[] all = http.get().uri("/tickets").retrieve().body(Ticket[].class);
|
||||
assertThat(all).extracting(Ticket::title).contains("Checkout outage");
|
||||
}
|
||||
|
||||
/** The SBOM generated at build time is inside the jar/classpath and served by the actuator. */
|
||||
@Test
|
||||
void servesTheSbom() {
|
||||
RestClient http = RestClient.create("http://localhost:" + port);
|
||||
String bom = http.get().uri("/actuator/sbom/application").retrieve().body(String.class);
|
||||
assertThat(bom).contains("\"bomFormat\"").contains("CycloneDX").contains("spring-boot");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user