In this exercise we'll learn how nx improves the way how we write and execute CI/CD pipelines.
Please go to the repo and create a PR with your changes.
In best case you give it a name that is somehow unique to your name :)
Create a new folder .github/workflows
and create an empty ci.yml
file.
First, the meta. We want it to run only on your branch specifically and treat it as main branch.
That's why we configure the branches
to your branch name.
CI Meta
name: CI
on:
push:
branches:
- YOUR_BRANCH_NAME # 👈️👈️👈️👈️👈️👈️👈️ IMPORTANT
Now, we can add in the jobs.
For now we only have one job called main
. It should:
checkout
setup-node
run: npm ci
CI Skeleton
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: 20
cache: 'npm'
- run: npm ci
Cool, we now have a CI that spins up a usable node environment and also installs all of our node_modules.
Well done, we are close to getting there!
Add e.g. npx nx affected -t lint build
as a run command to get
executed after the npm ci
.
CI with nx commands
name: CI
on:
push:
branches:
- YOUR_BRANCH_NAME # 👈️👈️👈️👈️👈️👈️👈️ IMPORTANT
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: 20
cache: 'npm'
- run: npm ci
- run: npx nx affected -t lint build --base=HEAD^
Nicely done, I guess it's time to push and execute :).
Run git push
to push the changes to your branch and watch how the
CI is getting executed.
You should notice on the first run, that nothing gets executed as nothing was affected at all.
Nx presents you with useful information about what happened during your CI run.
Note
Really take your time here to actually play around with the dashboard. It really gives you valuable information about any task that is getting executed.
Inspect the CI run results. Expand the section where your pipeline code is executed. It'll give you information in form of a link that points to the nx cloud dashboard, representing the current run on CI.
There you can inspect the outcome of tasks. You see the stdout of the respective command.
The Nx cloud report tells you about the outcome of task executions without looking at the pipeline terminals. Also from here you will get to the nx cloud dashboard, representing the current run on CI.
Now do some changes and let the pipeline actually execute something - but please let it fail! Introduce something that will prevent it from succeed. It's easiest to make the lint or some test fail.
Tip
Make sure to visit the nx cloud dashboard. It'll tell you the details you want to know :)
Now do some "healthy" changes and let the pipeline actually execute something that doesn't break. It's up to you what you want to make affected.
Tip
Make sure to visit the nx cloud dashboard. It'll tell you the details you want to know :) Take especially a look at the cache hit or miss part
Wait until the pipeline has finished its job and run a local build. You should see that you now get the cached output from distributed cache.
Now let's do the same trick, but in vice versa.
- Create a change locally
- warmup the cache by running e.g
nx build movies
git push
your changes
Now you can watch the CI using the cache you have produced locally. It's pure beauty 🤌
Make sure to visit the nx cloud dashboard. It'll tell you about cache hits or misses!