|
| 1 | +name: Frontend Test |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + runner-class: |
| 7 | + description: "Github Runner class to use" |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + default: "ubuntu-latest" |
| 11 | + node-version: |
| 12 | + description: "Node version to use." |
| 13 | + required: false |
| 14 | + type: string |
| 15 | + default: "18.x" |
| 16 | + node-version-file: |
| 17 | + description: "Node version file to use. node-version overrides this parameter." |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + default: "" |
| 21 | + context: |
| 22 | + description: "Root directory to start the build from." |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + default: "." |
| 26 | + cache: |
| 27 | + description: "Use node modules installation caching. Default true." |
| 28 | + required: false |
| 29 | + type: boolean |
| 30 | + default: true |
| 31 | + cache-key-file: |
| 32 | + description: "Key file for cache." |
| 33 | + required: false |
| 34 | + type: string |
| 35 | + default: "${{ inputs.context }}/package.json" |
| 36 | + package-manager: |
| 37 | + description: "Package manager to use. Supports [npm, yarn]" |
| 38 | + required: false |
| 39 | + type: string |
| 40 | + default: "npm" |
| 41 | + build-script-name: |
| 42 | + description: "Build script name in package.json" |
| 43 | + required: false |
| 44 | + type: string |
| 45 | + default: "build" |
| 46 | + test_frontend_command: |
| 47 | + description: "Testing command to run" |
| 48 | + required: true |
| 49 | + type: string |
| 50 | + test_frontend_build: |
| 51 | + description: "Perform a build test" |
| 52 | + required: false |
| 53 | + type: boolean |
| 54 | + default: true |
| 55 | + |
| 56 | +jobs: |
| 57 | + node-build: |
| 58 | + runs-on: ${{ inputs.runner-class }} |
| 59 | + |
| 60 | + environment: |
| 61 | + name: ${{ github.ref_name }} |
| 62 | + |
| 63 | + outputs: |
| 64 | + artifact-name: ${{ steps.get_artifact_name.outputs.artifact_name }} |
| 65 | + |
| 66 | + permissions: |
| 67 | + contents: read |
| 68 | + packages: write |
| 69 | + |
| 70 | + steps: |
| 71 | + - name: Checkout repository |
| 72 | + uses: actions/checkout@v4 |
| 73 | + |
| 74 | + - id: node_setup |
| 75 | + name: Install node |
| 76 | + uses: actions/setup-node@v4 |
| 77 | + #reference: https://github.com/actions/setup-node |
| 78 | + with: |
| 79 | + node-version: ${{ inputs.node-version }} |
| 80 | + node-version-file: ${{ inputs.node-version-file && inputs.node-version-file || '' }} |
| 81 | + |
| 82 | + - name: Cache Node packages |
| 83 | + uses: actions/cache@v3 |
| 84 | + env: |
| 85 | + cache_name: node-${{ inputs.node-version || hashFiles(inputs.node-version-file) }}-${{ inputs.package-manager }}-${{ hashFiles(inputs.cache-key-file) }} |
| 86 | + with: |
| 87 | + key: ${{ runner.os }}-build-${{ env.cache_name }} |
| 88 | + path: | |
| 89 | + ~/.npm |
| 90 | + restore-keys: | |
| 91 | + ${{ runner.os }}-build-${{ env.cache_name }} |
| 92 | +
|
| 93 | + - id: node_packages_install |
| 94 | + name: Install node pacakges |
| 95 | + working-directory: ${{ inputs.context }} |
| 96 | + run: | |
| 97 | + case "${{ inputs.package-manager }}" in |
| 98 | + yarn) |
| 99 | + yarn |
| 100 | + ;; |
| 101 | + npm) |
| 102 | + npm i |
| 103 | + ;; |
| 104 | + esac |
| 105 | +
|
| 106 | + - id: vars_and_secrets |
| 107 | + name: Vars and Secrets to Env file |
| 108 | + env: |
| 109 | + VARS_CONTEXT: ${{ toJson(vars) }} |
| 110 | + SECRETS_CONTEXT: ${{ toJson(secrets) }} |
| 111 | + shell: bash |
| 112 | + run: | |
| 113 | + parsed_vars=$(jq -n --argjson VARS_CONTEXT "$VARS_CONTEXT" --argjson SECRETS_CONTEXT "$SECRETS_CONTEXT" "$VARS_CONTEXT+$SECRETS_CONTEXT") |
| 114 | + to_envs() { jq -r "to_entries[] | \"\(.key)=\\\"\(.value)\\\"\n\""; } |
| 115 | + echo "$parsed_vars" | to_envs > ${{ inputs.context }}/.env |
| 116 | +
|
| 117 | + - id: test_frontend |
| 118 | + name: Test Frontend |
| 119 | + working-directory: ${{ inputs.context }} |
| 120 | + run: | |
| 121 | + ${{ inputs.test_frontend_command }} |
| 122 | + |
| 123 | + - id: build_frontend |
| 124 | + name: Build Frontend |
| 125 | + if: ${{ inputs.test_frontend_build }} |
| 126 | + working-directory: ${{ inputs.context }} |
| 127 | + run: | |
| 128 | + case "${{ inputs.package-manager }}" in |
| 129 | + yarn) |
| 130 | + yarn ${{ inputs.build-script-name }} |
| 131 | + ;; |
| 132 | + npm) |
| 133 | + npm run ${{ inputs.build-script-name }} |
| 134 | + ;; |
| 135 | + esac |
| 136 | + |
0 commit comments