跳至主要內容

访问令牌与ActionSecrets

LincZero大约 4 分钟

访问令牌与ActionSecrets

GitLab 访问令牌

创建

GitLab 是直接在仓库下的Setting就能看到对应选项并创建

git clone http://dependToken:glpat-CbkyNxTQ3ibn35m8dqBS@192.168.1.50/recorder/recorder.git

使用

例如:

prepare_linux_cpp:
  stage: prepare
  tags:
    - gcc48
  image: 192.168.1.208:9050/msy/android:jdk17
  script:
    - git clone http://dependToken:glpat-CbkyNxTQ3ibn35m8dqBS@192.168.1.50/recorder/recorder.git	# 这里clone,注意这里的`dependToken是令牌名`
    - mkdir -p app/src/main/cpp/cmake
    - mkdir -p app/src/main/cpp/src
    - cp -r recorder/cmake/* app/src/main/cpp/cmake
    - cp -r recorder/src/* app/src/main/cpp/src
    - cp recorder/main.cpp app/src/main/cpp/main.cpp
    - cp recorder/CMakeLists.txt app/src/main/cpp/CMakeLists.txt
    - cp recorder/config.h.in app/src/main/cpp/config.h.in
    - cp recorder/hconfig.h.in app/src/main/cpp/hconfig.h.in
  artifacts:
    paths:
      - app/

GitHub 访问令牌

(感觉没GitLab的好用,坑更多)

创建

不同于GitLab,GitHub 相关设置不在仓库中,在个人设置里。 右上角头像 > Setting > 左侧栏 Developer Setting (一般在最下) > 左侧栏 Personal access tokens / Tokens (classic) > Generate new token > classic > 填好选项后创建成功 (推荐先创建再修改具体内容)

使用

除了比较常用的clone/pull/push等操作,访问令牌能调取各种 github/gitlab 的 api,例如获取CICD列表、获取构建产物等

旧版使用:

旧版的使用方式是类似这样open in new window这样open in new window

就是直接clone,然后会让你输入账号和密码,此时可以用tokens代替。但是现在Github由于安全原因,不再允许使用账号密码方式了。该方法不再能使用

新版使用 (将token放置到url中):

git clone https://oauth2:[Your_Access_Token]@[Git_Host]/[User_Or_Org]/[Repo_Name].git
# 例如:
git clone https://oauth2:1234567890abcdef@github.com/user/repo.git
# 注意这里的oauth2是协议前缀,指定Git操作使用OAuth 2.0协议进行身份验证。
# 这个前缀通常与你的个人访问令牌一起使用,以便在不需要输入用户名和密码的情况下,通过令牌来验证你对GitHub仓库的访问权限。
# 这个前缀似乎是可选的,不必填

手把手demo:

github的访问令牌怎么clone

我的令牌:github_pat_11AGFN......MVrMIq

我的仓库:git@github.com:LincZero/MdNote_Work.githttps://github.com/LincZero/MdNote_Work.git


最终命令:

# 格式
git clone https://YOUR_PAT@github.com/LincZero/MdNote_Work.git
# (你会发现这个地址类似于git+https版的混合,其实就是https版多了个 `pat@` 部分而已)

# 即
git clone https://github_pat_11AGFN......MVrMIq@github.com/LincZero/MdNote_Work.git

_

遇到的坑

GitHub的令牌创建位置居然在个人设置而非仓库里

如题,害我找半天

classic 的细粒度太低,不能针对仓库,要用beta那个token

如题。但是这两者的设置又不太一样。前者的资料比较多,后者很少,害我又踩了些坑

资料陈旧

例如现在的Github不支持帐密的问题

再例如权限名的问题:

权限问题

github的访问令牌需要什么权限才能clone,我设置了:

Administration Access: Read-only
Commit statuses Access: Read-only
Contents Access: Read-only
Metadata Access: Read-only

然后使用:

git clone github_pat_11AGFNV...WNZ4gzta@github.com:LincZero/MdNote_Work.git

但报错:

......TGN7WNZ4gzta@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 

原因:命令写错了,要用https的基础上加token才对

在CICD工具流中使用的技巧

(以 GitHub 为例)

Actions secrets and variables

应用场景

我有个Github工作流权限相关问题:

例如我将网页源码分别放在三个仓库:VuePress工程、共建笔记库、只读笔记库。

其中第一个开源,第三个不开源,而第一个仓库会通过 github action 拉取“只读笔记库”的笔记。

这个时候一般的做法是什么?笔记库创建一个只读的访问令牌?然后 VuePress工程使用 Github Setting/Security/Actions 里的Repository sercrets?

设置

得到访问令牌后,在 Github Setting/Security/Actions 里添加一个 Secrets,例如这里我添加一个名为 “VUPRESS” 的 Secrets

工作流中使用,例如:

- name: 更新子模块到最新 (通过访问令牌)
        # ${{ secrets.VUEPRESS }}
        run: |
          export VUEPRESS_TOKEN = ${{ secrets.VUEPRESS }}
          git config --global http.extraheader "Authorization: token $VUEPRESS_TOKEN"
          git config --global url."https://x-access-token:${{ secrets.VUEPRESS }}@github.com/".insteadOf "https://github.com/"
          git submodule foreach git pull origin main

环境config方法

有时我们不仅是使用 clone 方法,例如在 CICD 中使用:

- name: 更新子模块到最新 (通过访问令牌)
        # ${{ secrets.VUEPRESS }}
        run: |
          export VUEPRESS_TOKEN = ${{ secrets.VUEPRESS }}
          git config --global http.extraheader "Authorization: token $VUEPRESS_TOKEN"
          # 或
          git config --global url."https://x-access-token:${{ secrets.VUEPRESS }}@github.com/".insteadOf "https://github.com/"
          git submodule foreach git pull origin main

CICD语法糖

通过 uses: actions/checkout@v3 (或v4),可以很方便地处理好子模块、以及token。

不需要也不能像前面那样再通过 git config --global ... 来配置tokens,否则会因为重复设置 tokens 报错 重复头 问题

jobs:
  deploy-gh-pages:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          # 如果你文档需要 Git 子模块,取消注释下一行
          submodules: true
          token: '${{ secrets.VUEPRESS }}'
          
      - name: Checkout - 更新子模块到最新
        run: |
          git submodule foreach git pull origin main