How to write a pipeline for a pull request for lint and run tests - github

I never wrote pipelines in github actions, I was asked to create a pipeline for a pull request for repositories in github using the commands:
npm run lint;
npm run test;

GitHub's own setup-node Action has examples of how to do this. Based on the docs there, something like this placed in a YAML file in the .github/workflows directory should work:
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '16'
- run: npm install
- run: npm lint
- run: npm test

Related

How to publish a whole directory using Github Actions with the semantic-release Github plugin?

I want to publish a whole directory (the build directory) on a Github release using semantic-release but unfortunately it releases each build file as a single asset.
For reproduction:
I'm using the Vue CLI to generate a project vue create foo
Install semantic-release as a dev dependency npm install --save-dev semantic-release
Install the Github plugin for semantic-release npm install #semantic-release/github -D
Create a .releaserc.json with the content
.
{
"plugins":[
"#semantic-release/commit-analyzer",
"#semantic-release/release-notes-generator",
[
"#semantic-release/github",
{
"assets":[
{
"path":"dist",
"label":"foo-${nextRelease.gitTag}"
}
]
}
]
]
}
Inside the package.json set the version key to 0.0.0-development
Create a .github/workflows directory with the workflow ci.yml
.
name: CI
on:
push:
branches:
- main
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout#v2
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node#v2
with:
node-version: 16.x
- name: Install dependencies
run: npm install
- name: Run build
run: npm run build
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release --branches main
Commit and push it with feat: pushed
The release seems to be fine but unfortunately it didn't publish the dist directory as a single asset.
It simply published each file inside dist as a single
Adding the step
- name: Log
run: ls
shows that the dist directory exists
How can I fix that?
It seems this is not possible. So I have to add this step after building the app
- name: ZIP build
run: zip -r dist.zip dist
and set the assets config to
{
"path":"dist.zip",
"label":"foo-${nextRelease.gitTag}.zip"
}

Github actions: Dependencies lock file is not found in runners/path

I have a single Github repository for both server and frontend. The directory structure looks like:
root
|- frontend
|- server (Express App)
Github Action:
name: Node.js CI
on:
push:
branches: [ main ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
working-directory: './server'
- run: npm run start
working-directory: './server'
I only have a single job to build the Express server (and not the frontend yet) so I set the working-directory to ./server. However, I still get an error:
Dependencies lock file is not found in /home/{username}/runners.../repository_name. Supported file patterns: package-lock.json,yarn.lock
So apparently it's not trying to run in .../reposirtoy_name/server.
I'm just trying to build both server and frontend in single Github action.
There might be a chance that your problem is specifically with "uses: actions/setup-node". They mention in the docs that if you have multiple lock files or a lock file(s) in a directory that is not the root
In my case I had a single project with nested projects/dir. In my GitHub actions I wanted to run npm test on the nested project/dir so I had to specify to use my package.json inside the specific sub-directory. Double check to see that you are specifying the right directories with cache-dependency-path.
Specified here
https://github.com/actions/setup-node#caching-packages-dependencies
Try out this solution. It worked in my case.
In the build insert the default working directory
build:
runs-on: self-hosted
defaults:
run:
working-directory: ./server/
strategy:
matrix:
node-version: [14.x]
Then include cache dependency path. This should be the location of your package-lock.json file
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: './server/package-lock.json'
tldr
Make sure your checkout repo step is BEFORE setup node step, if using cache property with actions/setup-node#v3.
For me, it was caused by cache property on actions/setup-node#v3.
Without it - everything worked fine.
With it - failed. Reason is, it uses as cache key the package-lock.json (or yarn.lock) file.
See: https://github.com/actions/setup-node
My checkout repo step (actions/checkout#v2) was AFTER the setup node step, so it didn't find the package-lock.json file - because it wasn't checked out yet.

EACCES: permission denied, unlink '/home/runner/work/gos_front/gos_front/node_modules/.yarn-integrity

I have this workflow.yaml for github actions:
name: CI/CD
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14]
steps:
- name: Checkout repository
uses: actions/checkout#v2
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
uses: borales/actions-yarn#v2.0.0
with:
cmd: install --ignore-engines
- name: Run linters
uses: borales/actions-yarn#v2.0.0
with:
cmd: lint:prettier
- name: Run cypress
uses: cypress-io/github-action#v2
with:
build: npm run build
start: npm start
- name: Build
uses: borales/actions-yarn#v2.0.0
with:
cmd: build
Then it runs, on run cypress occurs an error:
error An unexpected error occurred: "EACCES: permission denied, unlink
'/home/runner/work/gos_front/gos_front/node_modules/.yarn-integrity'"
.
Before that I add cypress run all was right. I tried to add sudo but it not helped. I use yarn.
cypress-io/github-action tries to install dependencies as well and runs into an access issue. The problem is the way your node_modules folder is created.
I ran into this when using Docker, mapping node_modules from inside the docker image to the outside. This is a known issue.
You use borales/actions-yarn. This is an outdated package, that probably does something similar. You should instead use yarn directly. From their readme:
Please keep in mind that this Action was originally written for GitHub Actions beta (when Docker was the only way of doing things). Consider using actions/setup-node to work with Yarn. This repository will be mostly supporting the existing flows.

Cannot consume private NPM package hosted on GPR

I have a private repo that, using Github Actions workflow, I have published as a private npm package on Github Package Registry. I want to consume this package in the scope of another private project of mine. But there is an issue. Upon importing the GPR hosted package as a dependency I get a 'module not found' error.
Github Actions workflow successfully publishes private npm package to GPR.
The published package appears under 'Package' tab at Github user landing.
GPR_ACCESS_TOKEN is a PAT (ensuring that I can consume the package).
IMAGE: the error in question
.npmrc file at root of project consuming private package
#slackermorris:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=XXXX-XXXX-XXXX-XXXX
Github Action responsible for republishing private npm package to Github Registry.
name: Node.js Package
on:
push:
branches:
- master
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
- run: npm ci
- run: npm test
env:
CI: true
publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com
scope: slackermorris
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GPR_ACCESS_TOKEN}}
package.json of the published npm package.
"name": "#slackermorris/bostock-metaball-animation",
"version": "1.0.3",
"main": "index.js",
"author": "slackermorris",
"license": "MIT",
"publishConfig": {
"registry": "https://npm.pkg.github.com"
} ...
What you are trying should work, but I can confirm it does not. Better than putting your token in .npmrc is storing your Personal Access Token in a repo secret that is exported by the workflow. IE, create a repo secret named GPR_AUTH_TOKEN with the contents of your PAT. Then add this to your workflow:
env:
GPR_AUTH_TOKEN: ${{ secrets.GPR_AUTH_TOKEN }}
Then have your .npmrc load the token via the env variable:
//npm.pkg.github.com/:_authToken=${GPR_AUTH_TOKEN}
That avoids exposing your PAT to everyone with repo access.
That solution works for me on node.js v16 but it doesn't work with node 14.
What works for every LTS version of Node.js is to configure .npmrc with this npm step in your workflow:
- name: Configure NPM
run: npm config set '//npm.pkg.github.com/:_authToken' "${{secrets.GPR_AUTH_TOKEN}}"
After that, the npm install works as expected.

github action: run a test file on pull request

I want to run a test file when someone sends a pull request.
This is my action.yml file.
name: "GitHub Actions Test"
on:
pull_request:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
steps:
# - uses: actions/checkout#v1
- name: 'Install Node'
uses: actions/setup-node#v1
- name: Install mocha
run: npm install -g mocha
- name: Install dependencies
run: npm install
- name: "Run Test"
run: mocha test-mocha.test.js
but when running the test from github, I got the following error:
Error: No test files found: "test-mocha.test.js"
I wonder something is wrong on the last line of my yml file.
how to fix this?
This is because you've commented out the line that checks out your code:
# - uses: actions/checkout#v1 # Remove the comment from this line
By default, your code is not checked out in the workflow's directory. As such, you have to use the Checkout GitHub Action to check out your code.
From the README:
This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.