Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/maremare/try-github-reusable-workflows

🧪 Reusing workflows のおためし
https://github.com/maremare/try-github-reusable-workflows

actions github-actions

Last synced: about 1 month ago
JSON representation

🧪 Reusing workflows のおためし

Awesome Lists containing this project

README

        

# try-github-reusable-workflows
🧪 Reusing workflows のおためし

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

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

「[jobs\.\.steps\[\*\]\.uses]」には、次の記載があります。
> ジョブのステップの一部として実行するアクションを選択します。アクションは、再利用可能なコードの単位です。**ワークフローと同じリポジトリ、パブリック リポジトリ**、または**公開された Docker コンテナ イメージ**で定義されたアクションを使用できます。

1. [Example: Using a public action] `{owner}/{repo}@{ref}`
```yml
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/[email protected]
```
2. [Example: Using a public action in a subdirectory] `{owner}/{repo}/{path}@{ref}`
```yml
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`
```yml
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}`
```yml
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}`
```yml
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}`
```yml
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]
> ワークフローでは、プライベートリポジトリをチェックアウトし、アクションをローカルで参照する必要があります。個人用アクセストークンを生成し、そのトークンを暗号化されたシークレットとして追加します。詳しい情報については「[個人用アクセストークンの作成]」および「[暗号化されたシークレット]」を参照してください。
```yml
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](https://docs.github.com/en/actions/using-workflows/reusing-workflows) によると再利用可能なワークフローを纏めたリポジトリから直接使用することができるらしい。

* 再利用可能なワークフローの例
```yml
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 }}
```

* 再利用可能なワークフローの呼び出し例
```yml
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 }}
```

[jobs\.\.steps\[\*\]\.uses]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses

[Example: Using a public action]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-public-action

[Example: Using a public action in a subdirectory]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-public-action-in-a-subdirectory

[Example: Using an action in the same repository as the workflow]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-an-action-in-the-same-repository-as-the-workflow

[Example: Using a Docker Hub action]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-docker-hub-action

[Example: Using the GitHub Packages Container registry]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-the-github-packages-container-registry

[Example: Using a Docker public registry action]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-a-docker-public-registry-action

[Example: Using an action inside a different private repository than the workflow]: https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#example-using-an-action-inside-a-different-private-repository-than-the-workflow

[個人用アクセストークンの作成]: https://docs.github.com/ja/github/authenticating-to-github/creating-a-personal-access-token
[暗号化されたシークレット]: https://docs.github.com/ja/actions/reference/encrypted-secrets

## Deprecating save-state and set-output commands な件

2022/10/11 から非推奨になったみたい。
* [GitHub Actions: Deprecating save\-state and set\-output commands \| GitHub Changelog](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/)

❌ いままで
```yml
- name: Save state
run: echo "::save-state name={name}::{value}"

- name: Set output
run: echo "::set-output name={name}::{value}"
```

✅ これから
```yml
- name: Save state
run: echo "{name}={value}" >> $GITHUB_STATE

- name: Set output
run: echo "{name}={value}" >> $GITHUB_OUTPUT
```

## 条件に応じて [jobs..runs-on] を指定する方法

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

syntax errorになるワークフローの例:

```yml
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](https://stackoverflow.com/questions/71961921/specify-runner-to-be-used-depending-on-condition-in-a-github-actions-workflow/71973229#71973229)

回避策を施したワークフローの例:

```yml
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..runs-on]: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on

## 環境変数を渡せるのか?
直では無理ポイ。[jobs..outputs] に一度格納してからだと行けるらしい…

Stack Overflow 記載の回答例:

```yml
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 }}
```

### 参考
* [Passing Environment Variables to Reusable Workflow · Discussion \#26671 · community](https://github.com/orgs/community/discussions/26671)
* [github actions \- Passing env variable inputs to a reusable workflow \- Stack Overflow](https://stackoverflow.com/a/73305536/3363518)

[jobs..outputs]: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idoutputs

## あとで試そう

> **Note**
>
> どうやら private repo 内のワークフローやアクションを利用できるようになったみたい。
> * [GitHub Actions \- Sharing actions and reusable workflows from private repositories is now GA \| GitHub Changelog](https://github.blog/changelog/2022-12-14-github-actions-sharing-actions-and-reusable-workflows-from-private-repositories-is-now-ga/)
> * [リポジトリの GitHub Actions の設定を管理する \- GitHub Docs](https://docs.github.com/ja/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository)
>