After_script and variables


#1
cli_test:
  before_script:
    - export CLI_TEST_PREFIX=`uuidgen`
    - echo "BEFORE PREFIX=$CLI_TEST_PREFIX"
  stage: integration_test
  script:
    - echo "SCRIPT PREFIX=$CLI_TEST_PREFIX"
  after_script:
    - echo "AFTER PREFIX=$CLI_TEST_PREFIX"

Please note the snippet from the .gitlab-ci.yml

the echo line in the after_script section is empty, i need to retain this date in the after_script to do some cleanup ?


#2

I have a similar problem.

The before_script starts a ssh-agent which is used by the script to copy some files via rsync to a remote server.
I want the after_script to stop the agent. But it seems like the environment is reset after the script has run.
The script has access to the environment and is able to sync files using the previously started ssh-agent.
As it is unable to stop the ssh-agent the processes pile up from build to build wasting resources.

The deploy job:

deploy_dev:
  stage: deploy

  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - echo -e "Host remotehost\nHostName remotehost\nCompression yes\nUser deployer\n\n" > ~/.ssh/config

  script:
    - ./gradlew -Penv=test publish --stacktrace --info
    
  after_script:
    - ssh-agent -k

The output:

Running with gitlab-ci-multi-runner 1.5.2 (76fdacd)
Using Shell executor...
Running on zweiblum...
Fetching changes...
Lösche .gradle/
Lösche build/
HEAD ist jetzt bei c4442b5 kill the damn agent....
Checking out c4442b55 as develop...
Checking cache for deploy_dev/develop...
Downloading artifacts for build_dev (61)...
Downloading artifacts from coordinator... ok        id=61 responseStatus=200 OK token=shU-FXJr
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
/usr/bin/ssh-agent
$ eval $(ssh-agent -s)
Agent pid 32699
$ ssh-add <(echo "$SSH_PRIVATE_KEY")
Identity added: /dev/fd/63 (rsa w/o comment)
$ mkdir -p ~/.ssh
$ echo -e "Host remotehost\nHostName remotehost\nCompression yes\nUser deployer\n\n" > ~/.ssh/config
$ ./gradlew -Penv=test publish --stacktrace --info
:publish

BUILD SUCCESSFUL

Total time: 18.177 secs
Running after script...
$ ssh-agent -k
SSH_AGENT_PID not set, cannot kill agent

#3

Hi,

We have the same issue with ssh-agent.

We’ve identified (a lot of “echo $$”) that „before_script” and the actual stage is ran in the same shell session but the „after_script” is ran in another shell session.

I think this is clearly a small bug.

We’ve managed to do it by writing the pid of the ssh-agent in a file:
echo $SSH_AGENT_PID > /tmp/agent-${CI_JOB_ID}

And in after_script we kill the pid from that file.