All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intershop.gradle.icm.docker.tasks.PushImages.kt Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
/*
 * Copyright 2020 Intershop Communications AG.
 *
 * 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
 *
 *      http://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 com.intershop.gradle.icm.docker.tasks

import com.bmuschko.gradle.docker.DockerRegistryCredentials
import com.bmuschko.gradle.docker.tasks.AbstractDockerRemoteApiTask
import com.bmuschko.gradle.docker.tasks.RegistryCredentialsAware
import com.github.dockerjava.api.model.PushResponseItem
import com.intershop.gradle.icm.docker.ICMDockerPlugin
import com.intershop.gradle.icm.docker.tasks.utils.PushImageCallback
import com.intershop.gradle.icm.docker.utils.BuildImageRegistry
import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.model.ObjectFactory
import org.gradle.api.services.BuildServiceRegistry
import org.gradle.api.services.internal.BuildServiceRegistryInternal
import org.gradle.util.ConfigureUtil
import javax.inject.Inject

open class PushImages
        @Inject constructor(objectFactory: ObjectFactory):
        AbstractDockerRemoteApiTask(), RegistryCredentialsAware {

    private val registryCredentials: DockerRegistryCredentials =
            objectFactory.newInstance(DockerRegistryCredentials::class.java)

    /**
     * The target Docker registry credentials for usage with a task.
     */
    override fun getRegistryCredentials(): DockerRegistryCredentials {
        return registryCredentials
    }

    /**
     * Configures the target Docker registry credentials for use with a task.
     */
    override fun registryCredentials(action: Action?) {
        action!!.execute(registryCredentials)
    }

    /**
     * Set the credentials for the task.
     *
     * @param c closure with Docker registry credentials.
     */
    fun registryCredentials(c: Closure) {
        ConfigureUtil.configure(c, registryCredentials)
    }

    init {
        group = "icm image build"
        description = "Push all available images to registry."
        onlyIf {
            project.hasProperty("runOnCI") &&
                    project.property("runOnCI") == "true"
        }
    }

    override fun runRemoteCommand() {

        val serviceRegistry = services.get(BuildServiceRegistryInternal::class.java)
        val buildService = getBuildService(serviceRegistry, ICMDockerPlugin.BUILD_IMG_REGISTRY)

        if(buildService != null) {
            buildService.images.forEach { image ->
                logger.quiet("Pushing image '${image}'.")
                val pushImageCmd = dockerClient.pushImageCmd(image)
                val authConfig = registryAuthLocator.lookupAuthConfig(image, registryCredentials)
                pushImageCmd.withAuthConfig(authConfig)
                val callback = createCallback(nextHandler)
                pushImageCmd.exec(callback).awaitCompletion()
            }
        } else {
            throw GradleException("Buildservice registry is not correct configured.")
        }
    }

    private fun createCallback(nextHandler: Action?): PushImageCallback {
        return object : PushImageCallback() {
            override fun onNext(item: PushResponseItem) {
                if(nextHandler != null) {
                    try {
                        nextHandler.execute(item)
                    } catch ( e: Exception) {
                        logger.error("Failed to handle push response", e)
                        return
                    }
                }
                super.onNext(item)
            }
        }
    }

    private fun getBuildService(registry: BuildServiceRegistry, name: String): BuildImageRegistry? {
        val registration = registry.registrations.findByName(name)
                ?: throw GradleException ("Unable to find build service with name '$name'.")

        val buildservice = registration.service.get()
        return if(buildservice is BuildImageRegistry) {
            buildservice
        } else {
            null
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy