README.md #13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Azure Storage and VM | |
on: | |
push: | |
branches: [ main ] | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.10' | |
- name: Install Azure CLI | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y ca-certificates curl apt-transport-https lsb-release gnupg | |
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null | |
AZ_REPO=$(lsb_release -cs) | |
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list | |
sudo apt-get update | |
sudo apt-get install -y azure-cli | |
- name: Zip project | |
run: zip -r project_t.zip . | |
- name: Upload to Azure Storage | |
run: | | |
az storage blob upload -f project_t.zip -c cwb -n project_t.zip --account-name ${{ secrets.AZURE_STORAGE_ACCOUNT }} --account-key ${{ secrets.AZURE_STORAGE_KEY }} --overwrite | |
- name: Download from Azure Storage | |
run: | | |
az storage blob download -c cwb -n project_t.zip -f project_t.zip --account-name ${{ secrets.AZURE_STORAGE_ACCOUNT }} --account-key ${{ secrets.AZURE_STORAGE_KEY }} | |
- name: Deploy to VM | |
run: | | |
echo "${{ secrets.VM_SSH_KEY }}" > vm_key.pem | |
chmod 400 vm_key.pem | |
# Create .ssh directory | |
mkdir -p ~/.ssh | |
# Add the VM to known hosts to avoid host key verification issues | |
ssh-keyscan -H ${{ secrets.VM_IP_ADDRESS }} >> ~/.ssh/known_hosts | |
# Copy the ZIP file to the VM | |
scp -i vm_key.pem project_t.zip ${{ secrets.VM_USERNAME }}@${{ secrets.VM_IP_ADDRESS }}:/home/kamalikaghora14/flask-ml/ | |
# SSH into the VM, unzip file, delete ZIP, create .env, and start the application | |
ssh -i vm_key.pem ${{ secrets.VM_USERNAME }}@${{ secrets.VM_IP_ADDRESS }} << 'EOF' | |
cd /home/kamalikaghora14/flask-ml | |
unzip -o project_t.zip | |
rm project_t.zip | |
rm -rf myenv | |
touch .env | |
echo "${{ secrets.ENV }}" > .env | |
chmod +x start_app_flask.sh | |
sudo systemctl stop flask_app.service | |
sudo systemctl daemon-reload | |
sudo systemctl enable flask_app.service | |
sudo systemctl start flask_app.service | |
EOF | |
env: | |
AZURE_STORAGE_KEY: ${{ secrets.AZURE_STORAGE_KEY }} |