use Cache of make build
Asked by Shrey:

image: shreysway07/srijan-alpine:latest

pipelines:  
default:

- step:  
name: Build Docker images \& push to ECR  
services:  
- docker

script:  
- make build  
- aws configure set default.region ${ECR_REGION}  
- aws configure set aws_access_key_id ${ECR_KEY}  
- aws configure set aws_secret_access_key ${ECR_SECRET}  
- docker login ${ECR_URL} --username AWS --password $(aws ecr get-login-password --region ${ECR_REGION})  
- curl -sfL <https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh> \| sh -s -- -b /usr/local/bin  
- bash image-scanning.sh  
- step:  
name: Force Deploy Image To ECR  
trigger: manual  
services:  
- docker  
script:  
- aws configure set default.region ${ECR_REGION}  
- aws configure set aws_access_key_id ${ECR_KEY}  
- aws configure set aws_secret_access_key ${ECR_SECRET}  
- docker login ${ECR_URL} --username AWS --password $(aws ecr get-login-password --region ${ECR_REGION})  
#- make build  
- make push_image_1  
- make push_image_2

<br />

Hey team I want to use cache of make build command in step 2 which am using in step 1 . How can I use that. Any help is highly appreciated .


Answers:
author: Theodora Boudale
body: Hi [@Shrey](/t5/user/viewprofilepage/user-id/5150807),

Does the command **make build** from the first step build a Docker image that you want to use on the second step? If so, you can do this with cache, if these two steps are part of the same pipeline and if they are not parallel. In order to do this, you can add the following to both of the two steps you posted here ([Caches documentation](https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/)):

```
caches:
  - docker
```

I would suggest including the command **make build** in the second step as well. Caches get cleared automatically 1 week after they are created. It is also possible for users to manually remove the cache from the Pipelines page of the repo. The second step you shared here is manual, so if any of these two things happen before you trigger the second step and the cache is not present, the second step will fail. Adding the command **make build** in the second step will ensure that the step doesn't fail because of a missing cache. If the cache is present, then the **make build** command in the second step will use the existing cache and the build will be faster.

If you have any questions, please feel free to reach out.

Kind regards,  
Theodora

comments: author: Shrey
body: I tried using using cache docker previously but the commit ID in ECR is being committed twice for eg for 1st time commit is 1234 it pushes to ECR with image:1234  
but when I commit 2nd time is how image:1234,567 .Commit ID logic written inside make build  

due to cache 1234 is also coming in next push


author: Theodora Boudale
body: Hi [@Shrey](/t5/user/viewprofilepage/user-id/5150807),

Thank you for reaching out. I'm not sure I understand what is happening, as I don't know what exactly your **make** commands are doing.

There is another option, you could use [artifacts](https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/) instead of cache. An example:

```
pipelines:
  default:
    - step:
        name: Build docker image
        services:
          - docker
        script:
          - docker build -t repo/imagename .
          - docker save --output tmp-image.docker repo/imagename
        artifacts:
          - tmp-image.docker
    - step:
        name: Deploy docker image
        services:
          - docker
        script:
          - docker load --input ./tmp-image.docker
          - docker images
```

In this example, I have added the **docker build** and **docker save** commands in the script of my first step, but you can include them both in the Makefile that the **make** command is using.

In the first step, I have defined as an artifact the file that the **docker save** command produces. Files defined as artifacts will be available for the next steps (as long as the steps are not parallel).

Then, in the second step, the **docker load** command will load the image from the artifact produced in the first step. With this approach, you don't have to build the image again in the second step.

Please feel free to let me know if this works for you.

If you still experience issues, it would be useful to have the build log from both steps and a description of what is the undesired behavior in this build log. You can download the build log for each step from our website: while viewing the build log for each step, there is an option to download it at the top right corner. Please make sure to sanitize any private/sensitive data prior to sharing here.

If you don't feel comfortable sharing build logs here, you can create a ticket with the support team (which will be visible only to you and Atlassian staff), and share more details and the build URL there. You can create a ticket via <https://support.atlassian.com/contact/#/>, in "What can we help you with?" select "Technical issues and bugs" and then Bitbucket Cloud as product.

Please feel free to let me know if you have any questions.

Kind regards,  
Theodora


Tags: bitbucket-cloud

cloud