'Command not found on gitlab ci using cat

I have a Gitlab job in which I get a value from a .txt file. This value (v100322.1) was written into the text file in a previous stage and passing by to the job through artifacts. When I try to get value from the file with cat command I get this error on the pipeline:

$ $PACKAGE_VERSION=$(cat build.txt)
+++ cat build.txt
++ $'=\377\376v100322.1\r'
bash: line 132: $'=\377\376v100322.1\r': command not found

And this is my YAML file for GitLab-CI:

stages:
  - deploy
  - trigger

.deploy_job_base:
  stage: deploy
  tags:
    - dotnet
  script:
    - $PACKAGE_VERSION="v100322.1"
    - ${PACKAGE_VERSION} > build.txt
  artifacts:
    expire_in: 1 week
    paths:
      - build.txt
  allow_failure: false

deploy_job_sport:
  extends: .deploy_job_base

deploy_job_TestClient:
  extends: .deploy_job_base

# trigger GitLab API call
.trigger_base:
  stage: trigger
  script:
    - $PACKAGE_VERSION=$(cat build.txt)
    - 'curl --include --fail --request POST --form "token=$CI_JOB_TOKEN" --form "PACKAGE_VERSION=$PACKAGE_VERSION" --form "ref=feature/1000" $GITLAB_BASE_URL/api/v4/projects/$APP_PROJECT_ID/trigger/pipeline'

trigger_sport:
  extends: .trigger_base
  variables:
    APP_PROJECT_ID: "2096"
  needs: [deploy_job_sport]
  dependencies:
    - deploy_job_sport

trigger_TestClient:
  extends: .trigger_base
  variables:
    APP_PROJECT_ID: "2110"
  needs: [deploy_job_TestClient]
  dependencies:
    - deploy_job_TestClient

Do you know which is the problem here? Thanks in advance.



Solution 1:[1]

The cause is the syntax, you can always check it on a virtual box, or better, pull the docker image down that the job will be executed on, and test the script in there

(using docker run -it ${JOB_DOCKER_IMAGE} /bin/bash for instance).

I just tested your script and got this:
enter image description here

you can clearly see bash will not like the $ in front of PACKAGE_VERSION and interpret it, like a command ...

But you can turn the script of .deploy_job_base into a one-liner like this:

enter image description here

you circumnavigate the need for defining a variable and just dump it in the build.txt file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VeRo