Search code examples
githubgitlabyaml

Saving version number to package.json in Gitlab yaml


I am able to successfully get the version number from my package.json and increment it, however I need to save the new version back to package.json and save it.

How can I do this in the below script in Gitlab

build_android:
  stage: build
  script:
    - export VERSION_NUMBER=$(node -p "require('./package.json').version")
    - echo "Current version number:" $VERSION_NUMBER
    - export NEW_VERSION_NUMBER=$(echo "$VERSION_NUMBER + 0.0.1" | bc)
    - echo "New version number:" $NEW_VERSION_NUMBER

I have tried adding

 - npm version $NEW_VERSION_NUMBER

to the end but the problem is the package.json is never saved to every subsequent build will use the original build number


Solution

  • With all of these answers, none of them address the issue of a gitlab loop. Each time you check into a repository it will trigger another build of the yaml. You need to add a flag so that the check in will be skipped and the yaml doesn't get fired over and over

    You have to place the [skip ci] flag in your commit message or use the flag -o ci.skip in your git push.

    - git clone https://${GITLAB_USER_LOGIN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git
        - git checkout $CI_COMMIT_REF_NAME
        - npm version minor --no-git-tag-version 
        - git commit -a -m "Bump version [skip ci]"
        - git push "https://${GITLAB_USER_LOGIN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" "HEAD:${CI_COMMIT_REF_NAME}" -o **ci.skip**