Pushing a docker image to aws ECR through jenkins pipeline

Sithija Thewahettige
2 min readJul 21, 2019

--

here is a project i worked on

what i basically had to do was to create a jenkins pipeline to create a docker image of my code base and push it to amazon ecr image repository

so to do this you will need an aws account

have to be little familiar with aws code build and aws ecr

and also github and jenkins

above image summarizes what i did

but let me explain it in detail

first i created a jenkins job (my jenkins server was a windows server )

what it basically did was pull out the code that i am interetsed from the github and invoke a aws codebuild job afterwards

so the codebuild job will look for a file called buildspec.yml in the root folder of our codebase

then codebuild will perform the steps one by one that is in the buildspec

in my case building the project to building the docker image to tagging the docker image and pushing it to ecr ALL These steps were included in the build spec.

below is a sample buildspec.yml

phases:
install:
runtime-versions:
java: openjdk8
docker: 18
python: 3.7

pre_build:
commands:
- chmod +x gradlew
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --no-include-email --region us-east-1)
build:
commands:
- echo Build started on `date`
- ./gradlew build -x test
- echo Building the Docker image...
- docker build -t cp-batch .
- docker tag {tags to tag the image }
post_build:
commands:
- echo Build completed
- echo Pushing the Docker images...
- docker push {ECR account name}

so as above build spec will install the relevant runtimes during the install phase

then during the prebuild phase it will log in to the aws ecr account

then during the build phase it will build the code with gradle

and build the docker image too while tagging it so it can be identified

the finally during the pstbuild phase this docker image that was built and tagged will be pushed to our aws ecr image repo.

--

--