- Basics of GitHub actions
- Use Nx to setup scalable checks on your PRs to ensure only passing code goes into master
- Explore other practical uses of
nx affected
Before starting on this task, it's important that you have a version of your local workshop pushed to your GitHub repo.
-
Create a new file
.github/workflows/ci.ymlname: Run CI checks # The name will show up on the GitHub Actions dashboard on: [pull_request] # This workflow will run only on Pull Requests jobs: test-store: # give our job an ID runs-on: ubuntu-latest # the image our job will run on name: Test Store # the name that will appear on the Actions UI steps: # what steps it will perform - uses: actions/checkout@v1 # checkout whatever branch the PR is using - uses: bahmutov/npm-install@v1 # trigger an `npm install` - run: npm run nx test store # test the "store" project test-api: runs-on: ubuntu-latest name: Test API steps: - uses: actions/checkout@v1 - uses: bahmutov/npm-install@v1 - run: npm run nx test api
-
Commit your changes to the main branch
git add . && git commit -m "add ci" git push origin master git checkout -b dynamic-title⚠️ I know we just switched to master above. But it was important we bring it up to date. Now we need to switch to a new branch so we can submit our PR.
- Open
apps/store/src/app/app.tsx
-
And make any change at all to the component (the title of the header dynamic):
const title = "Board Game Hoard"; // ... <Header title={title} />🐳 Hint - set the props in the Header as well
export interface HeaderProps { title: string; } export const Header = ({ title }: HeaderProps) => { const classes = useStyles(); return ( <AppBar position="static"> <Toolbar> <Typography variant="h6" className={classes.title}> {title} </Typography> </Toolbar> </AppBar> ); };
-
Commit all your changes and push your new branch.
- Go to GitHub and make a Pull Request to
master
- The unit tests will be failing - that's expected.
We are starting to set-up our CI, that will verify our Pull Requests to ensure bad code
doesn't go into master.
But now we're testing both projects - even though we only changed the store.
-
Let's use
nx affectedto only test the changed projects:Instead of running two
nxcommands in your CI, run a singlenx affectedcommand that tests all affected projects.🐳 Hint 1
Check-out this handy tutorial Refer to the docs
🐳 Hint 2
Since it's a Pull Request, your base commit will always be
--base=origin/master🐳 Hint 3
You should only need 1 job now:
jobs: test: runs-on: ubuntu-latest name: Testing affected apps steps: - uses: actions/checkout@v1 - uses: bahmutov/npm-install@v1.4.5 - run: .....
⚠️ It's okay to work on this on your new branch. We'll merge everything tomastereventually.
-
Commit and push. On your Github Actions log you should see only the
storetests running: -
Our tests are now being ran sequentially for each project. See if you can run them in parallel (consult the Nx Affected docs if unsure)
-
Our CI only does testing now. But we also have targets for
lint,e2eandbuild. Would really be handy if CI also told us if any of those failed.Add more jobs under your CI workflow that run affected for each of the above targets
- Commit and push your
ci.ymlchanges.
- You'll notice some new steps in the GitHub Actions UI. Some of them are failing. That is okay. We can fix them later.
- For now, you can merge your PR into
master
-
Switch to
masterlocally and pull latest so all your new CI changes are up to date.git checkout master git pull origin master
-
BONUS: Currently, if we create a PR with a change only to our
ci.ymlfile, ournx affectedcommands won't run at all: as they'll think no project has been affected:
To be safe, we'd like to mark all projects as affected whenever we change our CI config, as we don't know what those changes could have broken. Have a look through the docs in the hint and see if you can do this.
🎓 If you get stuck, check out the solution

