From 47d1b1579e5c3884145064e501b65d54caa5699d Mon Sep 17 00:00:00 2001 From: TANABE Ken-ichi Date: Mon, 2 Nov 2020 21:10:51 +0900 Subject: [PATCH 1/3] add action.yml --- .github/workflows/test-action.yml | 24 ++++++++++++++++++++++++ action.yml | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .github/workflows/test-action.yml create mode 100644 action.yml diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml new file mode 100644 index 0000000..25a899d --- /dev/null +++ b/.github/workflows/test-action.yml @@ -0,0 +1,24 @@ +name: Test Action + +on: + push: + branches: + - gh-action + +jobs: + test_action: + name: Run + runs-on: ubuntu-latest + + steps: + - name: Get GITHUB_TOKEN for Github Apps + uses: nabeken/go-github-apps@gh-action + id: go-github-apps + with: + installation_id: ${{ secrets.installation_id }} + app_id: ${{ secrets.app_id }} + private_key: ${{ secrets.private_key }} + + - name: Test Github API call + run: | + curl --fail -H 'Authorization: token ${{ steps.go-github-apps.outputs.app_github_token }}' https://api.github.com/ diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..aae7b85 --- /dev/null +++ b/action.yml @@ -0,0 +1,30 @@ +inputs: + version: + description: "A version to install" + default: "0.1.1" + installation_id: + description: "Github Apps Installation ID" + app_id: + description: "Github Apps App ID" + private_key: + description: "Github Apps Private Key" + +outputs: + app_github_token: + description: "GITHUB_TOKEN for your Github Apps" + value: ${{ steps.go-github-apps.outputs.github_token }} + +runs: + using: "composite" + steps: + - run: | + curl -sSLf https://raw.githubusercontent.com/nabeken/go-github-apps/master/install-via-release.sh | bash -s -- -v v${{ inputs.version }} + sudo cp go-github-apps /usr/local/bin + shell: bash + - id: go-github-apps + run: | + T=$(mktemp) + trap "rm -f $T" 1 2 3 15 + env GITHUB_PRIV_KEY="${{ inputs.private_key }}" go-github-apps -inst-id ${{ inputs.installation_id }} -app-id ${{ inputs.app_id }} > $T + echo "::set-output name=github_token::$(cat $T)" + shell: bash From 20a3f9a3c0ddb9aefbf369c6d77e956692346ba6 Mon Sep 17 00:00:00 2001 From: TANABE Ken-ichi Date: Mon, 2 Nov 2020 21:35:24 +0900 Subject: [PATCH 2/3] test-action: run test for master and pull requests --- .github/workflows/test-action.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml index 25a899d..0148127 100644 --- a/.github/workflows/test-action.yml +++ b/.github/workflows/test-action.yml @@ -1,9 +1,10 @@ name: Test Action on: + pull_request: push: branches: - - gh-action + - master jobs: test_action: @@ -12,7 +13,7 @@ jobs: steps: - name: Get GITHUB_TOKEN for Github Apps - uses: nabeken/go-github-apps@gh-action + uses: nabeken/go-github-apps@v0 id: go-github-apps with: installation_id: ${{ secrets.installation_id }} From 8f76970b26a766ba3883e18f0242f4b2399e1e28 Mon Sep 17 00:00:00 2001 From: TANABE Ken-ichi Date: Mon, 2 Nov 2020 21:35:53 +0900 Subject: [PATCH 3/3] README: Update for Github Actions --- README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 180a56b..aef44e2 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ When you want to call Github APIs from machines, you would want an access token which independs of a real account. Github provides several ways to issue tokens, for example: -- Issue Personal Access Token via machine-user: Before Github Apps exists, this is typical method to issue a token but it consumes one user seats. -- Create Github Apps and issue a token for the app: This is a new way and recommended way. The problem is [it's not that easy to issue a token](https://docs.github.com/en/developers/apps/authenticating-with-github-apps#authenticating-as-a-github-app) just to automate small stuff. +- **Personal Access Token via machine-user**: Before Github Apps exists, this is typical method to issue a token but it consumes one user seats. +- **Github Apps**: This is a new and recommended way. The problem is [it's not that easy to issue a token](https://docs.github.com/en/developers/apps/authenticating-with-github-apps#authenticating-as-a-github-app) just to automate small stuff. This command-line tool allows you to get a token with just providing `App ID`, `Installation ID` and the private key. @@ -43,6 +43,25 @@ curl -sSLf https://raw.githubusercontent.com/nabeken/go-github-apps/master/insta sudo cp go-github-apps /usr/local/bin ``` +## Github Actions + +You can automate issuing a token with Github Actions. + +Example: +```yml +- name: Get GITHUB_TOKEN for Github Apps + uses: nabeken/go-github-apps@v0 + id: go-github-apps + with: + installation_id: ${{ secrets.installation_id }} + app_id: ${{ secrets.app_id }} + private_key: ${{ secrets.private_key }} + +- name: Test Github API call + run: | + curl --fail -H 'Authorization: token ${{ steps.go-github-apps.outputs.app_github_token }}' https://api.github.com/ +``` + ## AppID and Installation ID You can find how to get those ID at https://github.com/bradleyfalzon/ghinstallation#what-is-app-id-and-installation-id