Skip to content

Commit

Permalink
chore: Adds GitHub actions for PR and merging into main/v2.0.x branch…
Browse files Browse the repository at this point in the history
…es (#22)
  • Loading branch information
codemile authored May 30, 2024
1 parent 65f4ed8 commit cb74074
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/actions/install/README.md
Original file line number Diff line number Diff line change
@@ -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
```
17 changes: 17 additions & 0 deletions .github/actions/install/action.yml
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions .github/actions/restore/README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions .github/actions/restore/action.yml
Original file line number Diff line number Diff line change
@@ -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
86 changes: 86 additions & 0 deletions .github/workflows/deploy-artifacts.yml
Original file line number Diff line number Diff line change
@@ -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

81 changes: 81 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cb74074

Please sign in to comment.