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 [email protected] '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>
print(os.environ['BITBUCKET_REPO_SLUG'])
File "C:\Python\lib\os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'BITBUCKET_REPO_SLUG'
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 [email protected] '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
...
print(os.environ['BITBUCKET_REPO_SLUG'])
print(os.environ['BITBUCKET_BRANCH'])
...
...
I'm not sure what exactly am i doing wrong. Any help will really be appreciated.