Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add static-analysis-js.yml #159

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions .github/workflows/static-analysis-js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Static code analysis Javascript

on:
workflow_call:
inputs:
NPM_REGISTRY_DOMAIN:
description: Domain of the private npm registry.
default: https://npm.pkg.github.com/
required: false
type: string
NODE_VERSION:
description: Node version with which the static analysis is to be executed.
default: 18
required: false
type: string
NODE_OPTIONS:
description: Space-separated list of command-line Node options.
type: string
default: ''
required: false
secrets:
NPM_REGISTRY_TOKEN:
description: Authentication for the private npm registry.
required: false
GITHUB_USER_EMAIL:
description: Email address for the GitHub user configuration.
required: false
GITHUB_USER_NAME:
description: Username for the GitHub user configuration.
required: false
GITHUB_USER_SSH_KEY:
description: Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME`.
required: false
ENV_VARS:
description: Additional environment variables as a JSON formatted object.
required: false

jobs:
static-analysis-js:
timeout-minutes: 5
runs-on: ubuntu-latest
env:
NODE_OPTIONS: ${{ inputs.NODE_OPTIONS }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }}
NODE_CACHE_MODE: ''
GITHUB_USER_EMAIL: ${{ secrets.GITHUB_USER_EMAIL }}
GITHUB_USER_NAME: ${{ secrets.GITHUB_USER_NAME }}
GITHUB_USER_SSH_KEY: ${{ secrets.GITHUB_USER_SSH_KEY }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up custom environment variables
env:
ENV_VARS: ${{ secrets.ENV_VARS }}
if: ${{ env.ENV_VARS }}
uses: actions/github-script@v7
with:
script: |
JSON
.parse(process.env.ENV_VARS)
.forEach(envVar => core.exportVariable(envVar.name, envVar.value));

- name: Set up SSH
if: ${{ env.GITHUB_USER_SSH_KEY != '' }}
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ env.GITHUB_USER_SSH_KEY }}

- name: Set up Git
run: |
git config --global user.email "${{ env.GITHUB_USER_EMAIL }}"
git config --global user.name "${{ env.GITHUB_USER_NAME }}"

- name: Set up node cache mode
run: |
if [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; then
echo "NODE_CACHE_MODE=npm" >> $GITHUB_ENV
else
echo "No lock files found"
fi

- name: Set up node
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.NODE_VERSION }}
registry-url: ${{ inputs.NPM_REGISTRY_DOMAIN }}
cache: ${{ env.NODE_CACHE_MODE }}

- name: Install dependencies
env:
ARGS: ${{ env.NODE_CACHE_MODE == 'npm' && 'ci' || 'install' }}
run: ${{ format('npm {0}', env.ARGS) }}

- name: Run tsc
run: |
./node_modules/.bin/tsc --noEmit --skipLibCheck --pretty false |
while read -r line
do
if [[ $line =~ ([0-9a-z\/\.]+)[\(]([0-9]+)[\,]([0-9]+)[\)].*(TS.*) ]]; then
echo "::error file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}"
fi
done
exit "${PIPESTATUS[0]}"
55 changes: 55 additions & 0 deletions docs/js.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- markdownlint-disable MD024 -->

# Reusable workflows – JavaScript

## Unit tests JavaScript
Expand Down Expand Up @@ -56,3 +58,56 @@ jobs:
NODE_VERSION: 14
JEST_ARGS: 'my-test --reporters=jest-junit --coverage'
```

## Static analysis Javascript

This workflow runs [Typescript compiler](https://www.typescriptlang.org/docs/handbook/compiler-options.html)
with `--noEmit` argument. It does so by executing the `tsc` binary in the `./node_modules/.bin/` folder.

**Simplest possible example:**

```yml
name: Static code analysis Javascript
on:
push:
jobs:
static-analysis-js:
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-js.yml@main
```

### Configuration parameters

#### Inputs

| Name | Default | Description |
|-------------------------|---------------------------------|-----------------------------------------------------------------------------------|
| `NODE_OPTIONS` | `''` | Space-separated list of command-line Node options |
| `NODE_VERSION` | `18` | Node version with which the assets will be compiled |
| `NPM_REGISTRY_DOMAIN` | `'https://npm.pkg.github.com/'` | Domain of the private npm registry |

#### Secrets

| Name | Description |
|-----------------------|------------------------------------------------------------------------------|
| `NPM_REGISTRY_TOKEN` | Authentication for the private npm registry |
| `GITHUB_USER_EMAIL` | Email address for the GitHub user configuration |
| `GITHUB_USER_NAME` | Username for the GitHub user configuration |
| `GITHUB_USER_SSH_KEY` | Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME` |
| `ENV_VARS` | Additional environment variables as a JSON formatted object |

**Example with configuration parameters:**

```yml
name: Static code analysis Javascript
on:
pull_request:
jobs:
static-analysis-js:
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-js.yml@main
secrets:
NPM_REGISTRY_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }}
ENV_VARS: >-
[{"name":"EXAMPLE_USERNAME", "value":"${{ secrets.USERNAME }}"}]
with:
NODE_VERSION: 18
```