Github Actions - Run server & frontend, then execute tests - jestjs

I would like to use Github Actions for CI and run tests before the branch can be merged.
I have a single repository that has both my server and frontend within it (Nest & Angular). I am using Cypress/Jest for my tests.
I need my backend server running for my frontend cypress tests to pass.
Currently GH Actions doesn't move onto the next step because the backend process is running - but that's what I need to happen...
How should I set this up so that I can use GH Actions for CI?
name: test
on: [push]
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OTHER_SECRETS: ${{ secrets.otherSecrets }}
jobs:
cypress-run:
runs-on: macos-11
steps:
# start cypress w/github action: https://github.com/cypress-io/github-action
- name: Setup Node.js environment
uses: actions/setup-node#v2.5.0
with:
node-version: '16.13.0'
- name: Checkout
uses: 'actions/checkout#v2'
- name: "Start Backend"
run: |
cd server &&
npm install &&
npm run build &&
npm run start:prod
- name: "Start Frontend"
run: |
npm install &&
npm run build &&
npm run start
- name: Cypress run
uses: cypress-io/github-action#v2
with:
record: true
browser: chrome
- name: "Run Jest Tests"
run: |
cd server &&
npm run test
#note: I have tried appending the "&& sleep 10 && curl http://localhost:port -i" option to the npm commands - and it hasn't worked for me.
#note2: It's my first time w/ GH Actions, so maybe I'm missing something obvious!!

#note: I have tried appending the "&& sleep 10 && curl http://localhost:port -i" option to the npm commands - and it hasn't worked for me.
There is a slight error here, && will wait for the previous command to complete and only run the next one if it is successful & would run the previous command in the background and then will move on to running the next one. Therefore as nothing stops your server, && won't work.
I am not sure it is the cleanest way but the following should work, I have used an equivalent to run a UI in one of my projects.
- name: "Start Backend"
run: |
cd server &&
npm install &&
npm run build &&
npm run start:prod &
sleep 5 &&
curl http://localhost:port -I
- name: "Start Frontend"
run: |
npm install &&
npm run build &&
npm run start &
sleep 5 &&
curl http://localhost:port -I

I had the same problem, server running but never moving to the next step of running the Cypress tests.
Thanks didwefixit, using just one & worked to start the server and then run Cypress test script worked:
jobs:
build:
env:
CI: true
strategy:
matrix:
node-version: [14.x, 16.x]
runs-on: [ ubuntu-latest ]
steps:
- uses: actions/checkout#v2
- name: Use Node.js version ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install --prefix client
- run: npm install --prefix server
- run: npm install
- run: npm run build --prefix client
- run: npm run start --prefix server & npm run test
script in client package.json:
"build": "BUILD_PATH=../server/public react-scripts build"
script in server package.json:
"start": "node src/server.js"
script in root package.json:
"test": "npx cypress run"

Related

How to set up a scheduled cypress test on github action to run one spec file only?

I have set up a cron job for my cypress tests however I want to run it on a specific test alone.
This is my cron job:
name: Cypress Tests
on: [push]
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
# Install NPM dependencies, cache them correctly
# and run all Cypress tests
- name: Cypress run
uses: cypress-io/github-action#v4.x.x # use the explicit version number
with:
build: npm run build
start: npm start
I just want to run this one spec file:
Is there a way to run this spec file alone?
Try this out!
Add new command in script section of package.json. Use file or folder here.
"scripts": {
"cypress:spec-run": "cypress run --browser chrome --spec test.spec.js"
},
Run the commond in respective crone job
npm run cypress:spec-run

Github Actions with Jest Suddenly Stopped

I registered test-coverage ci in github actions
name: TEST-COVERAGE
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- name: Checkout source code
uses: actions/checkout#v3
- name: Set up Nest.js
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
- name: Set up Node_modules
run: |
cd ./client
rm -rf node_modules
yarn install --immutable --immutable-cache --check-cache
cd ..
cd ./server/gateway
rm -rf node_modules
npm ci
cd ../services/core
rm -rf node_modules
npm ci
cd ../auth
rm -rf node_modules
npm ci
cd ../ticket
rm -rf node_modules
npm ci
- name: Run Test Coverage
run: |
rm -rf node_modules
npm ci
npm run coveralls
- name: Coveralls
uses: coverallsapp/github-action#master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
I passed all the test cases with jest, and while executing it, a situation occurred where the Github action suddenly stopped. Even locally, the test code passes and does not emit an error message, but it stops without any output, so it cannot be solved.
Error: Process completed with exit code 1.
Below is my Current Directory
client
server
gateway
auth
core
ticket
At first, it seemed to be a Github action memory leak, so when I changed the npm install command to npm ci, it ran fine. However, as we added and expanded new features, npm ci encountered the same problem.
https://github.com/boostcampwm-2022/web03-FanUP/actions/runs/3521371482/jobs/5903147717
This is github actions result
"scripts": {
"test": "jest --detectOpenHandles --forceExit",
"coverage": "jest --detectOpenHandles --coverage --forceExit",
"coveralls": "jest --coverage --detectOpenHandles --forceExit && coveralls < ./coverage/lcov.info"
},

How can I run multiple Node App in a Github Actions?

This is my actions
# This is a basic workflow to help you get started with Actions
name: CI/CD Akper Bina Insan - Live
on:
push:
branches: [ master ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- name: Install Yarn
run: npm install -g yarn
- name: Build UI - Akper Bina Insan
working-directory: ./ui
run: yarn && yarn build && yarn start
- name: Build Backend Service - Akper Bina Insan
working-directory: ./be
run: yarn && yarn build && yarn prod
After it finished with the first run, it don't go continue with the second run. Even though the server is ready. I waited for 5 minutes then I stopped since I don't want to waste time.
How can I make it run for the second one?
GitHub actions are a tool for CI/CD and not for hosting (running) your application.
In the given workflows, you build an then run your UI application. The run command is a blocking process - e.g. your workflow will remain blocked because you have started your UI application. You should not do that in workflows.
Use GitHub Action for build and test, but not for hosting.
Adding an ending & to the node command solved this for my use-case
run: yarn && yarn build && yarn start &
or
run: |
yarn
yarn build
yarn start &
Using & in the end of a command, the shell executes the command in the background in a subshell, therefore running the first yarn start won't be blocking your next step in the workflow.

Github actions not working after npm start

I have a very simple config in order to run e2e tests with Cypress using Github Actions in a Nextjs app. When it reaches the npm start command, although it seems to work since it gives the correct output: > Ready on http://localhost:3000, the step stays in pending state without ever advancing to the next step.
Any suggestions on how to fix this?
Following github actions config (.github/workflows/nodejs.yml):
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm ci
npm run build --if-present
npm start
npx wait-on http://localhost:3000
env:
CI: true
- name: Run Cypress
run: |
npx cypress run
env:
CI: true
Using the control operator & works for me. Try the following.
- name: npm install, build, and test
run: |
npm ci
npm run build --if-present
npm start & npx wait-on http://localhost:3000
env:
CI: true
From man bash
If a command is terminated by the control operator &, the shell executes the command in
the background in a subshell. The shell does not wait for the command to finish, and the
return status is 0. These are referred to as asynchronous commands. Commands separated
by a ; are executed sequentially; the shell waits for each command to terminate in turn.
The return status is the exit status of the last command executed.

.ebextensions with CodePipeline and Elastic Beanstalk

I started working on my first CodePipeline with node.js app which is hosted on github. I would like to create simple pipe as follow:
Github repo triggers pipe
Test env (Elastic Beanstalk app) is built from S3 .zip file
Test env runs npm test and npm lint
If everything is OK then QA env (another EB app) is built
For above pipe I've created .config files under .ebextensions directory:
I would like to use npm install --production for QA and PROD env, but it seems that EC2 can't find node nor npm. I checked logs and EC2 triggered npm install by default in temporary folder, then it fails on my first script and app catalogue is always empty.
container_commands:
install-dev:
command: "npm install"
test: "[ \"$NODE_ENV\" = \"TEST\" ]"
ignoreErrors: false
install-prod:
command: "npm install --production"
test: "[ \"$NODE_ENV\" != \"TEST\" ]"
ignoreErrors: false
Is it posible to run unit tests and linting without jenkins?
container_commands:
lint:
command: "npm run lint"
test: "[ \"$NODE_ENV\" = \"TEST\" ]"
ignoreErrors: false
test:
command: "npm run test"
test: "[ \"$NODE_ENV\" = \"TEST\" ]"
ignoreErrors: false
I set NODE_ENV for each Elastic Beanstalk instance. No matter what I will do every time my pipe fails because of npm is not recognized, but how is it possible if I'm running 64bit Amazon Linux with node.js ? What's more I cannot find any examples about CodePipeline with node.js in AWS Docs. Thanks in advance!
If you're using AWS for CI/CD, you can use CodeBuild. However, Github provides a great feature called Actions for running Unit Tests, which I find much simpler than AWS. Anyway, I will walk you through both examples:
Using AWS for running Unit Tests
Essentially, you could create a new stage into your CodePipeline, and configure the CodeBuild for running Unit Tests, e.g.
First, add a buildspec.yml file in the root folder of your app so you can use the following example:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- echo Installing Mocha globally...
- npm install -g mocha
pre_build:
commands:
- echo Installing dependencies...
- npm install
- npm install unit.js
build:
commands:
- echo Build started on `date`
- echo Run Unit Tests and so on
- npm run test
- npm run lint
post_build:
commands:
- echo Build completed on `date`
# THIS IS OPTIONAL
artifacts:
files:
- app.js
- package.json
- src/app/*
- node_modules/**/*
You can find everything you need in the BackSpace Academy, this course is for free:
AWS DevOps CI/CD - CodePipeline, Elastic Beanstalk and Mocha
Using Github for running Unit Tests
You could create your custom actions using Github, it will automatically set up everything you need in your root folder, e.g.
After choosing the appropriate workflow, it will automatically generate a folder/file ".github > workflow > nodejs.yml".
So it will look like this:
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm test
env:
CI: true
I hope you could find everything you need in this answer. Cheers
Have you incorporated CodeBuild into your pipeline?
You should
1) Create a pipeline whose source is your github account. Go through the setup procedure so that commits on a particular branch trigger the Codepipeline
2) Create a test stage in your Codepipeline which leverages the CodeBuild service. In order to run your Node tests, you might need to provide a configured build environment. And you probably also need to provide a build spec file that specifies the tests to run etc.
3) Assuming that the test stage passes, you can determine if the pipeline continues to another stage which is linked to an elasticbeanstalk app environment which supports the Node platform. These environments are purely for artifacts that have passed testing, so I see no need to have the .ebextensions commands written above.
Have a read of what CodeBuild can do to help you run tests for Node,
https://aws.amazon.com/codebuild/
Good luck!

Resources