Search code examples
amazon-web-servicesdockercontinuous-integrationbitbucket-pipelinesamazon-ecr

Bitbucket Pipelines to build Java app, Docker image and push it to AWS ECR?


I am setting up Bitbucket Pipelines for my Java app and what I want to achive is whenever I merge something with branch master, Bitbucket fires the pipeline, which in first step build and test my application, and in second step build Docker image from it and push it to ECR. The problem is that in second step it isn't possible to use the JAR file made in first step, because every step is made in a separate, fresh Docker container. Any ideas how to solve it?

My current files are:

1) Bitbucket-pipelines.yaml

pipelines:
  branches:
    master:
      - step:
          name: Build and test application
          services:
            - docker
          image: openjdk:11
          caches:
            - gradle
          script:
            - apt-get update
            - apt-get install -y python-pip
            - pip install --no-cache-dir docker-compose
            - bash ./gradlew clean build test testIntegration

      - step:
          name: Build and push image
          services:
            - docker
          image: atlassian/pipelines-awscli
          caches:
            - gradle
          script:
            - echo $(aws ecr get-login --no-include-email --region us-west-2)  > login.sh
            - sh login.sh
            - docker build -f Dockerfile -t my-application .
            - docker tag my-application:latest 212234103948.dkr.ecr.us-west-2.amazonaws.com/my-application:latest
            - docker push 212234103948.dkr.ecr.us-west-2.amazonaws.com/my-application:latest

2) Dockerfile:

FROM openjdk:11
VOLUME /tmp
EXPOSE 8080
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

And the error I receive:

Step 4/5 : COPY build/libs/*.jar app.jar
COPY failed: no source files were specified

Solution

  • I have found the solutions, it's quite simple - we should just use "artifacts" feature, so in first step the additional line:

    artifacts:
    - build/libs/*.jar
    

    solves the problem.