Variable in runner dissappears in after_script

I add this to my .gitlab-ci.yml:
Running with gitlab-runner 16.5.0 (853330f9)

  before_script:
    - export TZ=Europe/Oslo; export STARTTIME=`date "+%Y-%m-%d %H:%M:%S%z"`
  script:
    - echo "TZ=$TZ; STARTTIME=$STARTTIME"
  :
  after_script:
    - echo "TZ=$TZ; STARTTIME=$STARTTIME"
    - /root/.pyenv/shims/python3 ./tools/elastic.py --testenv=$TESTENV \
          --start="$STARTTIME"

But $STARTTIME is empty? $TESTENV is set at the top so it is always fine, but why is $TZworking and not $STARTTIME. I’ve tried different names setting it on a standalone line, but it is still gone?
I need this to set timespan for when job has run to my logextractor script. Is there a better way?

$ export TZ=Europe/Oslo; export STARTTIME=`date "+%Y-%m-%d %H:%M:%S%z"`
$ echo "TZ=$TZ; STARTTIME=$STARTTIME"
TZ=Europe/Oslo; STARTTIME=2023-11-10 15:16:35+0100
:
:
$ echo "TZ=$TZ; STARTTIME=$STARTTIME"
TZ=Europe/Oslo; STARTTIME=
$ /root/.pyenv/shims/python3 ./tools/elastic.py --testenv=$TESTENV --start="$STARTTIME"

Thanks

variables from before_script or script are not available in after_script. after-script runs in a new shell context. This is also described in documentation.

1 Like

Thanks, so how can I pass this to after script then, a global variable ‘STARTTIME’ ?

You can’t. after_script is independent from previous steps.