With the use of the Jenkins Docker Pipeline plugin, it’s easy to build and push Docker images.    

For example, building in a Jenkinsfile:

script {
    dockerImage = docker.build("${env.DOCKER_IMAGE_TAG}",  "${args}")
}

And push:

script {
    docker.withRegistry("${env.DOCKER_PRIVATE_REGISTRY_URL}",
            "docker-private-registry-${env.DEPLOY_ENVIRONMENT}") {
        dockerImage.push("${env.DOCKER_IMAGE_SHORT_PUSH_TAG}")
    }
}

The Docker registry username and password are provided by the credential ID “docker-private-registry-${env.DEPLOY_ENVIRONMENT}”. However Amazon ECR uses tokens that are only valid for 12 hours. So the password that you specify when creating the credential will only work in the example above for a short period of time.

With the following script it’s More >