Skip to content

Commit

Permalink
ci: change runners
Browse files Browse the repository at this point in the history
  • Loading branch information
Turito committed Oct 21, 2024
1 parent c5fdd6c commit d9f7ad5
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 29 deletions.
57 changes: 57 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Getting Cache
description: 'Composite action to share cache fetching between workflows'

inputs:
PREFIX_CACHE_KEY:
description: 'Supported string values. Used as prefix to generate a cache key'
required: false
NODE_MODULES:
description: 'Supported boolean values. Used to know if should manage the NODE MODULES cache'
required: false
NPM:
description: 'Supported boolean values. Used to know if should manage the NPM cache'
required: false

outputs:
cache_node_modules:
description: 'Used to know if exist a NODE_MODULES cache'
value: ${{ steps.cache_node_modules.outputs.cache-hit }}

runs:
using: composite

steps:
- name: 'Set unify package version'
id: package
shell: sh
run: npm version -no-git-tag-version 0.0.0

- name: 'Get NPM cache directory path'
id: cache_path
shell: sh
run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT

- name: 'Get cache NODE MODULES dependencies'
id: cache_node_modules
if: inputs.NODE_MODULES == 'true'
uses: actions/cache@v4
with:
path: ./node_modules
key: ${{ inputs.PREFIX_CACHE_KEY }}-node_modules-${{ hashFiles('**/package-lock.json') }}

- name: 'Get cache NPM dependencies'
id: cache_npm
if: inputs.NPM == 'true' && steps.cache_node_modules.outputs.cache-hit != 'true'
uses: actions/cache@v4
with:
path: |
~/.npm
${{ steps.cache_path.outputs.dir }}
key: ${{ inputs.PREFIX_CACHE_KEY }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ inputs.PREFIX_CACHE_KEY }}-npm-
${{ inputs.PREFIX_CACHE_KEY }}-
- name: 'Undo package version changes'
shell: sh
run: git checkout -- .
20 changes: 20 additions & 0 deletions .github/actions/increment-version/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Increment Version

outputs:
version:
description: "New version"
value: ${{ steps.project.outputs.version }}
previous:
description: "Old version"
value: ${{ steps.project.outputs.previous }}

runs:
using: composite

steps:
- name: 'Increment version'
id: project
shell: sh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ${GITHUB_ACTION_PATH}/increment-version.sh
53 changes: 53 additions & 0 deletions .github/actions/increment-version/increment-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

set -e

# GET CURRENT VERSION
CURRENT_BRANCH_VERSION=$(node -p "require('./package.json').version")

echo "Current version: v$CURRENT_BRANCH_VERSION"
echo previous=$CURRENT_BRANCH_VERSION >> $GITHUB_OUTPUT
echo version=$CURRENT_BRANCH_VERSION >> $GITHUB_OUTPUT

# GET LAST RELEASE VERSION AND COMMITS
git fetch --tags
LAST_RELEASE_TAG=$(git tag -l "v*" | sort -V | grep -E "^v[0-9]+(\.[0-9]+)*$" | tail -1)
if [ -z "$LAST_RELEASE_TAG" ]; then
LAST_RELEASE_VERSION="0.0.0"
echo "There is no last release, starting from v$LAST_RELEASE_VERSION"
LAST_RELEASE_ID=$(git rev-list --max-parents=0 HEAD)
else
LAST_RELEASE_VERSION=$(expr "$LAST_RELEASE_TAG" : "v\(.*\)")
echo "Last released version: v$LAST_RELEASE_VERSION"
LAST_RELEASE_ID=$LAST_RELEASE_TAG
fi

COMMITS=$(git log --pretty=format:"%B" "$LAST_RELEASE_ID"..)
if [ "$COMMITS" == "" ]; then
echo "No changes from the last version ($LAST_RELEASE_VERSION). No version number change required."
exit 0;
fi

# FIGURE OUT NEXT VERSION
if echo "$COMMITS" | grep -qE "^BREAKING[ -]CHANGE: "; then
CURRENT_MAJOR=$(echo "$LAST_RELEASE_VERSION" | sed -E "s|^([0-9]+)\.([0-9]+)\.([0-9]+)|\1|")
NEXT_VERSION=$(echo "$LAST_RELEASE_VERSION" | sed -E "s|([0-9]+)\.([0-9]+)\.([0-9]+)|$(( CURRENT_MAJOR + 1 )).0.0|")
elif echo "$COMMITS" | grep -qE "^feat: |^feat\(.*\): "; then
CURRENT_MINOR=$(echo "$LAST_RELEASE_VERSION" | sed -E "s|^([0-9]+)\.([0-9]+)\.([0-9]+)|\2|")
NEXT_VERSION=$(echo "$LAST_RELEASE_VERSION" | sed -E "s|([0-9]+)\.([0-9]+)\.([0-9]+)|\1.$(( CURRENT_MINOR + 1 )).0|")
else
CURRENT_PATCH=$(echo "$LAST_RELEASE_VERSION" | sed -E "s|^([0-9]+)\.([0-9]+)\.([0-9]+)|\3|")
NEXT_VERSION=$(echo "$LAST_RELEASE_VERSION" | sed -E "s|([0-9]+)\.([0-9]+)\.([0-9]+)|\1.\2.$(( CURRENT_PATCH + 1 ))|")
fi

versions=$(printf '%s\n%s' "$NEXT_VERSION" "$CURRENT_BRANCH_VERSION")
if [ "$NEXT_VERSION" == "$CURRENT_BRANCH_VERSION" ] || [ "$versions" == "$(sort -V <<< "$versions")" ]; then
echo "Version is updated, no changes required!" >&2
exit 0
fi

echo "Suggested new version: $NEXT_VERSION"
echo version=$NEXT_VERSION >> $GITHUB_OUTPUT

## UPDATE VERSION
npm version -no-git-tag-version --force $NEXT_VERSION
14 changes: 2 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ concurrency:

jobs:
ci:
runs-on: [self-hosted, x]
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
Expand All @@ -21,28 +21,18 @@ jobs:
steps:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- name: Load Motive Shared Actions
uses: daspn/private-actions-checkout@v2
with:
app_id: ${{ secrets.MOTIVE_PRIVATE_ACTIONS_APP_ID }}
app_private_key: ${{ secrets.MOTIVE_PRIVATE_ACTIONS_APP_PRIVATE_KEY }}
configure_git: true
actions_list: '[ "empathyco/platform-motive-ci-library@v5" ]'
checkout_base_path: ./.loaded_actions/actions

- name: Set Node version
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4
with:
node-version-file: .nvmrc

- name: Setup Node cache
id: cache
uses: ./.loaded_actions/actions/platform-motive-ci-library/node/cache
uses: ./.github/actions/cache
with:
PREFIX_CACHE_KEY: ${{ runner.os }}
NODE_MODULES: 'true'
NPM: 'true'
CUSTOM_CACHE: 'false'

- name: Install dependencies
if: steps.cache.outputs.cache_node_modules != 'true'
Expand Down
21 changes: 4 additions & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ concurrency: release

jobs:
release:
runs-on: [self-hosted, x]
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
Expand All @@ -32,34 +32,21 @@ jobs:
token: ${{ steps.app-token.outputs.token }}
fetch-depth: 0

- name: Load Motive Shared Actions
uses: daspn/private-actions-checkout@v2
with:
app_id: ${{ secrets.MOTIVE_PRIVATE_ACTIONS_APP_ID }}
app_private_key: ${{ secrets.MOTIVE_PRIVATE_ACTIONS_APP_PRIVATE_KEY }}
configure_git: true
actions_list: '[ "empathyco/platform-motive-ci-library@v5" ]'
checkout_base_path: ./.loaded_actions/actions

- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4
with:
node-version-file: .nvmrc

- name: Setup CACHE
- name: Setup Node cache
id: cache
uses: ./.loaded_actions/actions/platform-motive-ci-library/node/cache
uses: ./.github/actions/cache
with:
PREFIX_CACHE_KEY: ${{ runner.os }}
NODE_MODULES: 'true'
NPM: 'true'
CUSTOM_CACHE: 'false'

- name: Increment and set new version
id: increment
uses: ./.loaded_actions/actions/platform-motive-ci-library/motive/increment-version
with:
PROJECT_TYPE: node
RELEASED_VERSION_SOURCE: tag
uses: ./.github/actions/increment-version

- name: Install dependencies
if: steps.cache.outputs.cache_node_modules != 'true'
Expand Down

0 comments on commit d9f7ad5

Please sign in to comment.