How to print pipeline variables

How do I print pipeline variables? I have this in my configuration:

echo “pipeline.number: <<pipeline.number>>”
echo “pipeline.number2: << pipeline.number >>”

But both are printing the variable itself. It’s not getting interpolated. This is the output I get:
pipeline.number: <<pipeline.number>>
pipeline.number2: << pipeline.number >>

I also tried to print it, following this doc:

Here is my configuration:
build:
working_directory: XXX
macos:
xcode: “14.2”
shell: /bin/bash --login -eo pipefail
environment:
PIPELINE_NUMBER: << pipeline.number >>

And in the run step in the build job, I am printing like this:
echo “pipeline.number: $PIPELINE_NUMBER”
However, it’s printing this:
pipeline.number: << pipeline.number >>

Does anyone have any idea what could be wrong? Any help will be greatly appreciated.

Thanks!

Without seeing your config.yml it is not possible to tell exactly as spacing is rather important when it comes to yml processing.

The first thing that comes to mind is - you are using GitHub or Bitbucket as your VCS as this value is not supported for GitLab.

Below I have posted a simple config.yml example that does retrieve the correct value when run with Bitbucket as the VCS.

config.yml

version: 2.1

executors:
  hosted_node:
    machine:
      image: ubuntu-2004:current
    resource_class: medium
    
jobs:
  build:
    executor: hosted_node
    environment:
       PIPELINE_NUMBER: << pipeline.number >>
    steps:
      - run:
          name: show pipeline number
          command: |
                   echo “pipeline.number <<pipeline.number>>”
                   echo “pipeline.number: $PIPELINE_NUMBER”
        
workflows:
  process:
    jobs:
     - build

the output of the show pipeline number step

#!/bin/bash -eo pipefail
echo “pipeline.number 15”
echo “pipeline.number: $PIPELINE_NUMBER”

“pipeline.number 15”
“pipeline.number: 15”

CircleCI received exit code 0

Thanks for further clarification. I am using Github as the repository.
This is my config.yml file:

version: 3.4.0PP_B
orbs:
node: circleci/node@18.6.0
gradle: circleci/gradle@7.3.3
jobs:
build:
working_directory: ~/XXX
macos:
xcode: “14.2”
resource_class: macos.x86.medium.gen2
shell: /bin/bash --login -eo pipefail
environment:
PIPELINE_NUMBER: << pipeline.number >>

    steps:
        - checkout
        - run:
           name: Print Pipeline Number
           command: |
               echo "pipeline number: <<pipeline.number>>"
               echo "pipeline.number: $PIPELINE_NUMBER"
        - *save_commit_log

Both of these lines print:

pipeline number: <<pipeline.number>>
pipeline.number: << pipeline.number >>
CircleCI received exit code 0

Thanks!

What platform/environment are you running on that allows the version to be set to 3.4.0PP_B ?

The current standard version for current config.yml script processing is 2.1 and if I try and use 3.4.0PP_B as a value on a linux test environment I get “Unsupported or missing workflows config version”. I know nothing about the macos environment, but there is the possibility that it accepts an incorrect value and then defaults to an older parser.

I am using MacOS on Circleci hosting environment. This is the output from their Spin up Environment step:

machine-agent version 1.0.47328-c11e970 (image: “xcode:14.2.0”, public ip: “38.23.39.18”, provider: “Mac Cloud”)
task-agent version 1.0.165681-0a5e66a2
Downloading task-agent: success after 1.830202922s.

Some more info on that environment:

v18.12.1
ARCH: i386
Processor info:
Intel(R) Xeon(R) W-3245 CPU @ 3.20GHz
Mac version:
Software:

System Software Overview:

  System Version: macOS 12.6 (21G115)
  Kernel Version: Darwin 21.6.0
  Boot Volume: macos_vm
  Boot Mode: Normal
  User Name: Distiller (distiller)
  Secure Virtual Memory: Enabled

But in your config.yml do you have

version: 3.4.0PP_B

or

version: 2.1

?

In my configuration file, I have this:

version: 3.4.0PP_B

Please change it to “version: 2.1”

Version is a ‘reserved word’ for the circleci config.yml ‘language’ and it tells the pre-processor what features should be enabled during the processing of the script. I’m not sure why you do not get an error as the only documented valid values are 2, 2.0 and 2.1.

(as in the example I posted)

When I changed it to version 2.1, I got the parse error:
Error calling job: 'build' Unclosed '<<' tag in string: 'echo "CIRCLE_BRANCH: ${CIRCLE_BRANCH}"

for this config:
check_build_env: &check_build_env
run:
name: Check Build Environment
command: |
echo “CIRCLE_BRANCH: ${CIRCLE_BRANCH}”

It’s working perfectly fine with the version I have.

Also, I see this for version 2.1 on Circleci site:

Server v2

End of service notice

CircleCI server v2 will cease to function at the end of March 2023. Server v2 reached EOL April 1, 2022, and has not been supported since that time. We encourage customers still migrating off v2 to reach out to your Customer Service teams.

“version” does not relate to the version of the circleci server, but instead the version of the language features available to the config.yml script.

The fact that you have a parser error is a good thing as it now means that the parser is now trying to process the contents of your config.yml script - I’m still not sure why it was allowing the version number you had entered as it should have error out, but that is something I do not have enough detail to be able to comment on.

Now for the error

I need you to post your config.yml file using the </> option (in the toolbar above the text reply area). This will allow all the white space to be retained in your upload. For .yml files, the white space provides the context for how the text will be processed and without it, I can not provide much feedback.

Using my own example config.yml that I posted a few messages ago with the </> feature, if I was to remove a few spaces or have used a mix of spaces and tabs there is a very good chance that I would get errors reported when I tried to run the script.

Thanks @rit1010 for your input and help! After changing the version to 2.1, it was giving me this parse error:

Unclosed '<<' tag in string: '# echo "CIRCLE_BRANCH: ${CIRCLE_BRANCH}" `` # CIRCLE_BRANCH_PREFIX="echo -n ${CIRCLE_BRANCH} | head -c 14"

I have found out that this line was causing the issue:

echo ‘export build_env_upper=“$(tr ‘[a-z]’ ‘[A-Z]’ <<< $build_env)”’ >> $BASH_ENV

When I replaced the above line with this:

echo ‘export build_env_upper=“$(tr ‘[a-z]’ ‘[A-Z]’ \<<< $build_env)”’ >> $BASH_ENV

The parse error was gone.

Also, I was able to print the variable: pipeline.number

This line is still not interpolating the variable:

echo “pipeline.number: <<pipeline.number>>”

However if I print like this, it works:

deploy_android:
        working_directory: ~/XXX
        macos:
            xcode: "14.2"
            resource_class: macos.x86.medium.gen2
        
        shell: /bin/bash --login -eo pipefail
        environment:
            PIPELINE_NUMBER: << pipeline.number >>
            steps:
               - run:
                    name: Generate Android build
                    command: |
                        echo "pipeline.number: $PIPELINE_NUMBER"