diff --git a/README.md b/README.md index 0ce22de..c22e842 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,40 @@ # Action Template -GitHub action to echo "Hello World" +Yyarn Install composite action for yarn 3/4+ and "nodeLinker: node-modules" -Template repository for reusable GitHub actions - -You can use this guide to figure out how to update your application using the template: [Creating a new repository from a template](https://amuniversal.atlassian.net/wiki/spaces/TD/pages/3419832336/Creating+a+New+GitHub+Repository#Creating-a-new-repository-from-a-template) +Reference: https://gist.github.com/belgattitude/042f9caf10d029badbde6cf9d43e400a ## Getting Started ```bash -git clone https://github.com/Andrews-McMeel-Universal/action_template +git clone https://github.com/Andrews-McMeel-Universal/cache-yarn-install ``` ## Installation -To make `action_template` a part of your workflow, just add a step to one of your workflows in your `.github/workflows/` directory in your GitHub repository. +To make `cache-yarn-install` a part of your workflow, just add a step to one of your workflows in your `.github/workflows/` directory in your GitHub repository. + +> Requirement: @setup/node should be run before + +Example: +```YAML +- name: Cache Yarn Install + uses: Andrews-McMeel-Universal/cache-yarn-install@v1 + with: + enable-corepack: false + cwd: ${{ github.workspace }}/apps/my-app + cache-prefix: add cache key prefix + cache-node-modules: false + cache-install-state: false +``` ## Options | Variable | Description | Required | `[Default]` | | -------- | ----------------- | :------: | --------------- | -| `input` | Input Description | x | `Default Value` | +| `cwd` | Changes node's process.cwd() if the project is not located on the root. Default to process.cwd() | | `.` | +| `cache-prefix` | Add a specific cache-prefix | | `'default'` | +| `cache-npm-cache` | Cache npm global cache folder often used by node-gyp, prebuild binaries (invalidated on lock/os/node-version) | | `'true'` | +| `cache-node-modules` | Cache node_modules, might speed up link step (invalidated lock/os/node-version/branch) | | `'false'` | +| `cache-install-state` | Cache yarn install state, might speed up resolution step when node-modules cache is activated (invalidated lock/os/node-version/branch) | | `'false'` | +| `enable-corepack` | Enable corepack | | `'true'` | diff --git a/action.yaml b/action.yaml index 45771cd..da5e6cf 100644 --- a/action.yaml +++ b/action.yaml @@ -1,12 +1,94 @@ -name: Default GitHub Action +name: 'Cache Yarn Install' +description: 'Run yarn install with node_modules linker and cache enabled' +inputs: + cwd: + description: "Changes node's process.cwd() if the project is not located on the root. Default to process.cwd()" + required: false + default: '.' + cache-prefix: + description: 'Add a specific cache-prefix' + required: false + default: 'default' + cache-npm-cache: + description: 'Cache npm global cache folder often used by node-gyp, prebuild binaries (invalidated on lock/os/node-version)' + required: false + default: 'true' + cache-node-modules: + description: 'Cache node_modules, might speed up link step (invalidated lock/os/node-version/branch)' + required: false + default: 'false' + cache-install-state: + description: 'Cache yarn install state, might speed up resolution step when node-modules cache is activated (invalidated lock/os/node-version/branch)' + required: false + default: 'false' + enable-corepack: + description: 'Enable corepack' + required: false + default: 'true' -description: Default GitHub Action that outputs "Hello World" - -branding: - color: purple - icon: unlock runs: - using: "composite" + using: 'composite' steps: - - run: echo "Hello World" >> $GITHUB_OUTPUT + - name: Enable Corepack + if: inputs.enable-corepack == 'true' + shell: bash + working-directory: ${{ inputs.cwd }} + run: corepack enable + + - name: Expose yarn config as "$GITHUB_OUTPUT" + id: yarn-config + shell: bash + working-directory: ${{ inputs.cwd }} + env: + YARN_ENABLE_GLOBAL_CACHE: 'false' + run: | + echo "CACHE_FOLDER=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT + echo "CURRENT_NODE_VERSION="node-$(node --version)"" >> $GITHUB_OUTPUT + echo "CURRENT_BRANCH=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's,/,-,g')" >> $GITHUB_OUTPUT + echo "NPM_GLOBAL_CACHE_FOLDER=$(npm config get cache)" >> $GITHUB_OUTPUT + + - name: Restore yarn cache + uses: actions/cache@v3 + id: yarn-download-cache + with: + path: ${{ steps.yarn-config.outputs.CACHE_FOLDER }} + key: yarn-download-cache-${{ inputs.cache-prefix }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} + restore-keys: | + yarn-download-cache-${{ inputs.cache-prefix }}- + + - name: Restore node_modules + if: inputs.cache-node-modules == 'true' + id: yarn-nm-cache + uses: actions/cache@v3 + with: + path: ${{ inputs.cwd }}/**/node_modules + key: yarn-nm-cache-${{ inputs.cache-prefix }}-${{ runner.os }}-${{ steps.yarn-config.outputs.CURRENT_NODE_VERSION }}-${{ steps.yarn-config.outputs.CURRENT_BRANCH }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} + + - name: Restore global npm cache folder + if: inputs.cache-npm-cache == 'true' + id: npm-global-cache + uses: actions/cache@v3 + with: + path: ${{ steps.yarn-config.outputs.NPM_GLOBAL_CACHE_FOLDER }} + key: npm-global-cache-${{ inputs.cache-prefix }}-${{ runner.os }}-${{ steps.yarn-config.outputs.CURRENT_NODE_VERSION }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} + + - name: Restore yarn install state + if: inputs.cache-install-state == 'true' && inputs.cache-node-modules == 'true' + id: yarn-install-state-cache + uses: actions/cache@v3 + with: + path: ${{ inputs.cwd }}/.yarn/ci-cache + key: yarn-install-state-cache-${{ inputs.cache-prefix }}-${{ runner.os }}-${{ steps.yarn-config.outputs.CURRENT_NODE_VERSION }}-${{ steps.yarn-config.outputs.CURRENT_BRANCH }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} + + - name: Install dependencies shell: bash + working-directory: ${{ inputs.cwd }} + run: yarn install --immutable --inline-builds + env: + # Overrides/align yarnrc.yml options (v3, v4) for a CI context + YARN_ENABLE_GLOBAL_CACHE: 'false' # Use local cache folder to keep downloaded archives + YARN_ENABLE_MIRROR: 'false' # Prevent populating global cache for caches misses (local cache only) + YARN_NM_MODE: 'hardlinks-local' # Reduce node_modules size + YARN_INSTALL_STATE_PATH: '.yarn/ci-cache/install-state.gz' # Might speed up resolution step when node_modules present + # Other environment variables + HUSKY: '0' # By default do not run HUSKY install \ No newline at end of file