|
| 1 | +name: Node.js Unit Test |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + os: |
| 7 | + type: string |
| 8 | + description: 'Operator System, such as: ubuntu-latest,macos-latest' |
| 9 | + default: 'ubuntu-latest, macos-latest, windows-latest' |
| 10 | + |
| 11 | + version: |
| 12 | + type: string |
| 13 | + description: 'Node.js Version, such as 16, 18' |
| 14 | + default: '16, 18' |
| 15 | + |
| 16 | + install: |
| 17 | + type: string |
| 18 | + description: 'Install dependencies script' |
| 19 | + default: 'npm i --no-package-lock --no-fund' |
| 20 | + |
| 21 | + action_ref: |
| 22 | + type: string |
| 23 | + description: 'Branch name for artusjs/github-actions, for test purpose' |
| 24 | + default: master |
| 25 | + |
| 26 | + mysql_version: |
| 27 | + type: string |
| 28 | + description: mysql version |
| 29 | + default: '5' |
| 30 | + |
| 31 | +jobs: |
| 32 | + Setup: |
| 33 | + runs-on: ubuntu-latest |
| 34 | + outputs: |
| 35 | + os: ${{ steps.handler.outputs.os }} |
| 36 | + version: ${{ steps.handler.outputs.version }} |
| 37 | + |
| 38 | + steps: |
| 39 | + # Checkout action repository |
| 40 | + - name: Checkout |
| 41 | + uses: actions/checkout@v3 |
| 42 | + with: |
| 43 | + repository: artusjs/github-actions |
| 44 | + path: action_repo |
| 45 | + ref: ${{ inputs.action_ref }} |
| 46 | + |
| 47 | + # Setup Node.js environment |
| 48 | + - name: Setup Node.js |
| 49 | + uses: actions/setup-node@v3 |
| 50 | + |
| 51 | + # Install dependencies |
| 52 | + - name: Install dependencies |
| 53 | + run: npm i --no-package-lock --no-fund |
| 54 | + working-directory: action_repo/scripts/test |
| 55 | + |
| 56 | + # Normalize inputs style |
| 57 | + - name: Convert Inputs to Matrix |
| 58 | + id: handler |
| 59 | + run: node action_repo/scripts/test/index.js |
| 60 | + env: |
| 61 | + INPUT_OS: ${{ inputs.os }} |
| 62 | + INPUT_VERSION: ${{ inputs.version }} |
| 63 | + |
| 64 | + Test: |
| 65 | + needs: Setup |
| 66 | + strategy: |
| 67 | + fail-fast: false |
| 68 | + matrix: |
| 69 | + os: ${{ fromJSON(needs.setup.outputs.os) }} |
| 70 | + version: ${{ fromJSON(needs.setup.outputs.version) }} |
| 71 | + |
| 72 | + name: Test (${{ matrix.os }}, ${{ matrix.version }}) |
| 73 | + runs-on: ${{ matrix.os }} |
| 74 | + |
| 75 | + services: |
| 76 | + mysql: |
| 77 | + image: mysql:${{ inputs.mysql_version }} |
| 78 | + env: |
| 79 | + MYSQL_ALLOW_EMPTY_PASSWORD: true |
| 80 | + MYSQL_DATABASE: test |
| 81 | + ports: |
| 82 | + - 3306:3306 |
| 83 | + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 |
| 84 | + |
| 85 | + redis: |
| 86 | + # https://docs.github.com/en/actions/using-containerized-services/about-service-containers#example-mapping-redis-ports |
| 87 | + image: redis |
| 88 | + ports: |
| 89 | + # Opens tcp port 6379 on the host and service container |
| 90 | + - 6379:6379 |
| 91 | + |
| 92 | + concurrency: |
| 93 | + group: ${{ github.workflow }}-#${{ github.event.pull_request.number || github.ref }}-(${{ matrix.os }}, ${{ matrix.version }}) |
| 94 | + cancel-in-progress: true |
| 95 | + |
| 96 | + steps: |
| 97 | + - name: Checkout Git Source |
| 98 | + uses: actions/checkout@v3 |
| 99 | + |
| 100 | + - name: Use Node.js ${{ matrix.version }} |
| 101 | + uses: actions/setup-node@v3 |
| 102 | + with: |
| 103 | + node-version: ${{ matrix.version }} |
| 104 | + |
| 105 | + - name: Install Dependencies |
| 106 | + run: ${{ inputs.install }} |
| 107 | + |
| 108 | + - name: Run Lint |
| 109 | + run: npm run lint --if-present |
| 110 | + |
| 111 | + - name: Run Test |
| 112 | + run: npm run ci |
| 113 | + |
| 114 | + - name: Code Coverage |
| 115 | + uses: codecov/codecov-action@v3 |
| 116 | + with: |
| 117 | + token: ${{ secrets.CODECOV_TOKEN }} |
0 commit comments