1

I need to find a way to keep 'version' intact in package.json of the 'dev' branch after merging 'feature' branch into 'dev'. I can't figure out the best way to do it. Can I do this just using .gitlab-ci.yml or a combination of bash script and gitlab yml. Note, 'feature' branch will always have differnt 'version' in package.json to the dev's package.json.

I tried using bash and assigning a variable to package.json 'version', then after merge updating package.json with pre-merge version. Doesn't seem like a good solution.

hypnobluez
  • 11
  • 2

2 Answers2

0

One way would be to not version package.json (ignore it), but to generate it (with the right value in it.

The generation script can determine the name of the checked out branch with:

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

That means you could:

  • version only a template file package.json.tpl
  • version value files named after the branches: version.dev, version.feature: since they are different, there is no merge issue when merging or switching branches.

Finally, you would register (in a .gitattributes declaration) a content filter driver.

smudge (image from "Customizing Git - Git Attributes", from "Pro Git book")

The smudge script, associated to the template file (package.json.tpl), would generate (automatically on git checkout) the actual package.json file by looking values in the right version.<branch> value file.
The generated actual package.json file remains ignored (by the .gitignore).

See a complete example at "git smudge/clean filter between branches".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Here is what I came up with: My script.sh:

#!/usr/bin/env bash
echo 'Current gitlab user and email'

echo $GITLAB_USER_NAME
echo $GITLAB_USER_EMAIL

git config user.email $GITLAB_USER_EMAIL
git config user.name $GITLAB_USER_NAME
git fetch --all

git checkout $CI_COMMIT_REF_NAME

echo 'Getting version from package.json master'
git checkout origin/master -- package.json
VERSION_DEV=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| tr -d '[[:space:]]')
git status
git reset
git checkout .

echo $VERSION_DEV
echo 'replacing version in package.json'
sed -i 's/\"version\":.*/\"version\": "'$VERSION_DEV'",/g' "package.json"
git add .
git commit -m "package.json version preserved"

# Success
echo "-------------------------------------------------------------------------"
echo "Success in preserving package.json version"


echo 'Checking out master'
git checkout master

echo 'Merging into master'
git merge $CI_COMMIT_REF_NAME --no-ff --no-edit
git push --follow-tags origin  master

my .gitlab-ci.yml:

job-merge:
  before_script:
    - git remote set-url origin https://oauth2:${CI_PUSH_TOKEN}@gitlab.com/mysername/myrepo.git
  script:
    - echo 'STARTING TO RUN SCRIPTS'
    - bash ./script.sh
  only:
    - merge_requests

CI_PUSH_TOKEN is a variable in CI/CD settings of the project that is personal access token with api scope.

hypnobluez
  • 11
  • 2