From 1bd101b0f4f32ffdaa3973785a48e8f2ade54e32 Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Tue, 29 Apr 2025 13:57:56 +0200 Subject: [PATCH 1/3] feat(tests): add Localstack testing workflow --- .github/workflows/localstack-tests.yml | 78 ++++++++++++++++++++++++++ localstack-tests/README.md | 3 + 2 files changed, 81 insertions(+) create mode 100644 .github/workflows/localstack-tests.yml create mode 100644 localstack-tests/README.md diff --git a/.github/workflows/localstack-tests.yml b/.github/workflows/localstack-tests.yml new file mode 100644 index 0000000..0ee3d8e --- /dev/null +++ b/.github/workflows/localstack-tests.yml @@ -0,0 +1,78 @@ +name: Localstack Tests + +on: + workflow_call: + inputs: + alchemy-api-key: + description: 'API key for Alchemy' + required: true + type: string + infura-project-id: + description: 'Project ID for Infura' + required: true + type: string + etherscan-api-key: + description: 'API key for Etherscan' + required: true + type: string + sonar-token: + description: 'Token for SonarCloud/ SonarQube' + required: true + type: string + sonar-host-url: + description: 'URL of Sonar server' + required: true + type: string + node-version: + description: 'Node.js version to use' + required: false + type: string + default: '18' + +jobs: + test: + runs-on: ubuntu-latest + env: + ALCHEMY_API_KEY: ${{ inputs.alchemy-api-key }} + INFURA_PROJECT_ID: ${{ inputs.infura-project-id }} + ETHERSCAN_API_KEY: ${{ inputs.etherscan-api-key }} + SONAR_TOKEN: ${{ inputs.sonar-token }} + SONAR_HOST_URL: ${{ inputs.sonar-host-url }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.node-version }} + + - name: Install dependencies + run: npm ci + + - name: Prepare local stack + id: prepare + run: npm run start-test-stack + + - name: Build + run: npm run build + + - name: Check format + run: npm run check-format + + - name: Lint + run: npm run lint + + - name: Install global & Test + run: | + sudo apt-get update + sudo apt-get install -y xxd + npm install -g . + npm test + + - name: SonarScanner + uses: SonarSource/sonarqube-scan-action@v5.1.0 + env: + SONAR_TOKEN: ${{ inputs.sonar-token }} + SONAR_HOST_URL: ${{ inputs.sonar-host-url }} diff --git a/localstack-tests/README.md b/localstack-tests/README.md new file mode 100644 index 0000000..664240d --- /dev/null +++ b/localstack-tests/README.md @@ -0,0 +1,3 @@ +# Localstack Tests + +doc From fa2244b092037ae1e6f040e05bb1920065665a3c Mon Sep 17 00:00:00 2001 From: Ugo Mignon <56931733+TartanLeGrand@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:46:42 +0200 Subject: [PATCH 2/3] feat: add check types --- .github/workflows/localstack-tests.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/localstack-tests.yml b/.github/workflows/localstack-tests.yml index 0ee3d8e..fe4da10 100644 --- a/.github/workflows/localstack-tests.yml +++ b/.github/workflows/localstack-tests.yml @@ -28,6 +28,11 @@ on: required: false type: string default: '18' + run-check-types: + description: 'Enable check types' + required: false + type: string + default: true jobs: test: @@ -64,6 +69,10 @@ jobs: - name: Lint run: npm run lint + - name: Check Types + if: ${{ inputs.run-check-types }} + run: npm run check-types + - name: Install global & Test run: | sudo apt-get update From 0d50993d75652dee1c6758e312e21bfed1e11efa Mon Sep 17 00:00:00 2001 From: Ugo Date: Mon, 16 Jun 2025 12:53:23 +0200 Subject: [PATCH 3/3] feat: update localstack tests workflow documentation and change input type for check types --- .github/workflows/localstack-tests.yml | 2 +- localstack-tests/README.md | 132 ++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 3 deletions(-) diff --git a/.github/workflows/localstack-tests.yml b/.github/workflows/localstack-tests.yml index fe4da10..a7bfac1 100644 --- a/.github/workflows/localstack-tests.yml +++ b/.github/workflows/localstack-tests.yml @@ -31,7 +31,7 @@ on: run-check-types: description: 'Enable check types' required: false - type: string + type: boolean default: true jobs: diff --git a/localstack-tests/README.md b/localstack-tests/README.md index 664240d..fd4921d 100644 --- a/localstack-tests/README.md +++ b/localstack-tests/README.md @@ -1,3 +1,131 @@ -# Localstack Tests +# Localstack Tests - Reusable Workflow Documentation ๐Ÿš€ -doc +## Overview ๐ŸŒŸ + +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 ci` to 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. ๐Ÿ“Š + +## Workflow Inputs ๐Ÿ› ๏ธ + +| **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` | + +### Environment Variables ๐ŸŒ + +The workflow sets up the following environment variables for the test job: + +- `ALCHEMY_API_KEY`: API key for Alchemy services +- `INFURA_PROJECT_ID`: Project ID for Infura services +- `ETHERSCAN_API_KEY`: API key for Etherscan services +- `SONAR_TOKEN`: Authentication token for SonarQube/SonarCloud +- `SONAR_HOST_URL`: URL of the Sonar server + +## Job and Steps โš™๏ธ + +### Job Name: `test` + +- **Runs On**: `ubuntu-latest` +- **Environment Variables**: Sets up API keys and tokens from inputs + +#### Steps: + +1. **Checkout Repository**: Uses `actions/checkout@v4` to fetch your code. ๐Ÿ“ฅ +2. **Setup Node.js**: Configures Node.js with `actions/setup-node@v4` using the specified version. โš™๏ธ +3. **Install Dependencies**: Runs `npm ci` to install dependencies. ๐Ÿ“ฆ +4. **Prepare Local Stack**: Executes `npm run start-test-stack` to set up the test environment. ๐Ÿ—๏ธ +5. **Build**: Builds the project using `npm run build`. ๐Ÿ”จ +6. **Check Format**: Verifies code formatting using `npm run check-format`. ๐Ÿงน +7. **Lint**: Performs code linting using `npm run lint`. ๐Ÿงน +8. **Check Types**: Conditionally runs type checking using `npm run check-types` if enabled. ๐Ÿ” +9. **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` +10. **SonarScanner**: Runs SonarQube/SonarCloud analysis using `SonarSource/sonarqube-scan-action@v5.1.0`. ๐Ÿ“Š + +## How to Use This Reusable Workflow ๐Ÿ”„ + +1. **Save the Workflow File** + This workflow is already saved as `.github/workflows/localstack-tests.yml` in the repository. ๐Ÿ’พ + +2. **Call the Reusable Workflow** + In another workflow file (e.g., triggered by a pull request), invoke this reusable workflow like so: + + ```yaml + 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 + ``` + +3. **Configure Secrets** + Ensure that the following secrets are added to your repository's settings: + - `ALCHEMY_API_KEY`: Your Alchemy API key + - `INFURA_PROJECT_ID`: Your Infura project ID + - `ETHERSCAN_API_KEY`: Your Etherscan API key + - `SONAR_TOKEN`: Your SonarQube/SonarCloud authentication token + +## Prerequisites ๐Ÿ“‹ + +1. **NPM Scripts**: + - Your project must have the following npm scripts defined in package.json: + - `start-test-stack`: Script to set up the local test environment + - `build`: Script to build the project + - `check-format`: Script to verify code formatting + - `lint`: Script to perform code linting + - `check-types`: Script for type checking (optional, can be disabled) + - `test`: Script to run tests + +2. **Sonar Configuration**: + - You should have a sonar-project.properties file or equivalent configuration for SonarQube/SonarCloud analysis. + +## Workflow Steps in Detail ๐Ÿ” + +1. **Environment Setup**: + - Sets up environment variables for API keys and tokens. + - Checks out the repository code. + +2. **Node.js Configuration**: + - Installs the specified Node.js version (default: 18). + - Prepares the Node.js environment for testing. + +3. **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). + +4. **Testing**: + - Installs the package globally. + - Runs tests using `npm test`. + +5. **Code Analysis**: + - Runs SonarQube/SonarCloud analysis to evaluate code quality and identify issues.