1

I'm trying to change a version of package.json with git TAG generated using jq. For that I need to use double quotes inside a single quote passing a gitlab variable as parameter. The command is jq -r '.version |= "${TAG}"' temp.json > package.json but the ${TAG} or $TAG is not resolving to its value.

build-cloudfront:
  stage: build
  image: node:18
  variables:
    TAG: $CI_COMMIT_TAG
  script:
    - apt-get update
    - apt-get install jq -y
    - mv package.json temp.json
    - jq -r '.version |= "${TAG}"' temp.json > package.json
    - npm install
    - npm run build
    - echo "FRONTEND BUILD SUCCESSFULY"
  artifacts:
    paths:
      - dist/
    expire_in: "10 mins"
  # Run this job for tags
  only:
    - tags

Resolve gitlab variable.

Jakob Liskow
  • 1,323
  • 14
  • 22

1 Answers1

0

For using a bash-variable in jq's filter you need to bind the variable as argument e.g. --arg name $variable or --argjson name JSON-text (jq manual)

In your case you could write following.

jq -r --arg TEMP_TAG "$TAG" '.version |= "${TEMP_TAG}"' temp.json > package.json

futher question and possible duplicate question: Passing bash variable to jq

Jakob Liskow
  • 1,323
  • 14
  • 22