Skip to content

cicd: update ssh command #22

cicd: update ssh command

cicd: update ssh command #22

name: "Deploy with git"
on:
push:
branches:
- main
env:
SSH_KEY_FILE_DIR: .keys
SSH_KEY_FILE_PATH: .keys/deploy_key.pem
SSH_COMMAND_FILE: ssh_command.sh
jobs:
ssh_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Create deploy key file
run: |
mkdir ${{env.SSH_KEY_FILE_DIR}}
echo "${{secrets.SSH_DEPLOY_KEY}}" > ${{env.SSH_KEY_FILE_PATH}}
chmod 600 ${{env.SSH_KEY_FILE_PATH}}
- name: Deploy with git
run: |
echo "cd ${{secrets.SSH_DEPLOY_PATH}} && git pull" >> ${{env.SSH_COMMAND_FILE}}
HAS_PYTHON_CHANGE=true
GIT_CHANGED_FILE=$(git show --name-only)
if [[ $(echo $GIT_CHANGED_FILE | grep ".py") == "" ]]; then
HAS_PYTHON_CHANGE=false
else
echo $GIT_CHANGED_FILE
echo "Python file changed"
fi
if [[ $HAS_PYTHON_CHANGE == true ]]; then
echo "kill \$(ps | grep app.py | grep -v grep | awk '{print \$1}') && nohup python app.py &" >> ${{env.SSH_COMMAND_FILE}}
fi
cat ${{env.SSH_COMMAND_FILE}}
chmod +x ${{env.SSH_COMMAND_FILE}}
cat ${{env.SSH_COMMAND_FILE}} | ssh -i ${{env.SSH_KEY_FILE_PATH}} -o StrictHostKeyChecking=no ${{secrets.SSH_DEPLOY_USER}}@${{secrets.SSH_DEPLOY_HOST}}
rm -rf ${{env.SSH_COMMAND_FILE}}
- name: Clean deploy key file
run: rm -rf ${{env.SSH_KEY_FILE_PATH}}