# 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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chloe-codes1.gitbook.io/til/infra/jenkins/jenkins_build_and_ecr_push.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
