From cb74074ff2723041c1d54c679c41375e42c56644 Mon Sep 17 00:00:00 2001 From: Nick Foscarini <50146659+codemile@users.noreply.github.com> Date: Thu, 30 May 2024 12:08:35 -0400 Subject: [PATCH] chore: Adds GitHub actions for PR and merging into main/v2.0.x branches (#22) --- .github/actions/install/README.md | 38 ++++++++++++ .github/actions/install/action.yml | 17 +++++ .github/actions/restore/README.md | 57 +++++++++++++++++ .github/actions/restore/action.yml | 12 ++++ .github/workflows/deploy-artifacts.yml | 86 ++++++++++++++++++++++++++ .github/workflows/pull_request.yml | 81 ++++++++++++++++++++++++ 6 files changed, 291 insertions(+) create mode 100644 .github/actions/install/README.md create mode 100644 .github/actions/install/action.yml create mode 100644 .github/actions/restore/README.md create mode 100644 .github/actions/restore/action.yml create mode 100644 .github/workflows/deploy-artifacts.yml create mode 100644 .github/workflows/pull_request.yml diff --git a/.github/actions/install/README.md b/.github/actions/install/README.md new file mode 100644 index 0000000..9bd18fe --- /dev/null +++ b/.github/actions/install/README.md @@ -0,0 +1,38 @@ +# 📥 Install GitHub Action + +This GitHub Action is designed to install dependencies for your project. It does so by first attempting to cache the `node_modules` directory to improve subsequent installation speeds. If the dependencies have not changed (i.e., the `yarn.lock` file remains unchanged), it retrieves the cached `node_modules`. Otherwise, it performs a fresh install. + +## Features: + +1. 📦 **Caching of `node_modules`**: This step caches the `node_modules` directory to improve the speed of subsequent installations. It uses the `actions/cache@v4` action for this purpose. +2. 📥 **Installation of Dependencies**: If the cache was not hit (i.e., the dependencies or the `yarn.lock` file changed), it installs the dependencies using Yarn. + +## How it Works: + +1. **Cache Key Generation**: The key for the cache is generated using the operating system of the runner and a hash of the `yarn.lock` file. This ensures that the cache is only hit when the `yarn.lock` file remains unchanged across workflow runs. +2. **Cache Retrieval**: If the cache is hit, the `node_modules` directory is restored, skipping the installation step. +3. **Dependency Installation**: If the cache is not hit, it installs the dependencies using `yarn install --frozen-lockfile`. + +## Usage: + +To use this action in your workflow, add the following steps to your `.github/workflows/your-workflow-file.yml`: + +```yaml +name: Your Workflow Name + +on: + push: # or any other GitHub event + branches: + - main + +jobs: + install: + runs-on: ubuntu-latest # or any other runner + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: 📥 Install + uses: ./.github/actions/install +``` diff --git a/.github/actions/install/action.yml b/.github/actions/install/action.yml new file mode 100644 index 0000000..92c61c2 --- /dev/null +++ b/.github/actions/install/action.yml @@ -0,0 +1,17 @@ +name: "📥 Install" +description: "📥 Install dependencies" + +runs: + using: "composite" + steps: + - name: "📦 Cache node_modules" + id: node-modules + uses: actions/cache@v4 + with: + path: '**/node_modules' + key: ${{ runner.os }}-${{ runner.arch }}-node-modules-${{ hashFiles('**/yarn.lock') }} + + - name: "📥 Install dependencies" + if: steps.node-modules.outputs.cache-hit != 'true' + shell: bash + run: yarn install --frozen-lockfile diff --git a/.github/actions/restore/README.md b/.github/actions/restore/README.md new file mode 100644 index 0000000..29ba037 --- /dev/null +++ b/.github/actions/restore/README.md @@ -0,0 +1,57 @@ +# 📦 Node Modules - GitHub Action + +This GitHub Action is designed to restore the `node_modules` cache, improving the speed of workflows by using cached versions of dependencies. It specifically targets Node.js projects and uses the cache based on the content of the `yarn.lock` file. + +## 🚀 Features + +- Uses the `actions/cache` action to manage the caching of `node_modules`. +- Takes into account the OS of the runner to ensure platform-specific dependencies are properly cached. +- Relies on the `yarn.lock` file to ensure that the cache is as up-to-date as your dependencies. + +## 📝 Usage + +To use this action in your workflow, follow the steps below: + +1. Create (if you haven't already) a workflow `.yml` file in your `.github/workflows` directory. +2. Incorporate the `📦 Node Modules` action from the local action path `./.github/actions/restore` as illustrated in the example below: + +```yaml +name: Your Workflow Name + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Use Node.js + uses: actions/setup-node@v2 + with: + node-version: '14' + + - name: 📦 Node Modules + uses: ./.github/actions/restore + + - name: Install dependencies + run: yarn install + + # Add your other steps like running tests, build, etc. +``` + +## 📌 Notes + +- The action uses the `fail-on-cache-miss: true` setting, which means the workflow will fail if the cache can't be restored. Depending on your use-case, you might want to adjust this behavior. + +## 💡 Example + +Considering you've installed the action in the `./.github/actions/restore` directory: + +Your workflow will automatically attempt to restore the `node_modules` cache before installing the dependencies using `yarn install`. If the cache is found and matches the current `yarn.lock`, it will restore the `node_modules` directory from the cache. If not, the workflow will proceed to install the dependencies normally and cache them for future use. + +## 📖 Conclusion + +By utilizing this action, you can potentially save significant time during your workflow runs, especially for projects with a large number of dependencies. Make sure your workflow is set up correctly to make the most out of the caching capabilities provided by this action. diff --git a/.github/actions/restore/action.yml b/.github/actions/restore/action.yml new file mode 100644 index 0000000..7774c47 --- /dev/null +++ b/.github/actions/restore/action.yml @@ -0,0 +1,12 @@ +name: "📦 Node Modules" +description: "🔍 Restore node_modules cache" + +runs: + using: "composite" + steps: + - name: "🔍 Restore node_modules cache" + uses: actions/cache/restore@v4 + with: + path: '**/node_modules' + key: ${{ runner.os }}-${{ runner.arch }}-node-modules-${{ hashFiles('**/yarn.lock') }} + fail-on-cache-miss: true diff --git a/.github/workflows/deploy-artifacts.yml b/.github/workflows/deploy-artifacts.yml new file mode 100644 index 0000000..6c04601 --- /dev/null +++ b/.github/workflows/deploy-artifacts.yml @@ -0,0 +1,86 @@ +name: "🔧 Deploy to GitHub" + +on: + # When you push changes: only affected projects will be build/deploy + push: + branches: + - main + - v2.0.x + # When you manually trigger: all projects will be build/deploy + workflow_dispatch: + +concurrency: "deploy" + +jobs: + install: + runs-on: ubuntu-latest + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + + - name: "📦 Install dependencies" + uses: ./.github/actions/install + + lint: + runs-on: ubuntu-latest + needs: [ install ] + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Lint" + run: yarn lint + + test: + runs-on: ubuntu-latest + needs: [ install ] + if: false + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Test" + run: ${{ env.NX }} affected -t test + + storybooks: + runs-on: ubuntu-latest + needs: [ install ] + if: false + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Build storybooks" + run: yarn build-storybook + + build: + runs-on: ubuntu-latest + needs: [ lint, test, storybooks ] + if: false + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Build projects" + run: yarn build + diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..e7038d8 --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,81 @@ +name: "🔄 Pull Request" + +on: + pull_request: + types: [ opened, synchronize, reopened, converted_to_draft, ready_for_review ] + +concurrency: + group: "${{ github.workflow }}-${{ github.event.pull_request.number }}" + cancel-in-progress: true + +jobs: + install: + runs-on: ubuntu-latest + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + + - name: "📦 Install dependencies" + uses: ./.github/actions/install + + lint: + runs-on: ubuntu-latest + needs: [ install ] + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Lint" + run: yarn lint + + test: + runs-on: ubuntu-latest + needs: [ install ] + if: false + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Test" + run: ${{ env.NX }} affected -t test + + storybooks: + runs-on: ubuntu-latest + needs: [ install ] + if: false + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Build storybooks" + run: yarn build-storybook + + build: + runs-on: ubuntu-latest + needs: [ install ] + steps: + - name: "📥 Checkout code" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: "💽 Restore node_modules cache" + uses: ./.github/actions/restore + + - name: "🔨 Build projects" + run: yarn build