GitLab CI流水线在单引号内有双引号时无法解析变量,未解析。

huangapple go评论69阅读模式
英文:

Gilab-ci pipeline not resolving variables not resolved when there is a double quotes inside single quote

问题

The command to resolve the GitLab variable within your GitLab CI/CD script should be as follows:

jq -r ".version |= \"$TAG\"" temp.json > package.json

This command uses double quotes to properly substitute the value of the $TAG variable within the jq command.

英文:

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.

答案1

得分: 0

以下是翻译好的内容:

要在 jq 的过滤器中使用 bash 变量,你需要将该变量绑定为参数,例如 --arg name $variable--argjson name JSON-textjq 手册

在你的情况下,你可以这样写:

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

更多问题和可能的重复问题:
https://stackoverflow.com/questions/40027395/passing-bash-variable-to-jq

英文:

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:
https://stackoverflow.com/questions/40027395/passing-bash-variable-to-jq

huangapple
  • 本文由 发表于 2023年6月30日 00:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定