LoginSignup
0
0

githubのアクセストークンを設定

Posted at

githubのアクセストークンを設定

github actionsを用いて自動的にファイルを修正してもらいたい時などがあります.

その際に,自分のリポジトリの情報を取得するために,githubのtokenを設定する必要があります.

今回はそのやり方について詳細に説明をします.

1. githubのtokenを生成

自分のアカウントのリポジトリの情報を取得するために,githubのtokenを生成します.
これをすることで,自分のプライベートリポジトリの情報も取得できるようになります.

  1. githubの右上のアイコンをクリックして,Settingsをクリックします.
  2. 左のメニューからDeveloper settingsをクリックします.
  3. 左のメニューからPersonal access tokensをクリックします.

スクリーンショット 2024-06-08 1.38.02.png

4. Generate new tokenをクリックします.

Fine-granted tokensというbeta版のものもありますが,今回は簡単にTokens (classic)を使用します.

5. tokenの名前を入力し,repoにチェックを入れて,Generate tokenをクリックします.

スクリーンショット 2024-06-08 1.06.15.png

6. tokenが生成されるので,これをコピーしておきます.

このtokenは一度しか表示されないので,必ずコピーしておきましょう!

2. githubのtokenを設定する

  1. 自分のアカウント名のリポジトリのSettingsからSecrets and variablesactionsを開く

スクリーンショット 2024-06-08 1.46.35.png

2. Repository secretsNew repository secretをクリックします.

New repository secret

3. NamePERSONAL_ACCESS_TOKENValueに先ほどコピーしたtokenを入力し,Add secretをクリックします.

PERSONAL_ACCESS_TOKENの設定

4. SettingsActionsGeneralの一番下に,Workflow permissionsとあるので,それのRead and write permissionsを選択します.

スクリーンショット 2024-06-08 1.47.35.png

これによってgithub actionsが自分のリポジトリにcommit,pushできるようになります.

以上で,githubのtokenの設定が完了しました.

3. github actionsでの利用

せっかくなので,github actionsでの利用方法も記載しておきます.

.github/workflows/auto_commit.ymlとして作成します.

auto_commit.yml
name: commit and push

on:
  push:
    branches:
      - main
      - master
  workflow_dispatch:

jobs:
  run-and-commit-notebook:
    runs-on: ubuntu-latest
    steps:
    - name: リポジトリをチェックアウト
      uses: actions/checkout@v2

    ## ここにスクリプトを追加

    - name: Commit & Push
      env:
        ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      run: |
        git config user.name github-actions
        git config user.email github-actions@github.com
        git add -A .
        git commit -m "update file name"
        git -c http.extraheader="AUTHORIZATION: bearer ${ACCESS_TOKEN}" push

user.nameとuser.emailは,現在githubという仮の名前を設定しています.
自分のアカウントでコミットを行なって欲しい場合は,適宜変更してください.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0