Skip to main content

Set a dated file name using environment variables

How to set a dated file name using workspaces and environment variables

You can use workspaces and environment variables to include a date in the filename.

For this, we will make use of the date and datetime commands, which come as standard in most *nix systems.

You can find an example `config.yml` file here:

# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/reference/configuration-reference/version: 2.1jobs:
 say-date:
   docker:
     - image: cimg/base:stable
   parameters:
     date:
       type: string
       default: none
   steps:
     - checkout
     - run:
         name: set var
         command: echo "export DATETIME=`date +%Y%m%d.%H%M`" >> $BASH_ENV
     - run: echo "$DATETIME"
     - run: mkdir -p workspace
     - run: echo "export DATETIME=datetime.`date +%Y%m%d.%H%M%S`" >>workspace/new-env-vars
     - run: echo "export BOB=$BAZ" >> workspace/new-env-vars
     - run: cat workspace/new-env-vars 
     - run: cat workspace/new-env-vars >> $BASH_ENV
     - run: echo ${DATETIME:-NULL}
     - run: 
         name: make test artifacts
         command: |
           mkdir /tmp/artifacts;
           echo "my artifact files in a dir" > /tmp/artifacts/encap-staging-no-color_${DATETIME:-NULL}.txt 
     - store_artifacts:
         path: /tmp/artifacts
     - persist_to_workspace:
         root: workspace
         paths:
           - new-env-varsworkflows:
   say-date-workflow:
    jobs:
      - say-date
Did this answer your question?