GitHub Actionsで`actions/checkout`の使用時に`x-access-token`が無視される
GitHub Actionsのある挙動に関して、知らずに少々ハマったため、こちらで広く共有します。
遭遇した事象
GitHub Actionsを使用している際に、以下のようにGitHub Appのトークンをhttps://x-access-token:TOKEN@github.comのようにセットしているにも関わらず、想定通りに認証されないことがありました。
- name: Generate GitHub App Token
id: app-token
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
with:
app-id: ${{ secrets.SAMPLE_GITHUB_APP_ID }}
private-key: ${{ secrets.SAMPLE_GITHUB_APP_CLIENT_SECRET }}
- name: Checkout
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
- name: Set x-access-token
run: |
git config --global url."https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/".insteadOf "https://github.com/"
事象の原因
actions/checkoutにはtokenというパラメータがあり、デフォルトではtoken: ${{ github.token }}となります。
# Personal access token (PAT) used to fetch the repository. The PAT is configured
# with the local git config, which enables your scripts to run authenticated git
# commands. The post-job step removes the PAT.
#
# We recommend using a service account with the least permissions necessary. Also
# when generating a new PAT, select the least scopes necessary.
#
# [Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
#
# Default: ${{ github.token }}
token: ''
このトークンは、http.extraHeaderに設定され、サーバーとの通信時にAuthorization: Basic <token>がHTTPヘッダとして追加されることになります。
http.extraHeader
Pass an additional HTTP header when communicating with a server. If more than one such entry exists, all of them are added as extra headers. To allow overriding the settings inherited from the system config, an empty value will reset the extra headers to the empty list.
action/checkoutのソースコード内では、以下の箇所がそれに相当します。
このhttp.extraHeaderによるトークンの設定が、x-access-tokenによるトークンの設定よりも優先されるため、actions/checkoutの使用時にx-access-tokenで設定されたトークンが無視されることとなります。
実際に試す
以下のようなworkflowで、トークンを使用したプライベートリポジトリからのfetchを試すと、x-access-tokenで渡したトークンが使用されないことを確認できます。
name: Test Token Priority
jobs:
test:
name: Test token priority
runs-on: ubuntu-latest
steps:
- name: Generate GitHub App Token
id: app-token
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
with:
app-id: ${{ secrets.SAMPLE_GITHUB_APP_ID }}
private-key: ${{ secrets.SAMPLE_GITHUB_APP_CLIENT_SECRET }}
owner: ${{ secrets.PRIVATE_REPO_OWNER }}
repositories: ${{ secrets.PRIVATE_REPO_NAME }}
- name: Checkout
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
with:
persist-credentials: true # ここがfalseならば、http.extraHeaderの設定が保持されず、x-access-tokenが使用されるようになる (デフォルトはtrue)。
- name: Set x-access-token
run: |
git config --global url."https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/".insteadOf "https://github.com/"
- name: Add remote and test fetch
# Privateリポジトリからのフェッチを試す。
run: |
git remote add test-remote https://github.com/${{ secrets.PRIVATE_REPO_OWNER }}/${{ secrets.PRIVATE_REPO_NAME }}.git
git fetch test-remote
なお、actions/checkoutのpersist-credentialsをfalseにすると、x-access-tokenが使用されることも確認できます。
これは、persist-credentialsがfalseの場合には、checkout終了後に認証用のhttp.extraHeaderがクリアされるためです。
関連するissueや記事
本記事の内容と同じく、http.extraHeaderの優先度が高いため、オーバーライドが難しいというissueです。
また、以下はpersist-credentialsはそもそもfalseにすべきという記事です。
Discussion