Copying previous job artifacts into a Dockerfile

Hey, I have a CI job that runs an npm build command and stores a folder in artifacts.

NPM Build:
  script:
    - npm run build
  artifacts:
    paths:
      - build

Docker Build:
  script:
    - docker build .
  dependencies: ['NPM Build']
  needs: ['NPM Build']

In my docker build I want to get access to the build folder inside my Dockerfile

FROM node:18-alpine

# Get build folder

However, I can’t see a way to do this. Any help would be great, thanks.

I’ve asked GitLab Duo Chat, and solution 1 sounds like the easiest with the additional build context. Note: Untested.


I did not know that, and asked a follow up question.

Also was curious about DinD on self-managed.

Solution 1: Copy artifacts into Docker context

NPM Build:
  script:
    - npm run build
  artifacts:
    paths:
      - build

Docker Build:
  script:
    - cp -r build/ docker-context/  # Copy artifacts to a directory in your Docker context
    - docker build -f Dockerfile docker-context/
  dependencies: ['NPM Build']
  needs: ['NPM Build']
FROM node:18-alpine

# The build folder is now at the root of the build context
COPY build/ /app/build/
2 Likes

Thanks for the reply. I managed to implement option 1, we was already using buildx which has --build-context flag. Many thanks for the help

1 Like