diff --git a/.github/workflows/localstack-tests.yml b/.github/workflows/localstack-tests.yml new file mode 100644 index 0000000..a7bfac1 --- /dev/null +++ b/.github/workflows/localstack-tests.yml @@ -0,0 +1,87 @@ +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' + run-check-types: + description: 'Enable check types' + required: false + type: boolean + default: true + +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: Check Types + if: ${{ inputs.run-check-types }} + run: npm run check-types + + - 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..fd4921d --- /dev/null +++ b/localstack-tests/README.md @@ -0,0 +1,131 @@ +# Localstack Tests - Reusable Workflow Documentation ๐Ÿš€ + +## 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.