Skip to content

Instantly share code, notes, and snippets.

@h4xrOx
Created August 27, 2022 10:35
Show Gist options
  • Select an option

  • Save h4xrOx/acd8e35021f5b208c56eb8308d8928c6 to your computer and use it in GitHub Desktop.

Select an option

Save h4xrOx/acd8e35021f5b208c56eb8308d8928c6 to your computer and use it in GitHub Desktop.
Azure DevOps Pipeline: deploy python3 app to remote server and launch it via pm2
# variables:
# dest-dir: /home/ubuntu/backend
# dest-server: dev server
# pm2-instance-name: bruh
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- task: DeleteFiles@1
displayName: Clear .git stuff
inputs:
SourceFolder:
Contents: |
.git/*
- task: SSH@0
displayName: Install python3
inputs:
sshEndpoint: $(dest-server)
runOptions: 'commands'
commands: 'sudo apt-get install -qq -y python3-pip'
readyTimeout: '20000'
- task: SSH@0
displayName: Stop existing pm2 service
inputs:
sshEndpoint: $(dest-server)
runOptions: 'inline'
# Cringe python3 eval with stdout/stderr ignore cz otherwise it will fail on 1st run :shrug:
inline: |
python3 -c 'import subprocess; import sys; subprocess.run(["pm2", "delete", " ".join(sys.argv[1:])], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)' $(PM2-INSTANCE-NAME)
failOnStdErr: false
readyTimeout: '20000'
- task: SSH@0
displayName: Clear destination directory
inputs:
sshEndpoint: $(dest-server)
runOptions: 'inline'
inline: 'find $(DEST-DIR)/ -type f -not -name ''config.json'' -delete'
readyTimeout: '20000'
- task: CopyFilesOverSSH@0
displayName: Upload content
inputs:
sshEndpoint: $(dest-server)
contents: '**'
targetFolder: $(dest-dir)
readyTimeout: '20000'
- task: SSH@0
displayName: Install python requirements
inputs:
sshEndpoint: $(dest-server)
runOptions: 'commands'
commands: 'sudo -H pip3 install -r $(DEST-DIR)/requirements.txt'
readyTimeout: '20000'
- task: SSH@0
displayName: Start pm2 service
inputs:
sshEndpoint: $(dest-server)
runOptions: 'inline'
inline: |
cd $(DEST-DIR)
pm2 start main.py --interpreter=python3 --name=$(PM2-INSTANCE-NAME)
readyTimeout: '20000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment