You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

pip安装私有GitHub仓库时本地密钥存储问题及GitHub Actions流水线配置咨询

pip安装私有GitHub仓库时本地密钥存储问题及GitHub Actions流水线配置咨询

嘿,我看你正在搭建一个部署到Azure Web App的Python项目GitHub Actions流水线,同时应该还在纠结pip安装私有GitHub仓库时的密钥存储问题吧?先帮你把现有的流水线代码整理成规范格式,再给你针对性的解决建议:

整理后的现有GitHub Actions流水线代码

name: Build and deploy Python app to Azure Web App - app-xx-xx-api-dev
on:
  push:
    branches:
      - dev
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python version
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install dependencies
        # 你可以继续补充依赖安装的命令,示例如下
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

关于pip安装私有GitHub仓库的密钥处理建议

1. 本地开发环境的密钥存储

绝对不要在本地的requirements.txt里硬编码GitHub个人访问令牌(PAT),推荐用Git凭证助手安全存储:

  • 先在本地配置Git凭证助手:
    git config --global credential.helper store
    
  • 之后第一次拉取私有仓库时,输入你的GitHub用户名和PAT,凭证会自动存储到本地,后续pip安装时就不用再手动输入密钥了。

2. GitHub Actions流水线中的密钥处理

在Actions里安装私有仓库,需要先把GitHub PAT存到仓库的Secrets中(路径:仓库Settings -> Secrets and variables -> Actions -> New repository secret),比如命名为GITHUB_PAT,然后在流水线的依赖安装步骤中调用:

  • 修改Install dependencies步骤示例:
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        # 方式一:直接在pip命令中传入带PAT的私有仓库地址
        pip install git+https://${{ secrets.GITHUB_PAT }}@github.com/你的用户名/你的私有仓库.git
        # 方式二:通过.netrc文件传递凭证,适配requirements.txt里已有的私有仓库引用
        echo "machine github.com login ${{ secrets.GITHUB_PAT }} password x-oauth-basic" > ~/.netrc
        pip install -r requirements.txt
    

另外提一句,你目前的流水线只有build阶段,如果要完成Azure Web App部署,还需要添加部署步骤,比如使用azure/webapps-deploy@v2动作,记得把Azure的相关凭证也存到Secrets里哦。

备注:内容来源于stack exchange,提问作者Thomas Segato

火山引擎 最新活动