我正在致力于将通用模块分离到我们 github 组织的专用存储库中。使用
Dockerfile
中 git repo 的 pip install 来安装组织内部开发的共享模块
RUN pip3 install -r requirements.txt
其中 git repo 依赖项引用如下
git+https://github.com/org/repo.git@master
面临的问题是,当使用
pip3 install
内的 pip3 install
作为 github 操作运行时,我无法使 Dockerfile
对组织私有存储库进行身份验证。我想避免为其中一位开发人员创建私有访问令牌 (PAT),因为我希望与用户无关,并且不维护离开团队成员的令牌。尝试使用${{ secrets.GITHUB_TOKEN }}
,但通过更深入的阅读,意识到令牌可以访问启动 github 操作的存储库(link)
令牌的权限仅限于包含您的工作流程的存储库
有没有办法让
pip3 install
在没有PAT的情况下在github操作中工作?
在多次迭代之一中出现错误:
Collecting git+https://****@github.com/org/repo.git@master (from -r requirements.txt (line 17))
Cloning https://****@github.com/org/repo.git (to revision master) to /tmp/pip-req-build-mnge3zvd
Running command git clone -q 'https://****@github.com/org/repo.git' /tmp/pip-req-build-mnge3zvd
fatal: could not read Password for 'https://${GITHUB_TOKEN}@github.com': No such device or address
WARNING: Discarding git+https://****@github.com/org/repo.git@master. Command errored out with exit status 128: git clone -q 'https://****@github.com/org/repo.git' /tmp/pip-req-build-mnge3zvd Check the logs for full command output.
ERROR: Command errored out with exit status 128: git clone -q 'https://****@github.com/org/repo.git' /tmp/pip-req-build-mnge3zvd Check the logs for full command output.
我建议你像这样使用 ssh:
在你的 dockerfile 中:
RUN --mount=type=ssh,id=default pip install -r requirements.txt
在您的requirements.txt中,更改为
git+ssh://[email protected]/org/repo.git@master
在repo Settings/Actions/Secrets中准备一个与你的github帐户关联的ssh私钥,名称为
SSH_KEY
(使用专用的ssh密钥会更好)
在定义 yaml 的操作中,创建一个步骤
- name: Prepare Key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_KEY }}
这将导出一个环境变量
SSH_AUTH_SOCK
以供以后使用
下一步操作,使用
SSH_AUTH_SOCK
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
ssh: |
default=${{ env.SSH_AUTH_SOCK }}
参考:https://github.com/webfactory/ssh-agent#using-the-dockerbuild-push-action-action
要为组织的许多/所有存储库启用通用方法,可以使用私有 Github 应用程序。设置时间较长,但避免了为每个存储库生成部署密钥的需要,并且不需要创建专用帐户作为“机器用户”。
如果您希望组织中的所有用户都可以使用此功能,请在此处将之前的 pem 文件的内容设置为组织机密:https://github.com/organizations/YOUR_ORG_NAME/settings/secrets/actions 否则,您可以在存储库/环境级别进行设置。
假设您将此秘密称为 SSH_KEY。
对应用程序 ID 执行相同操作。假设您将此称为 APP_ID。
现在,假设您希望构建 Docker 映像并推送到 github 包,您的工作流程.yml 文件应包含以下内容:
...
- name: Generate App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.SSH_KEY }}
owner: ${{ github.repository_owner }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:latest
secrets: |
"token=${{ steps.app-token.outputs.token }}"
...
这会将您的 Github 应用程序生成的令牌传递给 docker build-push 操作。默认情况下,此安装令牌将在 1 小时后过期:此处文档。
在您的 Dockerfile 中,可以通过安装和读取以下内容来访问秘密:
...
RUN apt-get update && apt-get install -y git
RUN --mount=type=secret,id=token git config --global url."https://x-access-token:$(cat /run/secrets/token)@github".insteadOf https://github
RUN pip install -r requirements.txt
...
最后,在您的requirements.txt中,使用 git https 约定,如下所示:
git+https://github.com/YOUR_ORG_NAME/YOU_REPO_NAME.git
Dockerfile 中指定的 git 全局配置会将其替换为包含应用程序生成的令牌的链接。