OR13 / mono-repo-action-experiments

https://or13.github.io/mono-repo-action-experiments/a.html
0 stars 0 forks source link

github actions caching in mono repo with lerna #3

Open OR13 opened 2 years ago

OR13 commented 2 years ago

https://github.com/bahmutov/npm-install/issues/28#issuecomment-685163080

# .github/workflows/main.yml
name: Main workflows.
on: push
jobs:
    build-and-test:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout repo.
              uses: actions/checkout@v2
            - name: Set yarn cache directory
              run: echo "::set-env name=YARN_CACHE::$(yarn cache dir)"
            - name: Cache node modules
              id: lerna-yarn-cache
              uses: actions/cache@v2
              env:
                  cache-name: cache-node-modules
              with:
                  path: |
                      ${{ env.YARN_CACHE }}
                      node_modules
                      */*/node_modules
                  key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-build-${{ env.cache-name }}-
                      ${{ runner.os }}-build-
                      ${{ runner.os }}-
            - name: Install dependencies if needed.
              if: steps.lerna-yarn-cache.outputs.cache-hit != 'true'
              run: yarn install --ignore-platform --frozen-lockfile
OR13 commented 2 years ago
on:
  push:
    branches: [main]

name: Cache Example

jobs:
  cache-build:
    name: Build - With Cache
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm ci
      - run: npm run build
      - uses: actions/cache@v2
        id: restore-build
        with:
          path: ./*
          key: ${{ github.sha }}

  cache-use-build:
    name: Re-use Cache
    runs-on: ubuntu-latest
    needs: [cache-build]

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - uses: actions/cache@v2
        id: restore-build
        with:
          path: ./*
          key: ${{ github.sha }}
      - run: ls .next

  artifact-build:
    name: Build - With Artifact
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm ci
      - run: npm run build
      - uses: actions/upload-artifact@v2
        with:
          name: nextjs-build
          path: ./.next

  artifact-use-build:
    name: Re-use Artifact
    runs-on: ubuntu-latest
    needs: [artifact-build]

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - uses: actions/download-artifact@v2
        with:
          name: nextjs-build
      - run: ls .