I currently have a single job that does everything (testing, building and Docker imaging).
I would like to move from this single job workflow to a multi job workflow in order to parallelise tasks.
Currently it consists of: Testing –> Code Building –> Docker Building (3 images built sequentially)
I would like to move to this paradigm:
- Testing –> Code Building
- Docker Building (3 images built in parallel)
The reason why I went with the first system is because until now I haven’t found a way to persist the installed packages and built artifacts across jobs. The first two steps generate the node_modules
and the dist
folders.
Is there a way to do so? Am I missing something?
If not, is there a smarter alternative?
2
Answers
@VonC posted a great answer, I'm just adding a few minor details to the advancements I've made.
As he said,
artifacts
provide an excellent way to transfer data.Upload them when you're saving them:
Download them when you need them
Parallelisation of multiple docker builds is also rather simple, instead of going thru Dockerfiles like he proposed, I took a more programmatic approach:
And everytime I needed to set metadata for tagging, I took this approach:
In GitHub Actions, jobs can run on different runners and do not share the file system, which is why data generated during one job is not available in subsequent jobs.
To share data between jobs, you will have to use artifacts.
For instance, here is a multi-job system workflow, where the "
node_modules
" and "dist
" folders are shared across jobs:In the first job, perform your testing and code building as you currently do.
After building the code, upload the "
node_modules
" and "dist
" folders as artifacts. You will use theactions/upload-artifact
action to upload these folders.In the subsequent Docker building job, you will first download the artifacts using the
actions/download-artifact
action and then build your Docker images.This is a build matrix for the Docker building job to build three Docker images in parallel, using three different Dockerfiles named "
Dockerfile1
", "Dockerfile2
", and "Dockerfile3
".This workflow allows you to parallelize your Docker image builds while sharing the "
node_modules
" and "dist
" folders across jobs, which were generated in the initial build job.