|
| 1 | +name: Build Pipeline # A Build Pipeline which will use for build, test and deploy. |
| 2 | + |
| 3 | +on: |
| 4 | + push: # When we push the changes. |
| 5 | + branches: # Only for these branches. |
| 6 | + - master |
| 7 | + - bugfix/* |
| 8 | + - hotfix/* |
| 9 | + - release/* |
| 10 | + paths-ignore: # Ignoring the markdown file changes. |
| 11 | + - '**/*.md' |
| 12 | + pull_request: # Also on pull request events. |
| 13 | + workflow_dispatch: # Allows you to run this workflow manually from the Actions tab. |
| 14 | + |
| 15 | +jobs: |
| 16 | + check: |
| 17 | + name: State Verifier |
| 18 | + runs-on: ubuntu-latest |
| 19 | + outputs: |
| 20 | + skip_ci: ${{ steps.check_initial_commit.outputs.skip_ci }} |
| 21 | + steps: |
| 22 | + # The first step is to check out the repository code |
| 23 | + - name: Checking out repository code |
| 24 | + uses: actions/checkout@v4.1.1 # Action for checking out a repo. |
| 25 | + with: |
| 26 | + fetch-depth: 0 # Fetches all history for all branches and tags |
| 27 | + |
| 28 | + # The second step checks whether the commit is the initial commit |
| 29 | + - name: Check Initial Commit |
| 30 | + id: check_initial_commit |
| 31 | + run: | |
| 32 | + # Use a git command to count the number of revisions |
| 33 | + # If the count is 1, then this is the initial commit |
| 34 | + if [ "$(git rev-list --count HEAD)" -eq 1 ]; then |
| 35 | + echo "This is the initial commit." |
| 36 | + # Set the environment variable "skip_ci" to true, signifying CI should not run for the initial commit |
| 37 | + echo "skip_ci=true" >> $GITHUB_OUTPUT |
| 38 | + else |
| 39 | + # If the count is not 1, this is not the initial commit |
| 40 | + # Set the environment variable "skip_ci" to false, signifying CI should run |
| 41 | + echo "skip_ci=false" >> $GITHUB_OUTPUT |
| 42 | + fi |
| 43 | +
|
| 44 | + build: # Job named 'build' |
| 45 | + name: Build & Test |
| 46 | + if: needs.check.outputs.skip_ci != 'true' |
| 47 | + runs-on: ubuntu-latest # The type of machine to run the job on. |
| 48 | + |
| 49 | + needs: [check] |
| 50 | + |
| 51 | + strategy: # Allows you to create a matrix for job configuration. |
| 52 | + matrix: |
| 53 | + node-version: [18.x, 19.x, 20.x] # Running tests across different environments. |
| 54 | + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ |
| 55 | + |
| 56 | + steps: # The sequence of tasks that make up a job. |
| 57 | + - name: Checking out repository code |
| 58 | + uses: actions/checkout@v4.1.1 # Action for checking out a repo. |
| 59 | + |
| 60 | + - name: Setup Node.js ${{ matrix.node-version }} Environment |
| 61 | + uses: actions/setup-node@v4.0.0 # Action for setting up Node environment. |
| 62 | + with: |
| 63 | + node-version: ${{ matrix.node-version }} |
| 64 | + |
| 65 | + - name: Install pnpm package manager |
| 66 | + uses: pnpm/action-setup@v2.4.0 # Action for setting up pnpm. |
| 67 | + id: pnpm-install |
| 68 | + with: |
| 69 | + version: ^8 |
| 70 | + run_install: false |
| 71 | + |
| 72 | + - name: Capture pnpm store directory |
| 73 | + id: pnpm-cache |
| 74 | + run: | |
| 75 | + echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT |
| 76 | +
|
| 77 | + - name: Cache pnpm Store |
| 78 | + uses: actions/cache@v3.3.2 # Action provides caching dependencies and build outputs to improve workflow execution time. |
| 79 | + with: |
| 80 | + path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} # The path of the directory to cache. |
| 81 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} # An explicit key for restoring and saving the cache. |
| 82 | + restore-keys: | |
| 83 | + ${{ runner.os }}-pnpm-store- |
| 84 | +
|
| 85 | + # Installs all dependencies specified in the project's package.json file. |
| 86 | + - name: Install dependencies using pnpm |
| 87 | + run: pnpm install |
| 88 | + |
| 89 | + # This compiles the application in optimized production mode and output it to the build folder. |
| 90 | + - name: Build the application and export it |
| 91 | + run: pnpm run build |
| 92 | + |
| 93 | + # Runs unit tests for the application using Jest. |
| 94 | + - name: Execute tests using Jest |
| 95 | + run: pnpm run test |
| 96 | + |
| 97 | + release: |
| 98 | + name: Create Release |
| 99 | + # Specify the type of the runner the job will run on |
| 100 | + runs-on: ubuntu-latest |
| 101 | + |
| 102 | + needs: [build] |
| 103 | + |
| 104 | + if: ${{ github.ref_name == 'master' }} |
| 105 | + |
| 106 | + # Set permissions to write contents |
| 107 | + permissions: |
| 108 | + contents: write |
| 109 | + |
| 110 | + steps: |
| 111 | + # Checkout the repository code |
| 112 | + - name: Checkout code |
| 113 | + uses: actions/checkout@v4.1.1 |
| 114 | + with: |
| 115 | + fetch-depth: 0 # Fetches all history for all branches and tags |
| 116 | + |
| 117 | + # Generate a changelog for the new release using Git |
| 118 | + - name: Generate a changelog |
| 119 | + uses: orhun/git-cliff-action@v2.1.1 |
| 120 | + id: git-cliff |
| 121 | + with: |
| 122 | + config: cliff.toml # The configuration file for git-cliff |
| 123 | + args: -vv --latest --strip all # Show verbose output, grab the latest changes, and strip unnecessary details |
| 124 | + env: |
| 125 | + OUTPUT: CHANGES.md # The output file for the changelog |
| 126 | + |
| 127 | + # Prepare release notes by processing the generated changelog |
| 128 | + - name: Set the release info |
| 129 | + id: release |
| 130 | + shell: bash |
| 131 | + run: | |
| 132 | + version=$(jq -r '.version' package.json) |
| 133 | + echo "version=${version}" >> $GITHUB_OUTPUT |
| 134 | +
|
| 135 | + # Read contents of changelog into variable 'changelog_content' |
| 136 | + changelog=$(cat ${{ steps.git-cliff.outputs.changelog }}) |
| 137 | + # Remove first two lines from 'changelog' |
| 138 | + changelog="$(printf "$changelog" | tail -n +3)" |
| 139 | + # Save the value of 'changelog' back into the GitHub environment output |
| 140 | + { |
| 141 | + echo "notes<<EOF" |
| 142 | + echo "$changelog" |
| 143 | + echo "EOF" |
| 144 | + } >> $GITHUB_OUTPUT |
| 145 | +
|
| 146 | + # Create a new GitHub release using the gathered information |
| 147 | + - name: Create the release |
| 148 | + uses: nekofar/create-github-release@v1.0.12 |
| 149 | + with: |
| 150 | + tag: v${{ steps.release.outputs.version }} # The name of the tag to be released |
| 151 | + title: v${{ steps.release.outputs.version }} # The title for the release |
| 152 | + notes: ${{ steps.release.outputs.notes }} # The release notes generated in the previous step |
| 153 | + draft: true # The release will be created as a draft |
| 154 | + prerelease: ${{ contains(steps.release.outputs.version, '-rc') || contains(steps.release.outputs.version, '-beta') || contains(steps.release.outputs.version, '-alpha') }} # Conditions to mark the release as a pre-release |
| 155 | + |
| 156 | +concurrency: # Allows controlling the concurrency level of the job in the build pipeline. |
| 157 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 158 | + cancel-in-progress: true # If enabled, previous runs of this workflow for the same group-key will be cancelled while this build or run is in progress. |
0 commit comments