# Jenkins Build and ECR Push

> Docker build with Jenkins and push to Amazon ECR!

\ <br>

### Before getting started

* [Learn about Docker](https://chloe-codes1.gitbook.io/til/docker/01_getting_started_with_docker)
* [Learn about Amazon ECR](https://chloe-codes1.gitbook.io/til/aws/aws-services/amazon_ecr)

\ <br>

### Docker Build & Push with Jenkins

<br>

#### 1. Include a `Dockerfile` in the service's source code

#### 2. Configure the `jenkins` server to be able to access ECR

#### 3. Create a deployment Job for the service in `jenkins`, and set the Job to build a new image using the pre-included Dockerfile when executed

#### 4. Build a new docker image on the `jenkins` server and then push the image to ECR

#### 5. Finally, on the test server, pull the new image from ECR, delete the existing container, and create a new container

\ <br>

### Configure Jenkins job example

<br>

#### ex1) Discard old builds

* **Discard old builds**
  * With the settings shown in the example above, Build History is stored for 10 days and a maximum of 20 build histories are kept

    ex)

<br>

#### ex2) Invoke Gradle script

* For Java projects built with Gradle, you can specify Tasks as shown above

<br>

#### ex3) Execute shell

* You can write shell scripts to be executed during the Jenkins job build as shown above

<br>

#### ex4) Post-build Actions

* To save artifacts after the build, you can configure settings as shown above
