如何通过Git管理Azure本地Web App文件及关联Azure VM中的Git仓库
Hey there! Let’s tackle your two questions about managing Azure Web App files with Git and connecting it to a Git repo hosted on an Azure VM. Here’s a step-by-step breakdown:
Managing Azure Web App Files with Local Git
If you want to push code directly from your local machine to the Web App, follow these straightforward steps:
1. Enable Local Git Deployment in Azure Portal
- Head to your Web App in the Azure Portal.
- From the left menu, open Deployment Center.
- Under Source, select Local Git, then click Save.
- Azure will generate a Git endpoint URL for your app—copy this (you’ll need it later).
- If you haven’t set up deployment credentials yet, go to Deployment Center > Settings to create a username and password; these are used to authenticate when pushing code.
2. Connect Your Local Git Repo to Azure
- If your project isn’t already a Git repo, initialize it in your project folder:
git init git add . git commit -m "Initial web app commit" - Add the Azure Web App as a remote to your local repo:
git remote add azure <your-copied-web-app-git-url> - Push your code to Azure:
git push azure main # Use your default branch name if it's not main - After pushing, Azure will automatically build and deploy your app. You can check the deployment status in the Deployment Center > Logs tab.
Connecting Your Web App to a Git Repo Hosted on Azure VM
Yes, this is totally possible—though it requires a bit more setup than using a managed Git service like Azure Repos or GitHub. Here’s how to make it work:
1. Prepare the VM’s Git Repo
First, you’ll need a bare Git repo on your VM (bare repos are optimized for remote pushes):
- SSH into your Azure VM.
- Create a bare repo directory and initialize it:
mkdir -p /opt/git/my-webapp-repo.git cd /opt/git/my-webapp-repo.git git init --bare - Set proper permissions for the repo (so your user can push to it):
chown -R <your-vm-username>:<your-vm-username> /opt/git/my-webapp-repo.git
2. Make the VM’s Repo Accessible to Azure
- Secure Access: Use SSH keys instead of passwords for better security. Generate an SSH key pair (if you don’t have one) and add the public key to the VM’s
~/.ssh/authorized_keysfile for the user who will access the repo. - Network Setup: Ensure your VM’s Network Security Group (NSG) allows inbound traffic on port 22 (SSH) from either your local IP or the Web App’s outbound IPs (you can find these in the Web App’s Properties tab). For even better security, place both the Web App and VM in the same private VNet so they communicate without public IPs.
3. Configure the Web App to Pull from the VM’s Repo
- Go back to your Web App’s Deployment Center in the Azure Portal.
- Select External Git as the deployment source.
- Enter the VM’s Git repo URL in SSH format:
ssh://<vm-username>@<vm-public-ip>/opt/git/my-webapp-repo.git - For authentication, paste the private SSH key associated with the VM’s public key (or use the VM user’s username/password if you prefer, though SSH keys are safer).
- Choose the branch you want to deploy, then click Save.
- Azure will immediately pull the code from the VM’s repo and deploy it. To enable auto-deployments when you push changes to the VM’s repo, set up a post-receive hook in the bare repo:
- In the VM’s bare repo, create a
hooks/post-receivefile:nano /opt/git/my-webapp-repo.git/hooks/post-receive - Add this script (replace
<web-app-deployment-webhook-url>with the URL found in Deployment Center > Settings > Deployment webhook):#!/bin/bash curl -X POST <web-app-deployment-webhook-url> - Make the script executable:
chmod +x /opt/git/my-webapp-repo.git/hooks/post-receive
- In the VM’s bare repo, create a
Now, every time you push changes to the VM’s repo, the hook will trigger an automatic deployment to your Web App.
Key Considerations
- Maintenance: Unlike managed Git services, you’re responsible for backing up the VM’s repo, keeping the VM updated, and ensuring it’s available.
- Security: Restrict NSG rules to only allow necessary traffic, use SSH keys, and avoid exposing the VM’s repo to the public internet if possible.
- Deployment Speed: Pulling from a VM might be slower than using a managed service, especially if they’re not in the same region or VNet.
内容的提问来源于stack exchange,提问作者Ayed Mohamed Amine
相关产品推荐
相关产品推荐

