This reusable GitHub Actions workflow automates the process of running tests against a local stack environment for Node.js applications. It is configurable via inputs for API keys, Sonar configuration, Node.js version, and other options. The workflow performs the following actions:
- Sets Up Environment: Configures the environment with necessary API keys and tokens. 🔑
- Prepares Node.js: Installs the specified Node.js version. ⚙️
- Installs Dependencies: Uses
npm cito install the dependencies. 📦 - Prepares Local Stack: Sets up the test environment using
npm run start-test-stack. 🏗️ - Builds the Project: Builds the project using
npm run build. 🔨 - Runs Quality Checks: Performs format checking, linting, and optional type checking. 🧹
- Installs Globally & Runs Tests: Installs the package globally and runs tests. ✅
- Runs Sonar Analysis: Performs code analysis using SonarQube/SonarCloud. 📊
| Input | Description | Required | Default |
|---|---|---|---|
| alchemy-api-key | API key for Alchemy. | Yes | - |
| infura-project-id | Project ID for Infura. | Yes | - |
| etherscan-api-key | API key for Etherscan. | Yes | - |
| sonar-token | Token for SonarCloud/SonarQube. | Yes | - |
| sonar-host-url | URL of Sonar server. | Yes | - |
| node-version | Node.js version to use. | No | 18 |
| run-check-types | Enable check types. | No | true |
The workflow sets up the following environment variables for the test job:
ALCHEMY_API_KEY: API key for Alchemy servicesINFURA_PROJECT_ID: Project ID for Infura servicesETHERSCAN_API_KEY: API key for Etherscan servicesSONAR_TOKEN: Authentication token for SonarQube/SonarCloudSONAR_HOST_URL: URL of the Sonar server
- Runs On:
ubuntu-latest - Environment Variables: Sets up API keys and tokens from inputs
- Checkout Repository: Uses
actions/checkout@v4to fetch your code. 📥 - Setup Node.js: Configures Node.js with
actions/setup-node@v4using the specified version. ⚙️ - Install Dependencies: Runs
npm cito install dependencies. 📦 - Prepare Local Stack: Executes
npm run start-test-stackto set up the test environment. 🏗️ - Build: Builds the project using
npm run build. 🔨 - Check Format: Verifies code formatting using
npm run check-format. 🧹 - Lint: Performs code linting using
npm run lint. 🧹 - Check Types: Conditionally runs type checking using
npm run check-typesif enabled. 🔍 - Install Global & Test: Installs the package globally and runs tests. ✅
- Updates apt packages
- Installs xxd utility
- Installs the package globally
- Runs tests using
npm test
- SonarScanner: Runs SonarQube/SonarCloud analysis using
SonarSource/sonarqube-scan-action@v5.1.0. 📊
-
Save the Workflow File
This workflow is already saved as.github/workflows/localstack-tests.ymlin the repository. 💾 -
Call the Reusable Workflow
In another workflow file (e.g., triggered by a pull request), invoke this reusable workflow like so:name: Run Localstack Tests on: pull_request: branches: [main, develop] jobs: test: uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/localstack-tests.yml@main with: alchemy-api-key: ${{ secrets.ALCHEMY_API_KEY }} infura-project-id: ${{ secrets.INFURA_PROJECT_ID }} etherscan-api-key: ${{ secrets.ETHERSCAN_API_KEY }} sonar-token: ${{ secrets.SONAR_TOKEN }} sonar-host-url: ${{ secrets.SONAR_HOST_URL }} node-version: '20' run-check-types: true
-
Configure Secrets
Ensure that the following secrets are added to your repository's settings:ALCHEMY_API_KEY: Your Alchemy API keyINFURA_PROJECT_ID: Your Infura project IDETHERSCAN_API_KEY: Your Etherscan API keySONAR_TOKEN: Your SonarQube/SonarCloud authentication token
-
NPM Scripts:
- Your project must have the following npm scripts defined in package.json:
start-test-stack: Script to set up the local test environmentbuild: Script to build the projectcheck-format: Script to verify code formattinglint: Script to perform code lintingcheck-types: Script for type checking (optional, can be disabled)test: Script to run tests
- Your project must have the following npm scripts defined in package.json:
-
Sonar Configuration:
- You should have a sonar-project.properties file or equivalent configuration for SonarQube/SonarCloud analysis.
-
Environment Setup:
- Sets up environment variables for API keys and tokens.
- Checks out the repository code.
-
Node.js Configuration:
- Installs the specified Node.js version (default: 18).
- Prepares the Node.js environment for testing.
-
Build and Quality Checks:
- Installs dependencies using
npm ci. - Prepares the local stack environment.
- Builds the project.
- Performs code quality checks (formatting, linting, and optional type checking).
- Installs dependencies using
-
Testing:
- Installs the package globally.
- Runs tests using
npm test.
-
Code Analysis:
- Runs SonarQube/SonarCloud analysis to evaluate code quality and identify issues.