Skip to content

before_script, script, after_script not in same context

Summary

I don't known if it's bug, but i am not able to use ssh command in after_script

Steps to reproduce

In gitlab-ci configuration file have something like this :

before_script:
    # run ssh-agent
    - eval $(ssh-agent -s)
    # add ssh key stored in DEPLOY_SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$DEPLOY_SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - 'ping -c 1 $DEPLOY_SSH_HOST || ( echo "Host OK" )'
     
after_script:
     - 'ssh -v $DEPLOY_SSH_USER@$DEPLOY_SSH_HOST -C " ./do_some_post_script "'

stepOne:
  variables:
     MY_CUSTOM_CONF: "hi"
  environment: production
  when: manual
  script:
     - 'ssh $DEPLOY_SSH_USER@$DEPLOY_SSH_HOST -C "echo test"'

Expected behavior

Connection SSH will works, i thinks

Actual behavior

Connection failed (permission denied), it seams ssh-agent not running in after_script context. Need to add ssh-agent initialisation (like below) in after_script to connection success.

after_script:
    # run ssh-agent
    - eval $(ssh-agent -s)
    # add ssh key stored in DEPLOY_SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$DEPLOY_SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
     - 'ssh -v $DEPLOY_SSH_USER@$DEPLOY_SSH_HOST -C " ./do_some_post_script "'

Relevant logs and/or screenshots

(Paste any relevant logs - please use code blocks (```) to format console output, logs, and code as it's very hard to read otherwise.)

Output of checks

Results of GitLab application Check

(For installations with omnibus-gitlab package run and paste the output of: sudo gitlab-rake gitlab:check SANITIZE=true)

(For installations from source run and paste the output of: sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true)

(we will only investigate if the tests are passing)

Results of GitLab environment info

(For installations with omnibus-gitlab package run and paste the output of: sudo gitlab-rake gitlab:env:info)

(For installations from source run and paste the output of: sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production)

Possible fixes

(If you can, link to the line of code that might be responsible for the problem)