Guides / Share files between jobs with workspaces
Share files between jobs with workspaces
Each job runs on its own machine. A workspace carries files from one job to the jobs after it in the same workflow run, such as a build that several test jobs use.
A cache and a workspace differ in scope. A cache is reused across runs and may miss. A workspace belongs to one workflow run.
Prerequisites
Section titled “Prerequisites”- A job that makes files, and later jobs that need them.
-
Persist the files in the first job
Section titled “Persist the files in the first job”.hatchrun/config.yml jobs:build:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v5- run: npm ci && npm run build- workspace:persist:root: .paths: [dist, node_modules]rootis the directory thepathsare relative to. -
Attach the files in the later jobs
Section titled “Attach the files in the later jobs”attachrestores everything earlier jobs persisted into the directoryat:.hatchrun/config.yml jobs:e2e:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v5- workspace:attach:at: .- run: npm run e2e -
Order the jobs
Section titled “Order the jobs”attachrestores what earlier jobs persisted, so the job that persists must run first. Addrequiresin the workflow:.hatchrun/config.yml jobs:build:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v5- run: npm ci && npm run build- workspace:persist:root: .paths: [dist]e2e:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v5- workspace:attach:at: .- run: npm run e2edeploy:runs-on: ubuntu-lateststeps:- workspace:attach:at: .- run: ./deploy.sh distworkflows:ci:on:push:branches: [main]jobs:- build- e2e:requires: [build]- deploy:requires: [e2e] -
Validate
Section titled “Validate”Terminal window hatchrun validate
Expected result: e2e and deploy start with dist/ in their
working directory, without building again.
Good to know
Section titled “Good to know”persisttakesrootandpaths.attachtakesat. Neither takesname,iforenv.- Persist only what later jobs need. Every byte is uploaded and downloaded again.