I am trying to execute a Python script on Windows server 2016. I have Win32-OpenSSH installed on Windows server. The python script expects two environment variables (BITBUCKET_REPO_SLUG and BITBUCKET_BRANCH) to be present. These are already set by default in the Bitbucket pipeline. The python script is copied to the remote server and then using SSH, i invoke it from the Bitbucket pipeline.

When I was trying the following command in my Bitbucket pipeline...

scp <copy_python_script_to_win_server> # Works fine
echo $BITBUCKET_REPO_SLUG   # Prints the repo name
echo $BITBUCKET_BRANCH     # Prints the branch name
ssh john.doe@xx.yyy.zzz.xyz 'C:/Python/bin/python.exe C:/Users/john.doe/deploy.py' >> ./cmd_output
echo $?            
cat ./cmd_output

...I was getting the following error:

Traceback (most recent call last):
  File "C:/Users/john.doe/deploy.py", line 16, in <module>
  File "C:\Python\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None

It seemed to me that the Bitbucket environment variables were not getting passed to the python script (most probably because I was not running it from the pipeline itself but because i was invoking it on a remote server). So keeping everything else as is, I only changed the ssh command to the following:

ssh john.doe@xx.yyy.zzz.xyz 'set BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG; set BITBUCKET_BRANCH=$BITBUCKET_BRANCH; C:/Python/bin/python.exe C:/Users/john.doe/deploy.py' >> ./cmd_output

With the above change, the pipeline shows successful build and the return status ($?) always prints 0. Also, cat ./cmd_output prints nothing. As you can expect, on the Windows server, the python script doesn't really runs.

Content of C:/Users/john.doe/deploy.py:

import os

I'm not sure what exactly am i doing wrong. Any help will really be appreciated.

1 Answer 1


Environment variables are not resolved in single-quote strings.

Try replacing them with double-quotes:

ssh john.doe@xx.yyy.zzz.xyz "set BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG;  ..." >> ./cmd_output

Further your syntax is probably not valid.

  • If your shell is Windows cmd.exe: You cannot use a semicolon (;) to separate commands. You have to use ampersand (&).

    set VAR1=$VALUE & set VAR2=$VALUE2 & python ...
  • If your shell is an emulation of some common *nix shell, like bash: set command is not for setting environment variables. In bash, you set variables simply by an assignment, like BITBUCKET_REPO_SLUG=$BITBUCKET_REPO_SLUG.

    VAR1=$VALUE1; VAR2=$VALUE2; python ...
  • Thanks but for the quote thing, i came across a similar situation before though that time it was all *nix based environment. I think single quote is the suggested way although i tried double quote as well after i read your suggestion. Nothing happened. Command didn't run on Windows machine, echo returned 0 and output file was blank. :(
    – Technext
    Oct 1, 2018 at 6:22
  • What shell does your SSH server use? *nix-like shell or Windows cmd.exe? Oct 1, 2018 at 6:24
  • I ran this command: ssh john.doe@xx.yyy.zzz.xyz "echo $SHELL" and it returned /bin/ash.
    – Technext
    Oct 1, 2018 at 6:33
  • Where did you take ash from? Do you have WSL installed? Oct 1, 2018 at 7:15
  • I did not install it explicitly. I was just checking if i could install WSL but my server version is not compatible. Here are the details: Version # 1607 and OS Build # 14393.2485. I have only installed Win32-OpenSSH
    – Technext
    Oct 1, 2018 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.