Repository navigation
191 lines (182 loc) · 8.45 KB
/
Copy pathbuild.yml
File metadata and controls
191 lines (182 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# テスト・パッケージ作成と、タグ(v2.0.0 など)を push したときの nuget.org への公開
# 公開は nuget.org の Trusted Publishing(このファイル名 build.yml を登録する)を使う
name: build
on:
push:
branches: [master]
tags: ['v*']
pull_request:
branches: [master]
permissions:
contents: read
jobs:
# ドキュメントなどビルドに関係しないファイルだけの変更では、test と pack をスキップする。
# paths-ignore でワークフローごと止めると必須チェックが報告されず PR をマージできなくなるため、
# ワークフローは常に動かし、最後の build-result ジョブ(ルールセットの必須チェック)で結果をまとめる
changes:
runs-on: ubuntu-latest
outputs:
code: ${{ steps.filter.outputs.code }}
steps:
- uses: actions/checkout@v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- name: ビルドに関係する変更があるか
id: filter
shell: bash
env:
EVENT_NAME: ${{ github.event_name }}
PR_BASE: ${{ github.event.pull_request.base.sha }}
PR_HEAD: ${{ github.event.pull_request.head.sha }}
PUSH_BEFORE: ${{ github.event.before }}
run: |
# ビルドに関係しないファイル(これ以外が1つでも変わっていればビルドする)
ignore='(\.md$|^LICENSE$|^docs/|^\.claude/|^\.github/dependabot\.yml$)'
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
# タグは公開するので常にビルドする
echo "code=true" >> "$GITHUB_OUTPUT"; exit 0
elif [[ "$EVENT_NAME" == pull_request ]]; then
files=$(git diff --name-only "$PR_BASE...$PR_HEAD")
elif [[ -n "$PUSH_BEFORE" && ! "$PUSH_BEFORE" =~ ^0+$ ]] && git cat-file -e "$PUSH_BEFORE^{commit}" 2>/dev/null; then
files=$(git diff --name-only "$PUSH_BEFORE" "$GITHUB_SHA")
else
# 比較元が分からない場合はビルドする
echo "code=true" >> "$GITHUB_OUTPUT"; exit 0
fi
echo "$files"
if [[ -n "$files" ]] && ! grep -qvE "$ignore" <<< "$files"; then
echo "ビルドに関係する変更はないため、test と pack をスキップする"
echo "code=false" >> "$GITHUB_OUTPUT"
else
echo "code=true" >> "$GITHUB_OUTPUT"
fi
test:
# コアは netstandard2.0 と net8.0 で処理が異なる(#if NET8_0_OR_GREATER)ため、両方のビルドをテストする
name: test (${{ matrix.os }}, ${{ matrix.core }})
needs: changes
if: needs.changes.outputs.code == 'true'
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
core: [net8.0, netstandard2.0]
steps:
- uses: actions/checkout@v7.0.1
- uses: actions/setup-dotnet@v6.1.0
with:
dotnet-version: 10.0.x
- name: テスト
run: dotnet test -p:CoreTargetFramework=${{ matrix.core }}
# .editorconfig の書式(インデント・BOM・改行など)に従っているか
format:
needs: changes
if: needs.changes.outputs.code == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7.0.1
with:
persist-credentials: false
- uses: actions/setup-dotnet@v6.1.0
with:
dotnet-version: 10.0.x
- name: 書式の確認
# 崩れている場合は、ローカルで dotnet format ZipConvertCustomerBarCode.sln を実行して直す
run: dotnet format ZipConvertCustomerBarCode.sln --verify-no-changes
pack:
needs: test
# test がスキップされると pack もスキップされる
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7.0.1
- uses: actions/setup-dotnet@v6.1.0
with:
dotnet-version: 10.0.x
- name: バージョン
id: version
shell: bash
run: |
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
echo "args=-p:Version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
else
# タグ以外は Directory.Build.props の VersionPrefix のプレリリース版(公開はしない)
# 0.0.0 などにすると、公開済みバージョンとの API 互換性の確認でエラー(CP0003)になる
echo "args=-p:VersionSuffix=ci.${GITHUB_RUN_NUMBER}" >> "$GITHUB_OUTPUT"
fi
- name: パッケージ作成
run: dotnet pack ZipConvertCustomerBarCode.sln -c Release -o artifacts ${{ steps.version.outputs.args }}
- uses: actions/upload-artifact@v7.0.2
with:
name: packages
path: artifacts/
# ルールセットの必須チェック。test・format・pack のどれかが失敗・キャンセルされたら失敗、
# すべて成功またはスキップ(ドキュメントだけの変更)なら成功する
build-result:
if: always()
needs: [changes, test, format, pack]
runs-on: ubuntu-latest
steps:
- name: 結果の確認
if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
run: exit 1
publish:
if: startsWith(github.ref, 'refs/tags/v')
needs: pack
runs-on: ubuntu-latest
# release Environment: v* タグのみ・承認後に実行。nuget.org の Trusted Publishing のポリシーもこの Environment に限定している
environment: release
permissions:
id-token: write # Trusted Publishing の短期間の API キーの取得と、出所の証明の署名のため
attestations: write # 出所の証明を保存するため
artifact-metadata: write # 出所の証明の対象を記録するため
steps:
- uses: actions/download-artifact@v8.0.2
with:
name: packages
path: artifacts
- name: パッケージの出所の証明
# このワークフローで作ったパッケージであることを証明する(gh attestation verify で確認できる)。
# nuget.org は公開時にリポジトリ署名を付けてファイルが変わるため、確認には GitHub Release に添付したパッケージを使う
uses: actions/attest@v4.2.2
with:
subject-path: 'artifacts/*.nupkg'
- uses: actions/setup-dotnet@v6.1.0
with:
dotnet-version: 10.0.x
- name: NuGet ログイン(Trusted Publishing)
id: login
uses: NuGet/login@v1.2.0
with:
user: ${{ secrets.NUGET_USER }} # release Environment の Secret。nuget.org のユーザー名(プロフィール名。メールアドレスではない)
- name: nuget.org に公開
# .snupkg(シンボルパッケージ)は同じフォルダーにあれば一緒に公開される
run: dotnet nuget push "artifacts/*.nupkg" --api-key ${{ steps.login.outputs.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
# nuget.org に公開したあと、タグの GitHub Release を作り、出所を証明したパッケージを添付する
# (リリースノートはマージした PR から自動生成)
# 書き込み権限は、Trusted Publishing を扱う publish ジョブとは分けてこのジョブだけに付ける
github-release:
needs: publish
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@v8.0.2
with:
name: packages
path: artifacts
- name: GitHub Release の作成
env:
GH_TOKEN: ${{ github.token }}
shell: bash
run: |
# 手で先に作った場合や、ジョブを再実行した場合は、作成済みの Release にパッケージだけ添付する
if gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" > /dev/null 2>&1; then
echo "Release $GITHUB_REF_NAME は作成済みのため、パッケージだけ添付する"
gh release upload "$GITHUB_REF_NAME" artifacts/*.nupkg --repo "$GITHUB_REPOSITORY" --clobber
exit 0
fi
# v2.1.0-beta.1 のような - を含むタグはプレリリースにする
prerelease=()
if [[ "$GITHUB_REF_NAME" == *-* ]]; then prerelease=(--prerelease); fi
gh release create "$GITHUB_REF_NAME" artifacts/*.nupkg --repo "$GITHUB_REPOSITORY" --verify-tag --generate-notes --title "$GITHUB_REF_NAME" "${prerelease[@]}"