3

I have a existing Jenkins pipeline job which build docker image and push it to AWS ECR repository.

We are not using ECS in our shop.

I want to create a jenkins pipeline job which will take this latest ECR image form repository and put in an Existing EC2 instance,create container from that image and open on some port.

Need any sample pipeline job to achieve this,any reference will also help.

AWS_Lernar
  • 627
  • 2
  • 9
  • 26

1 Answers1

2

This is how you can pull docker image from ECR using Jenkins pipeline:

pipeline
{
    options
    {
        buildDiscarder(logRotator(numToKeepStr: '3'))
    }

    agent any
    environment 
    {
        PROJECT = 'tap_sample'
        ECRURL = 'http://999999999999.dkr.ecr.eu-central-1.amazonaws.com'
        ECRCRED = 'ecr:eu-central-1:tap_ecr'
    }
    stages
    {
        stage('Docker image pull')
        {
            steps
            {
                script
                {
                    sh("eval \$(aws ecr get-login --no-include-email | sed 's|https://||')")
                    docker.withRegistry(ECRURL, ECRCRED)
                    {
                        docker.image(PROJECT).pull()
                    }
                }
            }
        }
    }
}

The example is taken from this amazing article.

dmigo
  • 2,849
  • 4
  • 41
  • 62
  • @dmingo Thanks for reply.I saw this article.But my requirement is to Pull and the run the container form that image on particular existing EC2. – AWS_Lernar Nov 01 '19 at 10:15
  • Hmm.. have you considered using [AWS CodePipeline](https://aws.amazon.com/codepipeline/?nc1=h_ls) for the purpose? – dmigo Nov 01 '19 at 10:27
  • Unfortunately as per our shop standards we have to do this using jenkins only. – AWS_Lernar Nov 01 '19 at 10:30