You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Azure webapps-deploy GitHub Action设置Git提交SHA为环境变量

如何在Azure Web Apps部署时将GitHub提交SHA设置为环境变量

要将当前GitHub提交的SHA值设置为Azure Web App的环境变量,你可以直接利用GitHub Actions内置的github.sha上下文获取完整提交哈希,然后通过azure/webapps-deploy动作的app-settings参数完成配置。以下是针对你现有YAML流水线的修改方案:

修改核心逻辑

在deploy-staging和deploy-production的部署步骤中,添加app-settings参数注入环境变量即可,不需要额外复杂操作:

# 部署步骤中新增的配置
app-settings: |
  GIT_COMMIT_SHA=${{ github.sha }}

完整修改后的流水线配置

# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
# More info on Python, GitHub Actions, and Azure App Service: https://aka.ms/python-webapps-actions

name: My App Deployment Pipeline

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Set up Python version
        uses: actions/setup-python@v3
        with:
          python-version: '3.9'

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate

      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          python -m playwright install

      - name: Build Project
        run: |
          cd frontend
          npm ci
          npm run build

      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v2
        with:
          name: python-app
          path: |
            . 
            !venv/
            !frontend/node_modules/
          retention-days: 5

  deploy-staging:
    name: Deploy to staging
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'stage'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: python-app
          path: .

      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v2
        id: deploy-to-webapp
        with:
          app-name: 'my-app-name'
          slot-name: 'Production'
          publish-profile: ${{ secrets.publishprofile }}
          # 新增:设置提交SHA为环境变量
          app-settings: |
            GIT_COMMIT_SHA=${{ github.sha }}
          

  deploy-production:
    name: Deploy to production
    runs-on: ubuntu-latest
    needs: [build, deploy-staging]
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: python-app
          path: .

      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v2
        id: deploy-to-webapp
        with:
          app-name: 'my-app-name'
          slot-name: 'Production'
          publish-profile: ${{ secrets.publishprofile }}
          # 新增:设置提交SHA为环境变量
          app-settings: |
            GIT_COMMIT_SHA=${{ github.sha }}

补充说明

  • 如果需要短SHA(前7位),可以将${{ github.sha }}改为${{ github.sha }}[:7]
  • 配置的GIT_COMMIT_SHA会直接同步到Azure Web App的应用设置中,部署完成后可在Azure门户查看,也能在应用代码中直接读取该环境变量

内容的提问来源于stack exchange,提问作者rustyshackleford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:56:16