Skip to content

Commit

Permalink
Update pipeline.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
youngbryanyu committed Jan 16, 2024
1 parent 60cd042 commit 77290f0
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ jobs:
- name: Compile TypeScript into JavaScript
run: pnpm run build

# detect if there were code changes:
# Detect if there were code changes and save the result to:
# - File artifact
# - Environment variable (only available within the scope of this job, thus need artifact to pass to other jobs)
detect-code-changes:
name: Detect Code Changes
needs: build
Expand All @@ -87,16 +89,19 @@ jobs:
code_changes: ${{ steps.files.outputs.code_changes }}
steps:
- uses: actions/checkout@v3
- name: Determine Changed Files
id: files
- name: Determine Changed Files and Create Artifact
run: |
echo "CODE_CHANGES=true" >> $GITHUB_ENV
if git diff --name-only ${{ github.base_ref }}...${{ github.head_ref }} | grep -qvE '(\.md$|\.github/workflows/)'
then
echo "CODE_CHANGES=true" >> $GITHUB_ENV
echo "true" > code_changes.txt
else
echo "CODE_CHANGES=false" >> $GITHUB_ENV
echo "false" > code_changes.txt
fi
echo "CODE_CHANGES=$(cat code_changes.txt)" >> $GITHUB_ENV
- uses: actions/upload-artifact@v2
with:
name: code-changes-artifact
path: code_changes.txt

# Verify new NPM version is updated during push and PR.
# Skips version number check step if there were code changes
Expand All @@ -114,6 +119,12 @@ jobs:
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org/'
# - uses: actions/download-artifact@v2
# with:
# name: code-changes-artifact # get artifact containing whether there were code changes
# - name: Read Code Changes Flag
# run: |
# echo "CODE_CHANGES=$(cat code_changes.txt)" >> $GITHUB_ENV
- name: Install dependencies
run: npm install
- name: Check if version number is updated
Expand All @@ -126,13 +137,14 @@ jobs:
echo "Version in package.json is already published. Exiting..."
exit 1
fi
# publish to NPM if:
# publish to NPM. Runs the publish step only if:
# - It was a push
# - There were code changes
publish-npm:
name: Publish to NPM
if: github.event_name == 'push' && env.CODE_CHANGES == 'true'
if: github.event_name == 'push'
needs: verify-npm-version
runs-on: ubuntu-latest
steps:
Expand All @@ -144,21 +156,27 @@ jobs:
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org/'
# - uses: actions/download-artifact@v2
# with:
# name: code-changes-artifact # get artifact containing whether there were code changes
# - name: Read Code Changes Flag
# run: |
# echo "CODE_CHANGES=$(cat code_changes.txt)" >> $GITHUB_ENV
- name: Install dependencies
run: npm install
- name: Check if version updated
- name: Get package version number
id: check_version
run: |
LATEST_TAG=$(git describe --tags --abbrev=0)
PACKAGE_VERSION=$(node -p "require('./package.json').version")
echo "::set-output name=version::$PACKAGE_VERSION"
- name: Publish to NPM
if: steps.check_version.outputs.version
if: env.CODE_CHANGES == 'true' && steps.check_version.outputs.version
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

# Create Release on GitHub if:
# Create Release on GitHub if. Runs the release step only if:
# - It was a push
# - There were code changes
release:
Expand All @@ -169,10 +187,17 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3
# - uses: actions/download-artifact@v2
# with:
# name: code-changes-artifact # get artifact containing whether there were code changes
# - name: Read Code Changes Flag
# run: |
# echo "CODE_CHANGES=$(cat code_changes.txt)" >> $GITHUB_ENV
- name: Extract version number
id: package_version
run: echo "::set-output name=VERSION::$(jq -r .version package.json)"
- name: Create Release
if: env.CODE_CHANGES == 'true'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down

0 comments on commit 77290f0

Please sign in to comment.