Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MareMare/try-github-reusable-workflows

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

55 Commits

Repository files navigation

try-github-reusable-workflows

🧪 Reusing workflows のおためし

カスタムな GitHub Actions を使用する方法

jobs..steps[*].uses で指定する例:

jobs.<job_id>.steps[*].uses」には、次の記載があります。

ジョブのステップの一部として実行するアクションを選択します。アクションは、再利用可能なコードの単位です。ワークフローと同じリポジトリ、パブリック リポジトリ、または公開された Docker コンテナ イメージで定義されたアクションを使用できます。

  1. Example: Using a public action {owner}/{repo}@{ref}
    jobs:
     my_first_job:
     steps:
     - name: My first step
     # Uses the default branch of a public repository
     uses: actions/heroku@main
     - name: My second step
     # Uses a specific version tag of a public repository
     uses: actions/aws@v2.0.1
  2. Example: Using a public action in a subdirectory {owner}/{repo}/{path}@{ref}
    jobs:
     my_first_job:
     steps:
     - name: My first step
     uses: actions/aws/ec2@main
  3. Example: Using an action in the same repository as the workflow ./path/to/dir
    jobs:
     my_first_job:
     steps:
     - name: Check out repository
     uses: actions/checkout@v3
     - name: Use local my-action
     uses: ./.github/actions/my-action
  4. Example: Using a Docker Hub action docker://{image}:{tag}
    jobs:
     my_first_job:
     steps:
     - name: My first step
     uses: docker://alpine:3.8
  5. Example: Using the GitHub Packages Container registry docker://{host}/{image}:{tag}
    jobs:
     my_first_job:
     steps:
     - name: My first step
     uses: docker://ghcr.io/OWNER/IMAGE_NAME
  6. Example: Using a Docker public registry action docker://{host}/{image}:{tag}
    jobs:
     my_first_job:
     steps:
     - name: My first step
     uses: docker://gcr.io/cloud-builders/gradle
ワークフローとは異なる private リポジトリ内のアクションを使用する例:

どうやら、ワークフローのリポジトリに private リポジトリを取り込んで action を使用する模様。

  • Example: Using an action inside a different private repository than the workflow

    ワークフローでは、プライベートリポジトリをチェックアウトし、アクションをローカルで参照する必要があります。個人用アクセストークンを生成し、そのトークンを暗号化されたシークレットとして追加します。詳しい情報については「個人用アクセストークンの作成」および「暗号化されたシークレット」を参照してください。

    jobs:
     my_first_job:
     steps:
     - name: Check out repository
     uses: actions/checkout@v3
     with:
     repository: octocat/my-private-repo
     ref: v1.0
     token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
     path: ./.github/actions/my-private-repo
     - name: Run my action
     uses: ./.github/actions/my-private-repo/my-action
ワークフローとは異なる public リポジトリ内の reusable workflow を使用する例:

Reusing workflows - GitHub Docs によると再利用可能なワークフローを纏めたリポジトリから直接使用することができるらしい。

  • 再利用可能なワークフローの例

    name: Reusable workflow example
    on:
     workflow_call:
     inputs:
     config-path:
     required: true
     type: string
     secrets:
     token:
     required: true
    jobs:
     triage:
     runs-on: ubuntu-latest
     steps:
     - uses: actions/labeler@v4
     with:
     repo-token: ${{ secrets.token }}
     configuration-path: ${{ inputs.config-path }}
  • 再利用可能なワークフローの呼び出し例

    name: Call a reusable workflow
    on:
     pull_request:
     branches:
     - main
    jobs:
     call-workflow:
     uses: octo-org/example-repo/.github/workflows/workflow-A.yml@v1
     call-workflow-passing-data:
     permissions:
     contents: read
     pull-requests: write
     uses: octo-org/example-repo/.github/workflows/workflow-B.yml@main
     with:
     config-path: .github/labeler.yml
     secrets:
     token: ${{ secrets.GITHUB_TOKEN }}

Deprecating save-state and set-output commands な件

2022年10月11日 から非推奨になったみたい。

❌ いままで

- name: Save state
 run: echo "::save-state name={name}::{value}"
- name: Set output
 run: echo "::set-output name={name}::{value}"

✅ これから

- name: Save state
 run: echo "{name}={value}" >> $GITHUB_STATE
- name: Set output
 run: echo "{name}={value}" >> $GITHUB_OUTPUT

条件に応じて jobs.<job_id>.runs-on を指定する方法

runs-on: ${{ inputs.runs-on }} などと指定できない

syntax errorになるワークフローの例:
name: 09.choice-runner-os
on:
 workflow_dispatch:
 inputs:
 runs-on:
 description: 'OS'
 required: true
 defaults: "ubuntu-latest"
 type: choice
 options:
 - "ubuntu-latest"
 - "windows-latest"
jobs:
 sample:
 runs-on: ${{ inputs.runs-on }} # ❌ `Invalid workflow file. You have an error in your yaml syntax on line 15`
 steps:
 - name: おためし
 run: echo "runs on ${{ inputs.runs-on }}"

runs-on: ${{ needs.setup.outputs.runner }} などで指定できる

Specify runner to be used depending on condition in a GitHub Actions workflow - Stack Overflow

回避策を施したワークフローの例:
name: 09.choice-runner-os
on:
 workflow_dispatch:
 inputs:
 runs-on:
 description: 'OS'
 required: true
 defaults: "ubuntu-latest"
 type: choice
 options:
 - "ubuntu-latest"
 - "windows-latest"
jobs:
 setup:
 runs-on: ubuntu-latest
 outputs:
 runner: ${{ steps.step1.outputs.os }}
 steps:
 - name: 🎯 Determine OS
 id: step1
 run: |
 if [[ '${{ inputs.runs-on }}' == windows* ]]; then
 echo "os=windows-latest" >> $GITHUB_OUTPUT
 elif [[ '${{ inputs.runs-on }}' == macos* ]]; then
 echo "os=macos-latest" >> $GITHUB_OUTPUT
 else
 echo "os=ubuntu-latest" >> $GITHUB_OUTPUT
 fi
 - name: Determine OS結果 - '${{ steps.step1.outputs.os }}'
 run: echo "Determine OS結果 - '${{ steps.step1.outputs.os }}'"
 sample:
 needs: setup
 runs-on: ${{ needs.setup.outputs.runner }}
 steps:
 - name: おためし
 run: |
 echo "💡 This workflow on ${{ github.repository }} was started by ${{ github.actor }}"
 echo ""
 echo "📝 The runner context is:"
 echo "${{ toJson(runner) }}"
 echo ""

環境変数を渡せるのか?

直では無理ポイ。jobs.<job_id>.outputs に一度格納してからだと行けるらしい...

Stack Overflow 記載の回答例:
env:
 SOME_VAR: bla_bla_bla
 ANOTHER_VAR: stuff_stuff
jobs:
 print:
 runs-on: ubuntu-latest
 outputs:
 some_var: ${{ steps.step1.outputs.some_var }}
 another_var: ${{ steps.step1.outputs.another_var }} 
 steps:
 - name: Print inputs passed to the reusable workflow
 id: step1
 run: |
 echo "some var: $SOME_VAR"
 echo "::set-output name=some_var::$SOME_VAR"
 echo "another var: $ANOTHER_VAR"
 echo "::set-output name=another_var::$ANOTHER_VAR"
 
 call_reusable:
 needs:
 - print
 uses: ...
 with:
 input_var: ${{ needs.print.outputs.some_var }}
 another_input_var: ${{ needs.print.outputs.another_var }}

参考

あとで試そう

Note

どうやら private repo 内のワークフローやアクションを利用できるようになったみたい。

About

🧪 Reusing workflows のおためし

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

AltStyle によって変換されたページ (->オリジナル) /