diff --git a/README.md b/README.md index 2f63159..35e79cd 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/config-npm/action.yml b/config-npm/action.yml index cd264b4..9bfb0b8 100644 --- a/config-npm/action.yml +++ b/config-npm/action.yml @@ -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 }} diff --git a/config-npm/config.sh b/config-npm/config.sh index f1f7e78..a80a231 100755 --- a/config-npm/config.sh +++ b/config-npm/config.sh @@ -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() { @@ -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." diff --git a/mise.toml b/mise.toml index 2bc8c22..2250405 100644 --- a/mise.toml +++ b/mise.toml @@ -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" diff --git a/spec/config-npm_spec.sh b/spec/config-npm_spec.sh index e1617ef..be2015b 100755 --- a/spec/config-npm_spec.sh +++ b/spec/config-npm_spec.sh @@ -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()'