Upload built artifact from another step

41 Views Asked by At

I have a question about my bitbucket pipeline. I have set up all the variables but I am not able to copy the built docker image to another location in the next pipeline step.

 preview:
      - step:
          name: Build Docker image for preview branch and save as .tar
          caches:
            - docker
          size: 2x #2x memory flag
          services:
            - docker
          script:
            - echo "Building Docker image for preview branch..."
            - docker build -f ./src/API/Dockerfile -t $BITBUCKET_REPO_SLUG .
            - docker save -o $BITBUCKET_REPO_SLUG.tar $BITBUCKET_REPO_SLUG
      - step:
          name: Upload Docker image as artifact
          script:
            - pipe: atlassian/bitbucket-upload-file:0.7.1
              variables:
                BITBUCKET_USERNAME: $BITBUCKET_USERNAME
                BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
                FILENAME: "$BITBUCKET_REPO_SLUG.tar"
                DIRECTORY: "downloads"

It yields the following error:

Status: Downloaded newer image for bitbucketpipelines/bitbucket-upload-file:0.7.1
INFO: Executing the pipe...
✖ File Application.tar doesn't exist.

(docker build is successful)

How can I copy the resulted docker built .tar file to a new location?

2

There are 2 best solutions below

3
N1ngu On

Artifacts that should be copied from former step onto subsequent steps must be declared with the artifacts key.

With your example:

pipelines:
  branches:
    preview:
      - step:
          name: Build Docker image for preview branch and save as .tar
          caches:
            - docker
          services:
            - docker
          script:
            - echo "Building Docker image for preview branch..."
            - docker build -f ./src/API/Dockerfile -t $BITBUCKET_REPO_SLUG .
            - docker save -o dockerimage.tar $BITBUCKET_REPO_SLUG
          artifacts:
            - dockerimage.tar
      - step:
          name: Upload Docker image as artifact
          script:
            - pipe: atlassian/bitbucket-upload-file:0.7.1
              variables:
                BITBUCKET_USERNAME: $BITBUCKET_USERNAME
                BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
                FILENAME: dockerimage.tar
                DIRECTORY: "downloads"

You won't be able to interpolate variables in there so you should settle for a hard name or putting all images in a subfolder (then, declare artifacts: ["build/*.tar"] and whatnot)

0
Haezer89 On

had to change the filepermissions before with chmod

services:
  - docker
script:
  - echo "Building Docker image for preview branch..."
  - docker build -f ./src/API/Dockerfile -t $BITBUCKET_REPO_SLUG .
  - docker save -o $BITBUCKET_REPO_SLUG.tar $BITBUCKET_REPO_SLUG
  - chmod 644 $BITBUCKET_REPO_SLUG.tar
artifacts:
  - application.tar

Hope this helps other people too!