Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ artifactory {
## `config-npm`

Configure NPM and JFrog build environment with build number, authentication, and settings.
Set the project version in `package.json` with the build number.

Set the project version in `package.json` with the build number if the file exists.

> **Note:** This action automatically calls [`get-build-number`](#get-build-number) to manage the build number.

Expand Down
12 changes: 12 additions & 0 deletions config-npm/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,20 @@ runs:
ls -la .actions/*
echo "::endgroup::"

- name: Check for package.json
id: check_package_json
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
if [[ -f "package.json" ]]; then
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi

- uses: ./.actions/get-build-number
id: get_build_number
if: ${{ steps.check_package_json.outputs.exists == 'true' }}
with:
host-actions-root: ${{ steps.set-path.outputs.host_actions_root }}

Expand Down
7 changes: 6 additions & 1 deletion config-npm/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ PACKAGE_JSON="package.json"
source "$(dirname "${BASH_SOURCE[0]}")/../shared/common-functions.sh"

: "${ARTIFACTORY_URL:?}" "${ARTIFACTORY_ACCESS_TOKEN:?}"
: "${BUILD_NUMBER:?}"
: "${GITHUB_REPOSITORY:?}" "${GITHUB_OUTPUT:?}" "${GITHUB_ENV:?}"

set_build_env() {
Expand All @@ -44,6 +43,12 @@ check_version_format() {
}

set_project_version() {
if [[ ! -f "$PACKAGE_JSON" ]]; then
echo "No $PACKAGE_JSON file. Skipping project version update."
return 0
fi

: "${BUILD_NUMBER:?}"
echo "Setting project version..."
if [[ -n "${CURRENT_VERSION:-}" && -n "${PROJECT_VERSION:-}" ]]; then
echo "Using provided CURRENT_VERSION $CURRENT_VERSION and PROJECT_VERSION $PROJECT_VERSION without changes."
Expand Down
1 change: 1 addition & 0 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pre-commit = "4.2.0"
shellcheck = "0.10.0"
shellspec = "0.28.1"
jfrog-cli = "2.77.0"
"npm:markdownlint-cli" = "0.39.0"
12 changes: 12 additions & 0 deletions spec/config-npm_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ Describe 'set_project_version()'
The line 2 should equal "CURRENT_VERSION=1.2-SNAPSHOT (from package.json)"
The line 3 should equal "Replacing version 1.2-SNAPSHOT with 1.2.0-42"
End

It 'skips version update when package.json does not exist'
mv package.json package.json.bak
export BUILD_NUMBER="42"
export GITHUB_REF_NAME="main"
When call set_project_version
The status should be success
The line 1 should equal "No package.json file. Skipping project version update."
The variable CURRENT_VERSION should be undefined
The variable PROJECT_VERSION should be undefined
mv package.json.bak package.json
End
End

Describe 'main()'
Expand Down
Loading