Understanding persist_to_workspace

Im struggling here and hope someone can help me out. I just dont get how the root and path attributes are working together with persist_to_workspace.

I do the following:

  - checkout
  - run: rm -rf .git
  - persist_to_workspace:
      root: /root
      paths:
        - project

Later I do:

attach_workspace:
   at: ~/

This works. In my first job I have the workspace at /home/circleci/project with my everything I need.
Now I build my frontend which gives me a folder at /home/circleci/project/frontend/build.

The only thing I want to do now is persisting this folder at the same path so that its available in the next job who attachs the workspace at ~/project/frontend/build. But however I do the persist_to_workspace the build folder always is at the same level of project.

ls
-> project frontend/build

instead of

ls
-> project/frontend/build

How can I do that?

After wasting 2 hours yesterday…I had it 2mins after posting here…

I did this

  - persist_to_workspace:
      root: ~/
      paths: project/frontend/build

And it seems to save the workspace like I want to.

Thanks for this. Had a similar issue for a project I was working for.

I needed to add a file through a job to the project during build. I did it this way.

I added the checkout in the config job, and disabled checkout in the aws-ecr and aws-ecs jobs:

version: 2.1
parameters:
  image_tag: #tag of the docker image to rollback to (app version)
    type: string
    default: "0"
  rollback_trigger: #was build trigger by a request to rollback app version
    type: boolean
    default: false
orbs:
  aws-ecr: circleci/aws-ecr@8.2.1
  aws-ecs: circleci/aws-ecs@03.2.0
workflows:
  build-and-deploy:
    jobs:
      # PULL REQUESTS
      - config:
          name: config-pr
          filters:
            branches:
              ignore:
                - development
                - main
                - staging
      - aws-ecr/build-and-push-image:
          attach-workspace: true
          aws-access-key-id: AWS_ACCESS_KEY_ID_DEV
          aws-cli-version: latest
          aws-secret-access-key: AWS_SECRET_ACCESS_KEY_DEV
          checkout: false
          create-repo: false
          dockerfile: Dockerfile
          name: build-pull-request
          path: .
          platform: linux/amd64
          public-registry: false
          push-image: false
          region: '${AWS_REGION}'
          registry-id: AWS_ACCOUNT_ID_DEV
          repo: '${APP_NAME}'
          repo-scan-on-push: true
          tag: 'latest,${CIRCLE_SHA1}'
          workspace-root: .
          requires:
            - config-pr
          filters:
            branches:
              ignore:
                - development
                - main
                - staging

jobs:
  config:
    working_directory: ~/my_dir
    docker:
      - image: cimg/node:18.4.0
    steps:
      - checkout
      - run:
          name: Create config file
          command: |
            echo '{"OWNER": "'${OWNER}'", "PASSWORD": "'${PASSWORD}'", "JWT_SECRET": "'${JWT_SECRET}'", "INTEGRATIONS": "'${INTEGRATIONS}'"}' > config.json
            ls -lah
            cat config.json
            pwd
      - persist_to_workspace:
          root: .
          paths: