package util import groovy.transform.CompileStatic import groovy.transform.Memoized import java.util.function.Supplier @CompileStatic class DockerLogin { static void perform() { loginDockerInternal() } @Memoized private static Object loginDockerInternal() { // Проверяем на всякий случай, что докер вообще установлен if (System.findExecutablesInPath(['docker']).isEmpty()) throw new FileNotFoundException("Can't find installed docker-compose at that system!") // Логинимся в Registry ScriptLog.printf "Performing login to registry..." def registryName = CIProperties.getProperty("docker.registry") if (registryName.isNullOrBlank()) { throw new IllegalStateException("Docker registry name not set") } def credentials = getDockerCredentials() validateCredentials(credentials) invokeLogin(registryName, credentials) ScriptLog.printf "Login into docker registry '${registryName}' successful!" } private static void invokeLogin(String registryName, DockerRegistryCredentials credentials) { sh "docker login $registryName -u $credentials.username -p $credentials.password" } private static void validateCredentials(DockerRegistryCredentials credentials) { if (credentials?.username?.isNullOrBlank()) { throw new IllegalStateException("Docker registry user not set") } if (credentials?.password?.isNullOrBlank()) { throw new IllegalStateException("Docker registry password not set") } } private static DockerRegistryCredentials getDockerCredentials() { def useFiles = CIProperties.findProperty("docker.registry.use-file-credentials") .orNull()?.toBoolean() ?: false if (useFiles) { ScriptLog.printf "Reading docker registry credentials from files" return readCredentialsFromFiles() } else { ScriptLog.printf "Reading docker registry credentials from envs" return readCredentialsFromEnvs() } } private static DockerRegistryCredentials readCredentialsFromEnvs() { return new DockerRegistryCredentials( username: CIProperties.getProperty("docker.registry.username"), password: CIProperties.getProperty("docker.registry.password") ) } private static DockerRegistryCredentials readCredentialsFromFiles() { return new DockerRegistryCredentials( username: DockerSecret.read("registry.username"), password: DockerSecret.read("registry.password") ) } } class DockerRegistryCredentials { String username String password }