如何使用 GITHUB_TOKEN 克隆私有仓库?

时间:2021-07-03 08:34:09

标签: continuous-integration github-actions

这是我的动作脚本:

name: Build

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - run: |
       whoami
       sudo mkdir /first_org
       sudo chmod -R 777 /first_org
       cd /first_org
       git clone https://github.com/first_org/site
       sudo rm -rf /first_org/site/.git
       sudo mkdir /second_org
       sudo chmod -R 777 /second_org
       cd /second_org
       git clone https://github.com/second_org/site
       cp -a /first_org/site/. /second_org/site
       cd /second_org/site
       sudo apt-get update
       sudo apt install nodejs
       npm build

/first_org/site 是公共存储库,但 /second_org/site 是私有存储库。

我不使用 action/checkout@v2,因为它不允许我们指定要克隆到的 绝对 路径。因此我不得不使用纯 shell 命令。

此操作属于 /second_org/site 存储库,因此基于 docs,我可以使用 GITHUB_TOKEN 访问它。但没有一个示例显示如何在简单的 git clone 命令中使用它。

如何在我的 shell 中使用 GITHUB_TOKEN

1 个答案:

答案 0 :(得分:1)

您可以在 shell (bash) 中以这种方式使用令牌进行克隆:

git clone https://<token>@github.com/<owner>/<repoName>.git

请注意,GITHUB_TOKEN 可能没有足够的范围权限用于私有存储库。在这种情况下,您将需要使用 PAT。

以下是我在 bash 中创建克隆存储库的操作示例(作为参考):https://github.com/GuillaumeFalourd/create-other-repo-branch-action/blob/main/action.yml