Do you have node.js project that has specs and you want to make sure these specs run when you push your code to GitHub repo? Well you must setup CI in your project to avoid failing specs to be pushed to remote origin. Read this article to learn how can you setup CI for node.js project using GitHub Actions.
Node.js Template to run specs
GitHub provides Node.js workflow template to run your specs and install dependencies whenever you push to your main or master branch. Learn more about the node.js GitHub template here.
My Node.js Workflow template file steps
- I want to run my specs only in one node.js version
- I created a
Here is the workflow that I created for my node.js project:
Main.yaml complete node.js template for GitHub
name: Node.js CI on: push: branches: [main] pull_request: branches: [main] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Use Node.js uses: actions/setup-node@v2 with: node-version: '12.x' registry-url: https://registry.npmjs.org scope: '@octocat' - run: npm ci - run: npm run build --if-present - run: npm run test:once
Running GitHub workflow to test node.js specs
When I push my code in GitHub actions I see my CI build is passing. I am happy now 😄
Running GitHub workflow for failing tests
Lets fail specs and push our code.
Lets go over GitHub actions and check if our build is passing?
No, In my repo I see failing symbol.
Let me check actions there I see my build failed.
Here is the detailed error
How do you feel, did you setup differently let me know what improvement I can do?
How can I reject push from repository members using GitHub Actions?
You can’t use GitHub Actions to prevent a push; you would need a pre-receive hook to do that. GitHub’s cloud version doesn’t currently support that, although GitHub Enterprise Server (the on-premises version) does.
Read the stack-overflow answer here.
What I did to reject push if build failed in GitHub actions?
So basically the rules that I follow in my project:
- for each work always create Feature branch.
- If you feature branch build is passing.
- Then only merge your code to main/master branch.
You can always setup branch policy that no one directly push to your main/master branch. Everyone has to create pull request.
That way you can secure your code base in master/main branch.
I hope this is helpful.
How to skip GitHub actions CI to run on push
Sometime if you want to just update the project documents like readme file etc. and push your code and you do not want to run the entire CI to build, test and deploy etc. Then use the trick use
[skip CI] in your commit comments. Also add the filter in your workflow template to not run build when
[skip CI] text present in commit comments.
Add below expression in your workflow template:
if: "!contains(github.event.head_commit.message, '[skip CI]')"
Here is example to skip ci build in GitHub actions:
chore: updating readme [skip CI]
See my CI build did not run
Hope this article is helpful for all beginners for GitHub actions and node.js like me!
Thanks for reading my article till end. I hope you learned something special today. If you enjoyed this article then please share to your friends and if you have suggestions or thoughts to share with me then please write in the comment box.
Become full stack developer 💻
- Please subscribe to All-Access Membership PRO plan to access current and future angular, node.js and related courses.
- Please subscribe to All-Access Membership ELITE plan to get everything from PRO plan. Additionally, you will get access to a monthly live Q&A video call with
Rupeshand you can ask doubts/questions and get more help, tips and tricks.
Your bright future is awaiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a new Software Developer, Architect or Lead Engineer role.
Founder of Fullstack Master