use of com.google.cloud.tools.jib.registry.credentials.RegistryCredentials in project jib by google.
the class BuildImageMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateParameters();
ProjectProperties projectProperties = new ProjectProperties(project, getLog());
if (mainClass == null) {
mainClass = projectProperties.getMainClassFromMavenJarPlugin();
if (mainClass == null) {
throwMojoExecutionExceptionWithHelpMessage(new MojoFailureException("Could not find main class specified in maven-jar-plugin"), "add a `mainClass` configuration to jib-maven-plugin");
}
}
SourceFilesConfiguration sourceFilesConfiguration = projectProperties.getSourceFilesConfiguration();
// Parses 'from' into image reference.
ImageReference baseImage = getBaseImageReference();
// Checks Maven settings for registry credentials.
session.getSettings().getServer(baseImage.getRegistry());
Map<String, Authorization> registryCredentials = new HashMap<>(2);
// Retrieves credentials for the base image registry.
Authorization baseImageRegistryCredentials = getRegistryCredentialsFromSettings(baseImage.getRegistry());
if (baseImageRegistryCredentials != null) {
registryCredentials.put(baseImage.getRegistry(), baseImageRegistryCredentials);
}
// Retrieves credentials for the target registry.
Authorization targetRegistryCredentials = getRegistryCredentialsFromSettings(registry);
if (targetRegistryCredentials != null) {
registryCredentials.put(registry, targetRegistryCredentials);
}
RegistryCredentials mavenSettingsCredentials = RegistryCredentials.from("Maven settings", registryCredentials);
ImageReference targetImageReference = ImageReference.of(registry, repository, tag);
ImageFormat imageFormatToEnum = ImageFormat.valueOf(imageFormat);
BuildConfiguration buildConfiguration = BuildConfiguration.builder(new MavenBuildLogger(getLog())).setBaseImage(baseImage).setTargetImage(targetImageReference).setCredentialHelperNames(credHelpers).setKnownRegistryCredentials(mavenSettingsCredentials).setMainClass(mainClass).setEnableReproducibleBuilds(enableReproducibleBuilds).setJvmFlags(jvmFlags).setEnvironment(environment).setTargetFormat(imageFormatToEnum.getManifestTemplateClass()).build();
// Uses a directory in the Maven build cache as the Jib cache.
Path cacheDirectory = Paths.get(project.getBuild().getDirectory(), CACHE_DIRECTORY_NAME);
if (!Files.exists(cacheDirectory)) {
try {
Files.createDirectory(cacheDirectory);
} catch (IOException ex) {
throw new MojoExecutionException("Could not create cache directory: " + cacheDirectory, ex);
}
}
Caches.Initializer cachesInitializer = Caches.newInitializer(cacheDirectory);
if (useOnlyProjectCache) {
cachesInitializer.setBaseCacheDirectory(cacheDirectory);
}
getLog().info("");
getLog().info("Pushing image as " + targetImageReference);
getLog().info("");
// TODO: Instead of disabling logging, have authentication credentials be provided
// Disables annoying Apache HTTP client logging.
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "error");
RegistryClient.setUserAgentSuffix(USER_AGENT_SUFFIX);
buildImage(new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cachesInitializer));
getLog().info("");
getLog().info("Built and pushed image as " + targetImageReference);
getLog().info("");
}
use of com.google.cloud.tools.jib.registry.credentials.RegistryCredentials in project jib by google.
the class BuildConfigurationTest method testBuilder.
@Test
public void testBuilder() {
String expectedBaseImageServerUrl = "someserver";
String expectedBaseImageName = "baseimage";
String expectedBaseImageTag = "baseimagetag";
String expectedTargetServerUrl = "someotherserver";
String expectedTargetImageName = "targetimage";
String expectedTargetTag = "targettag";
List<String> expectedCredentialHelperNames = Arrays.asList("credentialhelper", "anotherCredentialHelper");
String expectedMainClass = "mainclass";
RegistryCredentials expectedKnownRegistryCredentials = Mockito.mock(RegistryCredentials.class);
boolean expectedEnableReproducibleBuilds = false;
List<String> expectedJvmFlags = Arrays.asList("some", "jvm", "flags");
Map<String, String> expectedEnvironment = ImmutableMap.of("key", "value");
Class<? extends BuildableManifestTemplate> expectedTargetFormat = OCIManifestTemplate.class;
BuildConfiguration.Builder buildConfigurationBuilder = BuildConfiguration.builder(Mockito.mock(BuildLogger.class)).setBaseImage(ImageReference.of(expectedBaseImageServerUrl, expectedBaseImageName, expectedBaseImageTag)).setTargetImage(ImageReference.of(expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)).setCredentialHelperNames(expectedCredentialHelperNames).setKnownRegistryCredentials(expectedKnownRegistryCredentials).setEnableReproducibleBuilds(expectedEnableReproducibleBuilds).setMainClass(expectedMainClass).setJvmFlags(expectedJvmFlags).setEnvironment(expectedEnvironment).setTargetFormat(OCIManifestTemplate.class);
BuildConfiguration buildConfiguration = buildConfigurationBuilder.build();
Assert.assertEquals(expectedBaseImageServerUrl, buildConfiguration.getBaseImageRegistry());
Assert.assertEquals(expectedBaseImageName, buildConfiguration.getBaseImageRepository());
Assert.assertEquals(expectedBaseImageTag, buildConfiguration.getBaseImageTag());
Assert.assertEquals(expectedTargetServerUrl, buildConfiguration.getTargetRegistry());
Assert.assertEquals(expectedTargetImageName, buildConfiguration.getTargetRepository());
Assert.assertEquals(expectedTargetTag, buildConfiguration.getTargetTag());
Assert.assertEquals(expectedCredentialHelperNames, buildConfiguration.getCredentialHelperNames());
Assert.assertEquals(expectedKnownRegistryCredentials, buildConfiguration.getKnownRegistryCredentials());
Assert.assertEquals(expectedEnableReproducibleBuilds, buildConfiguration.getEnableReproducibleBuilds());
Assert.assertEquals(expectedMainClass, buildConfiguration.getMainClass());
Assert.assertEquals(expectedJvmFlags, buildConfiguration.getJvmFlags());
Assert.assertEquals(expectedEnvironment, buildConfiguration.getEnvironment());
Assert.assertEquals(expectedTargetFormat, buildConfiguration.getTargetFormat());
}
Aggregations