diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 2a371fb7..4367af6a 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -28,6 +28,7 @@ If you are using Copilot/AI to modify this repo, follow the **How Copilot Should - Keep the multi-stage + flattened final stage pattern (`COPY --from=builder / /`). - UBI-repo (microdnf) installs belong in the `platform` stage of the deps Dockerfiles; non-UBI content (libnsl, MarkLogic rpm, tini, scripts) goes after it. The `marklogic-platform-` image must stay an exact layer prefix of the server image (used for the Black Duck NOTICE scan), and `sudo` must never be installed in rootless images. - Keep ownership/permissions correct for rootless (`marklogic_user:users`, UID 1000). + - Home-directory init files (for example `.gdbinit`) must match other home files: mode 0740 or less permissive (CIS `file_permission_user_init_files`). - If you add/remove files, update `test/structure-test.yaml` accordingly. - **External RPM pinning**: When pinning external RPMs (e.g., libnsl from AlmaLinux), avoid broad `microdnf -y update` in the same RUN layer, as it can advance glibc and break version compatibility. Use targeted upgrades instead (e.g., `microdnf -y upgrade tzdata`). Test both the dependency image build AND downstream MarkLogic RPM installation. - **Env vars / secrets** @@ -146,7 +147,7 @@ Container Structure Tests validate: - Command availability - Environment variables -Template: `test/structure-test.yaml` (placeholders replaced during `make structure-test`) +Template: `test/structure-test.yaml` (placeholders replaced during `make structure-test`). Rootless-only checks, such as the `.gdbinit` mode, live in `test/structure-test-rootless.yaml` and are added for rootless image types. ## Clustering Patterns diff --git a/Jenkinsfile b/Jenkinsfile index 10d4a209..88dec3be 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -636,6 +636,27 @@ void scapScan() { """ } +/** + * Makes this build's server and platform images available in the local Docker daemon. + * A same-tag image can be left on an agent by an earlier build, so a local tag is reused only + * when its image ID matches the ID recorded at build time. Otherwise the archive is unstashed + * and loaded, which replaces the stale tag. + */ +void ensureBuiltImagesLoaded() { + unstash 'built-image-ids' + def idsMatch = sh(returnStatus: true, script: ''' + while read -r tag id; do + [ "$(docker image inspect --format '{{.Id}}' "$tag" 2>/dev/null)" = "$id" ] || exit 1 + done < built-image-ids.txt + ''') == 0 + if (idsMatch) { + echo 'Built images already present locally with matching IDs, skipping archive load' + } else { + unstash 'built-image-archive' + sh "docker image load -i ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}" + } +} + pipeline { // No pipeline-wide agent: stages that need a different label (graviton) must not // overlap with a persistently-held top-level agent, or two nodes of the same scarce @@ -735,11 +756,21 @@ pipeline { buildDockerImage() script { // Always save image for cases where agents might differ + def imageId = { String tag -> sh(script: "docker image inspect --format '{{.Id}}' ${tag}", returnStdout: true).trim() } + def serverIdBefore = imageId(builtImage) + def platformIdBefore = imageId(platformBuiltImage) sh """ echo "Saving ${builtImage} and ${platformBuiltImage} to ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}..." docker image save ${builtImage} ${platformBuiltImage} -o ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE} ls -lh ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE} """ + // The tags must not have moved while the archive was written, or the recorded IDs would describe a different image. + if (imageId(builtImage) != serverIdBefore || imageId(platformBuiltImage) != platformIdBefore) { + error "Image tags changed while saving ${GRAVITON3_IMAGE_ARCHIVE}; rerun the build." + } + // Record each image's ID so later stages can confirm a same-tag image on their agent is this build's. + writeFile file: 'built-image-ids.txt', text: "${builtImage} ${serverIdBefore}\n${platformBuiltImage} ${platformIdBefore}\n" + stash name: 'built-image-ids', includes: 'built-image-ids.txt', allowEmpty: false stash name: 'built-image-archive', includes: "${GRAVITON3_IMAGE_ARCHIVE}", allowEmpty: false } } @@ -781,19 +812,7 @@ pipeline { expression { return params.SCAP_SCAN } } steps { - script { - unstash 'built-image-archive' - // Load image from tar if not already available (applies to all build types) - def imageSource = "${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}" - sh """ - if ! docker image inspect ${builtImage} &>/dev/null; then - echo "Loading image from ${imageSource} for SCAP scan..." - docker image load -i ${imageSource} - else - echo "Image ${builtImage} already available locally" - fi - """ - } + ensureBuiltImagesLoaded() scapScan() stash name: 'scap-results', includes: 'scap/**', allowEmpty: true } @@ -807,18 +826,8 @@ pipeline { expression { return isArmImage() && params.GRAVITON3_AGENT } } steps { - script { - unstash 'built-image-archive' - sh """ - if ! docker image inspect ${builtImage} &>/dev/null; then - echo "Loading image from ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}..." - docker image load -i ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE} - else - echo "Image ${builtImage} already loaded (likely by SCAP-Scan stage)" - fi - docker images | head -5 - """ - } + ensureBuiltImagesLoaded() + sh 'docker images | head -5' } } @@ -834,19 +843,7 @@ pipeline { expression { return params.TEST_STRUCTURE } } steps { - script { - unstash 'built-image-archive' - // Load image from tar if not already available (applies to all build types) - def imageSource = "${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}" - sh """ - if ! docker image inspect ${builtImage} &>/dev/null; then - echo "Loading image from ${imageSource} for Structure-Tests..." - docker image load -i ${imageSource} - else - echo "Image ${builtImage} already available locally" - fi - """ - } + ensureBuiltImagesLoaded() structureTests() stash name: 'structure-test-results', includes: 'container-structure-test.xml', allowEmpty: true } @@ -864,18 +861,8 @@ pipeline { expression { return params.DOCKER_TESTS } } steps { + ensureBuiltImagesLoaded() script { - unstash 'built-image-archive' - // Load image from tar if not already available (applies to all build types) - def imageSource = "${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}" - sh """ - if ! docker image inspect ${builtImage} &>/dev/null; then - echo "Loading image from ${imageSource} for Docker-Run-Tests..." - docker image load -i ${imageSource} - else - echo "Image ${builtImage} already available locally" - fi - """ unstash 'upgrade-image-archive' // Always load from the archive: upgradeDockerImage defaults to the // mutable 'latest-' tag, which a shared agent may already @@ -908,18 +895,10 @@ pipeline { } steps { script { - unstash 'built-image-archive' - // Load image from tar if not already available (applies to all build types) - // Node's Docker daemon is shared with other concurrent builds, so only ever - // trust the exact, fully-qualified tag - never a loose repo/type match, which - // could resolve to a different build's image (e.g. a different marklogicVersion). + ensureBuiltImagesLoaded() + // Images are matched by ID, so this build's images are published even when an earlier + // build left the same tag on this node. The exact-tag inspects below confirm both are present. sh """ - if ! docker image inspect ${builtImage} &>/dev/null || ! docker image inspect ${platformBuiltImage} &>/dev/null; then - echo "Image not found locally, loading from ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE}..." - docker image load -i ${WORKSPACE}/${GRAVITON3_IMAGE_ARCHIVE} - else - echo "Images ${builtImage} and ${platformBuiltImage} already available locally" - fi docker image inspect ${builtImage} >/dev/null docker image inspect ${platformBuiltImage} >/dev/null """ diff --git a/Makefile b/Makefile index 499242e5..ce9827d6 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ endif sed -i -e 's^ML_VERSION_PLACEHOLDER^${marklogicVersion}^g' ./test/structure-test.yaml sed -i -e 's^ML_DOCKER_VERSION_PLACEHOLDER^${dockerVersion}^g' ./test/structure-test.yaml sed -i -e 's^BRANCH_PLACEHOLDER^${build_branch}^g' ./test/structure-test.yaml - container-structure-test test --config ./test/structure-test.yaml --image ${current_image} \ + container-structure-test test --config ./test/structure-test.yaml $(if $(findstring rootless,$(docker_image_type)),--config ./test/structure-test-rootless.yaml) --image ${current_image} \ $(if $(Jenkins), --output junit | tee container-structure-test.xml,) #*************************************************************************** diff --git a/dockerFiles/marklogic-server-ubi-rootless:base b/dockerFiles/marklogic-server-ubi-rootless:base index 04c1869e..9738a354 100644 --- a/dockerFiles/marklogic-server-ubi-rootless:base +++ b/dockerFiles/marklogic-server-ubi-rootless:base @@ -162,10 +162,11 @@ RUN if [ -s /tmp/converters.rpm ]; then chown ${ML_USER}:users /tmp/converters.r ############################################################### # Configure GDB for debugging and set capabilities for non-root usage ############################################################### +# Same mode as other home-directory files (chmod 740 in the builder stage); CIS rule file_permission_user_init_files requires 0740 or less permissive. RUN setcap cap_sys_ptrace+ep "$(readlink -f /usr/bin/gdb)" \ && echo "set auto-load safe-path /" > /home/${ML_USER}/.gdbinit \ && chown ${ML_USER}:users /home/${ML_USER}/.gdbinit \ - && chmod 644 /home/${ML_USER}/.gdbinit + && chmod 740 /home/${ML_USER}/.gdbinit ############################################################### # Remove optional packages that have known vulnerabilities diff --git a/test/structure-test-rootless.yaml b/test/structure-test-rootless.yaml new file mode 100644 index 00000000..95ce618d --- /dev/null +++ b/test/structure-test-rootless.yaml @@ -0,0 +1,10 @@ +schemaVersion: "2.0.0" + +# Rootless-only checks. make structure-test adds this file for rootless image types. +fileExistenceTests: +- name: 'gdbinit mode matches other home-directory files' + path: '/home/marklogic_user/.gdbinit' + shouldExist: true + permissions: '-rwxr-----' + uid: 1000 + gid: 100