|
1 | | -# Localstack Tests |
| 1 | +# Localstack Tests - Reusable Workflow Documentation 🚀 |
2 | 2 |
|
3 | | -doc |
| 3 | +## Overview 🌟 |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | +- **Sets Up Environment**: Configures the environment with necessary API keys and tokens. 🔑 |
| 8 | +- **Prepares Node.js**: Installs the specified Node.js version. ⚙️ |
| 9 | +- **Installs Dependencies**: Uses `npm ci` to install the dependencies. 📦 |
| 10 | +- **Prepares Local Stack**: Sets up the test environment using `npm run start-test-stack`. 🏗️ |
| 11 | +- **Builds the Project**: Builds the project using `npm run build`. 🔨 |
| 12 | +- **Runs Quality Checks**: Performs format checking, linting, and optional type checking. 🧹 |
| 13 | +- **Installs Globally & Runs Tests**: Installs the package globally and runs tests. ✅ |
| 14 | +- **Runs Sonar Analysis**: Performs code analysis using SonarQube/SonarCloud. 📊 |
| 15 | + |
| 16 | +## Workflow Inputs 🛠️ |
| 17 | + |
| 18 | +| **Input** | **Description** | **Required** | **Default** | |
| 19 | +|----------------------|--------------------------------------|--------------|-------------| |
| 20 | +| **alchemy-api-key** | API key for Alchemy. | Yes | - | |
| 21 | +| **infura-project-id**| Project ID for Infura. | Yes | - | |
| 22 | +| **etherscan-api-key**| API key for Etherscan. | Yes | - | |
| 23 | +| **sonar-token** | Token for SonarCloud/SonarQube. | Yes | - | |
| 24 | +| **sonar-host-url** | URL of Sonar server. | Yes | - | |
| 25 | +| **node-version** | Node.js version to use. | No | `18` | |
| 26 | +| **run-check-types** | Enable check types. | No | `true` | |
| 27 | + |
| 28 | +### Environment Variables 🌍 |
| 29 | + |
| 30 | +The workflow sets up the following environment variables for the test job: |
| 31 | + |
| 32 | +- `ALCHEMY_API_KEY`: API key for Alchemy services |
| 33 | +- `INFURA_PROJECT_ID`: Project ID for Infura services |
| 34 | +- `ETHERSCAN_API_KEY`: API key for Etherscan services |
| 35 | +- `SONAR_TOKEN`: Authentication token for SonarQube/SonarCloud |
| 36 | +- `SONAR_HOST_URL`: URL of the Sonar server |
| 37 | + |
| 38 | +## Job and Steps ⚙️ |
| 39 | + |
| 40 | +### Job Name: `test` |
| 41 | + |
| 42 | +- **Runs On**: `ubuntu-latest` |
| 43 | +- **Environment Variables**: Sets up API keys and tokens from inputs |
| 44 | + |
| 45 | +#### Steps: |
| 46 | + |
| 47 | +1. **Checkout Repository**: Uses `actions/checkout@v4` to fetch your code. 📥 |
| 48 | +2. **Setup Node.js**: Configures Node.js with `actions/setup-node@v4` using the specified version. ⚙️ |
| 49 | +3. **Install Dependencies**: Runs `npm ci` to install dependencies. 📦 |
| 50 | +4. **Prepare Local Stack**: Executes `npm run start-test-stack` to set up the test environment. 🏗️ |
| 51 | +5. **Build**: Builds the project using `npm run build`. 🔨 |
| 52 | +6. **Check Format**: Verifies code formatting using `npm run check-format`. 🧹 |
| 53 | +7. **Lint**: Performs code linting using `npm run lint`. 🧹 |
| 54 | +8. **Check Types**: Conditionally runs type checking using `npm run check-types` if enabled. 🔍 |
| 55 | +9. **Install Global & Test**: Installs the package globally and runs tests. ✅ |
| 56 | + - Updates apt packages |
| 57 | + - Installs xxd utility |
| 58 | + - Installs the package globally |
| 59 | + - Runs tests using `npm test` |
| 60 | +10. **SonarScanner **: Runs SonarQube/SonarCloud analysis using `SonarSource/[email protected]`. 📊 |
| 61 | + |
| 62 | +## How to Use This Reusable Workflow 🔄 |
| 63 | + |
| 64 | +1. **Save the Workflow File** |
| 65 | + This workflow is already saved as `.github/workflows/localstack-tests.yml` in the repository. 💾 |
| 66 | + |
| 67 | +2. **Call the Reusable Workflow** |
| 68 | + In another workflow file (e.g., triggered by a pull request), invoke this reusable workflow like so: |
| 69 | + |
| 70 | + ```yaml |
| 71 | + name: Run Localstack Tests |
| 72 | + on: |
| 73 | + pull_request: |
| 74 | + branches: [main, develop] |
| 75 | + |
| 76 | + jobs: |
| 77 | + test: |
| 78 | + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/localstack-tests.yml@main |
| 79 | + with: |
| 80 | + alchemy-api-key: ${{ secrets.ALCHEMY_API_KEY }} |
| 81 | + infura-project-id: ${{ secrets.INFURA_PROJECT_ID }} |
| 82 | + etherscan-api-key: ${{ secrets.ETHERSCAN_API_KEY }} |
| 83 | + sonar-token: ${{ secrets.SONAR_TOKEN }} |
| 84 | + sonar-host-url: ${{ secrets.SONAR_HOST_URL }} |
| 85 | + node-version: '20' |
| 86 | + run-check-types: true |
| 87 | + ``` |
| 88 | +
|
| 89 | +3. **Configure Secrets** |
| 90 | + Ensure that the following secrets are added to your repository's settings: |
| 91 | + - `ALCHEMY_API_KEY`: Your Alchemy API key |
| 92 | + - `INFURA_PROJECT_ID`: Your Infura project ID |
| 93 | + - `ETHERSCAN_API_KEY`: Your Etherscan API key |
| 94 | + - `SONAR_TOKEN`: Your SonarQube/SonarCloud authentication token |
| 95 | + |
| 96 | +## Prerequisites 📋 |
| 97 | + |
| 98 | +1. **NPM Scripts**: |
| 99 | + - Your project must have the following npm scripts defined in package.json: |
| 100 | + - `start-test-stack`: Script to set up the local test environment |
| 101 | + - `build`: Script to build the project |
| 102 | + - `check-format`: Script to verify code formatting |
| 103 | + - `lint`: Script to perform code linting |
| 104 | + - `check-types`: Script for type checking (optional, can be disabled) |
| 105 | + - `test`: Script to run tests |
| 106 | + |
| 107 | +2. **Sonar Configuration**: |
| 108 | + - You should have a sonar-project.properties file or equivalent configuration for SonarQube/SonarCloud analysis. |
| 109 | + |
| 110 | +## Workflow Steps in Detail 🔍 |
| 111 | + |
| 112 | +1. **Environment Setup**: |
| 113 | + - Sets up environment variables for API keys and tokens. |
| 114 | + - Checks out the repository code. |
| 115 | + |
| 116 | +2. **Node.js Configuration**: |
| 117 | + - Installs the specified Node.js version (default: 18). |
| 118 | + - Prepares the Node.js environment for testing. |
| 119 | + |
| 120 | +3. **Build and Quality Checks**: |
| 121 | + - Installs dependencies using `npm ci`. |
| 122 | + - Prepares the local stack environment. |
| 123 | + - Builds the project. |
| 124 | + - Performs code quality checks (formatting, linting, and optional type checking). |
| 125 | + |
| 126 | +4. **Testing**: |
| 127 | + - Installs the package globally. |
| 128 | + - Runs tests using `npm test`. |
| 129 | + |
| 130 | +5. **Code Analysis**: |
| 131 | + - Runs SonarQube/SonarCloud analysis to evaluate code quality and identify issues. |
0 commit comments