FIO-7507: publish-dev-tag-to-npm #15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build / Test / Publish | |
on: | |
# push: | |
pull_request: | |
types: [opened, synchronize] | |
env: | |
NODE_VERSION: 18.x | |
jobs: | |
setup: | |
runs-on: ubuntu-latest | |
steps: | |
- run: echo "Triggered by ${{ github.event_name }} event." | |
- name: Check out repository code ${{ github.repository }} on ${{ github.ref }} | |
uses: actions/checkout@v3 | |
- name: Set up Node.js ${{ env.NODE_VERSION }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
cache: 'npm' | |
- name: Cache node modules | |
uses: actions/cache@v3 | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Installing dependencies | |
if: steps.cache.outputs.cache-hit != 'true' | |
uses: borales/actions-yarn@v4 | |
with: | |
cmd: install --frozen-lockfile | |
build: | |
needs: setup | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out repository code ${{ github.repository }} on ${{ github.ref }} | |
uses: actions/checkout@v3 | |
- name: Restore node modules from cache | |
uses: actions/cache@v3 | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Build | |
uses: borales/actions-yarn@v4 | |
with: | |
cmd: build | |
test: | |
needs: setup | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out repository code ${{ github.repository }} on ${{ github.ref }} | |
uses: actions/checkout@v3 | |
- name: Restore node modules from cache | |
uses: actions/cache@v3 | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Test | |
uses: borales/actions-yarn@v4 | |
with: | |
cmd: test | |
publish: | |
if: github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'synchronize') | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Fetch all history for all branches and tags | |
- name: Set up Node.js ${{ env.NODE_VERSION }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
cache: 'npm' | |
- name: Restore node modules from cache | |
uses: actions/cache@v3 | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Prepare version for publish | |
run: | | |
PR_NUMBER=$(echo ${{ github.event.number }}) | |
CURRENT_VERSION=$(node -p "require('./package.json').version") | |
# Explicitly fetch the master branch with its history | |
git fetch origin master:refs/remotes/origin/master | |
# Verify if master branch was fetched | |
if git rev-parse --verify origin/master; then | |
echo "Master branch fetched successfully." | |
# Use git log to count commits if git rev-list does not work as expected | |
COMMIT_COUNT=$(($(git log master..HEAD --oneline | wc -l) - 1)) | |
NEW_VERSION="${CURRENT_VERSION}-dev.${PR_NUMBER}.${COMMIT_COUNT}" | |
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV | |
else | |
echo "Failed to fetch master branch." | |
# Handle error or fallback scenario | |
fi | |
- name: Echo version for testing | |
run: | | |
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
echo "Version to publish: $NEW_VERSION" |