Skip to content

Commit

Permalink
Merge pull request #758 from geonetwork/auto-publish-npm-package
Browse files Browse the repository at this point in the history
Automatically publish NPM package on every commit on main branch
  • Loading branch information
jahow authored Jan 10, 2024
2 parents f89e411 + d433250 commit 0f95f70
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
47 changes: 46 additions & 1 deletion .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
build-archive-docker:
needs: checks
if: github.event_name != 'issue_comment' || needs.checks.outputs.shouldRun
name: Build docker images and archives
name: Build and upload docker images and archives
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -108,3 +108,48 @@ jobs:
run: |
docker image ls --format '{{.Repository}}:{{.Tag}}' --filter=reference='geonetwork/*' | \
xargs -r -L1 docker push $1
build-npm-package:
if: github.event_name != 'issue_comment'
name: Build and publish NPM package
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ needs.checks.outputs.ref }} # use the PR head ref if applicable; otherwise keep default behaviour
persist-credentials: false
fetch-depth: 0

- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: npm ci

- name: Adjust package version according to branch & commit
working-directory: package
run: npm version $(../tools/print-dev-version.sh) --no-git-tag-version

- name: Build NPM package
working-directory: package
run: node generate-package.js

- name: Publish NPM package with @dev tag
if: github.event_name != 'release'
working-directory: package/dist
run: npm publish --tag dev
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Publish NPM package
if: github.event_name == 'release'
working-directory: package/dist
run: npm publish --tag latest
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
25 changes: 25 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,28 @@ jobs:
- name: Run tests
run: npx nx run-many --target=e2e

build-npm-package:
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]'
name: Attempt to build the NPM package
runs-on: ubuntu-latest

steps:
- name: Checkout branch
uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0

- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Build NPM package
working-directory: package
run: node generate-package.js
1 change: 1 addition & 0 deletions libs/ui/catalog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './lib/catalog-title/catalog-title.component'
export * from './lib/language-switcher/language-switcher.component'
export * from './lib/organisation-preview/organisation-preview.component'
export * from './lib/organisations-filter/organisations-filter.component'
export * from './lib/organisations-result/organisations-result.component'
1 change: 1 addition & 0 deletions libs/ui/elements/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './lib/pagination/pagination.component'
export * from './lib/related-record-card/related-record-card.component'
export * from './lib/search-results-error/search-results-error.component'
export * from './lib/user-preview/user-preview.component'
export * from './lib/record-api-form/record-api-form.component'
3 changes: 3 additions & 0 deletions libs/ui/inputs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export * from './lib/copy-text-button/copy-text-button.component'
export * from './lib/drag-and-drop-file-input/drag-and-drop-file-input.component'
export * from './lib/navigation-button/navigation-button.component'
export * from './lib/viewport-intersector/viewport-intersector.component'
export * from './lib/checkbox/checkbox.component'
export * from './lib/search-input/search-input.component'
export * from './lib/date-range-picker/date-range-picker.component'
25 changes: 25 additions & 0 deletions tools/print-dev-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# Will print a version tag taking into account the current project version and whether it's a dev version
# on main branch: 1.0.0-dev.1234abcd (commit tag)
# on feature branch: 1.0.0-dev.branch-name
# on git tag: 1.0.0 (git tag)

npmVersion=$(node --print 'require("../package.json").version')
gitTag=$(git describe --exact-match --tags 2>/dev/null | sed "s/^v//") # remove "v" in front of version if any
gitBranch=$(git symbolic-ref --short HEAD)
gitRef=$(git rev-parse --short HEAD)

# git tag defined
if [ -n "${gitTag}" ]; then
echo ${gitTag}
exit 0
fi

# main branch
if [ "${gitBranch}" == "main" ]; then
echo "${npmVersion}.${gitRef}"
exit 0
fi

echo "${npmVersion}.${gitBranch}"

0 comments on commit 0f95f70

Please sign in to comment.