Skip to content

Configure Testcontainers to leverage proxy service on CI #2937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 5 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pipeline {
options { timeout(time: 30, unit: 'MINUTES') }
environment {
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
}
steps {
script {
Expand All @@ -47,7 +48,7 @@ pipeline {
when {
beforeAgent(true)
allOf {
branch(pattern: "main|(\\d\\.\\d\\.x)", comparator: "REGEXP")
branch(pattern: "issue/testcontainers|main|(\\d\\.\\d\\.x)", comparator: "REGEXP") // TODO: Drop it
not { triggeredBy 'UpstreamCause' }
}
}
Expand All @@ -60,6 +61,7 @@ pipeline {
options { timeout(time: 30, unit: 'MINUTES')}
environment {
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
}
steps {
script {
Expand All @@ -77,6 +79,7 @@ pipeline {
options { timeout(time: 30, unit: 'MINUTES')}
environment {
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
}
steps {
script {
Expand All @@ -94,6 +97,7 @@ pipeline {
options { timeout(time: 30, unit: 'MINUTES')}
environment {
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
}
steps {
script {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.6-SNAPSHOT</version>
<version>3.0.6-testcontainers-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.0.6-SNAPSHOT</version>
<version>3.0.6-testcontainers-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.6-SNAPSHOT</version>
<version>3.0.6-testcontainers-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.6-SNAPSHOT</version>
<version>3.0.6-testcontainers-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.0.6-SNAPSHOT</version>
<version>3.0.6-testcontainers-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.0.6-SNAPSHOT</version>
<version>3.0.6-testcontainers-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2015-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.support;

import java.util.List;

import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.ImageNameSubstitutor;

/**
* A {@link ImageNameSubstitutor} only used on CI servers to leverage internal proxy solution, that needs to vary the
* prefix based on which container image is needed.
*
* @author Greg Turnquist
*/
public class ProxyImageNameSubstitutor extends ImageNameSubstitutor {

private static final List<String> NAMES_TO_PROXY_PREFIX = List.of("ryuk");

private static final List<String> NAMES_TO_LIBRARY_PREFIX = List.of("mysql", "postgres");
private static final String PROXY_PREFIX = "harbor-repo.vmware.com/dockerhub-proxy-cache/";

@Override
public DockerImageName apply(DockerImageName dockerImageName) {

if (NAMES_TO_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
return DockerImageName.parse(applyProxyPrefix(dockerImageName.asCanonicalNameString()));
}

if (NAMES_TO_LIBRARY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
return DockerImageName.parse(applyProxyAndLibraryPrefix(dockerImageName.asCanonicalNameString()));
}

return dockerImageName;
}

@Override
protected String getDescription() {
return "Spring Data Proxy Image Name Substitutor";
}

/**
* Apply a library-based prefix.
*/
private static String applyProxyPrefix(String imageName) {
return PROXY_PREFIX + imageName;
}

/**
* Apply a non-library based prefix.
*/
private static String applyProxyAndLibraryPrefix(String imageName) {
return PROXY_PREFIX + "library/" + imageName;
}
}