-
Notifications
You must be signed in to change notification settings - Fork 8
171 lines (157 loc) · 6.93 KB
/
update-packagejson.yaml
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
name: (R) Update package.json
on:
workflow_call:
inputs:
file-path:
description: "package.json path to update. You can input multiline paths. Supported files are `package.json`, `plugin.cfg` and `Directory.Build.props`"
required: true
type: string
require-validation:
description: "true require validation must pass, false to keep going even validation failed."
required: false
type: boolean
default: true
tag:
description: "git tag you want create. (sample 1.0.0)"
required: true
type: string
dry-run:
description: "true to simularate commit but not push change."
required: true
type: boolean
dotnet-run-path:
description: "dotnet run path, executable should output desired change. (sample: src/Foo/Bar.csproj)"
required: false
type: string
default: ''
# TODO: Obsolete, this field can be removed after reference removed.
push-tag:
description: "true = push tag. false = no push tag."
required: false
type: boolean
default: true
ref:
description: "checkout ref"
required: false
type: string
default: ''
outputs:
sha:
description: "Git commit sha after package.json has changed."
value: ${{ jobs.update-packagejson.outputs.sha }}
branch-name:
description: Git branch name created.
value: ${{ jobs.update-packagejson.outputs.branch-name }}
is-branch-created:
description: Indicate is Git branch created or not.
value: ${{ jobs.update-packagejson.outputs.is-branch-created }}
jobs:
validate:
uses: Cysharp/Actions/.github/workflows/validate-tag.yaml@main
with:
tag: ${{ inputs.tag }}
require-validation: ${{ inputs.require-validation }}
secrets: inherit
update-packagejson:
needs: [validate]
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
sha: ${{ steps.commit.outputs.sha }}
branch-name: ${{ steps.configure.outputs.branch-name }}
is-branch-created: ${{ steps.commit.outputs.is-branch-created }}
steps:
- name: Configure Output variables
id: configure
run: |
echo "branch-name=test-release/${{ inputs.tag }}" | tee -a "$GITHUB_OUTPUT"
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
# package.json
# "version": 1.2.3 -> "version": 2.0.0
# plugin.cfg
# version="1.2.3" -> version="2.0.0"
#
# TIPS: `grep -v "^$"` is used to remove empty line.
- name: Update files to version ${{ needs.validate.outputs.normalized_tag }}
run: |
expected="${{ needs.validate.outputs.normalized_tag }}"
while read -r file_path; do
if [[ "$file_path" == "" ]]; then continue; fi
echo "Start $file_path"
file_name=$(basename "$file_path")
echo "::group::Before"
cat "$file_path"
echo "::endgroup::"
echo "::group::Updating"
if [[ "${file_name}" == "package.json" ]]; then
# Unity `"version": "VersionString",`
sed -i -e "s/\(\"version\":\) \"\(.*\)\",/\1 \"${{ needs.validate.outputs.normalized_tag }}\",/" "${file_path}"
actual=$(grep "version" "$file_path" | cut -d ':' -f 2 | tr -d ',' | tr -d '"' | tr -d ' ')
elif [[ "${file_name}" == "plugin.cfg" ]]; then
# Godot `version="VersionString"`
sed -i -e "s/\(version=\)\"\(.*\)\"/\1\"${{ needs.validate.outputs.normalized_tag }}\"/" "${file_path}"
actual=$(grep "version=" "$file_path" | cut -d '=' -f 2 | tr -d '"')
elif [[ "${file_name}" == "Directory.Build.props" ]]; then
# .NET `<VersionPrefix>VersionString</VersionPrefix>`
sed -i -e 's|<VersionPrefix>.*</VersionPrefix>|<VersionPrefix>${{ needs.validate.outputs.normalized_tag }}</VersionPrefix>|g' "${file_path}"
# -P is for perl regex, only available in GNU grep
actual=$(grep -oP '<VersionPrefix>\K.*(?=</VersionPrefix>)' "$file_path")
else
echo "Unknown file name ${file_name} is specified."
exit 1
fi
echo "::endgroup::"
echo "::group::After"
cat "$file_path"
echo "::endgroup::"
echo "::group::Validate Change"
if [[ "$actual" != "$expected" ]]; then
echo "Failed. Path: $file_path, Expected: $expected, Actual: $actual"
exit 1
else
echo "Success. Path: $file_path, Expected: $expected, Actual: $actual"
fi
echo "::endgroup::"
done <<< "${{ inputs.file-path }}"
# dotnet run
- uses: Cysharp/Actions/.github/actions/setup-dotnet@main
if: ${{ inputs.dotnet-run-path != '' }}
- name: Execute dotnet run with --version ${{ needs.validate.outputs.normalized_tag }}
run: |
while read -r file_path; do
if [[ "$file_path" == "" ]]; then continue; fi
echo "Start $file_path"
echo "::group::Execute"
dotnet run --project "$file_path" -- --version "${{ needs.validate.outputs.normalized_tag }}"
echo "::endgroup::"
done <<< "${{ inputs.dotnet-run-path }}"
if: ${{ inputs.dotnet-run-path != '' }}
- name: Check update on git
id: check_update
run: git diff --exit-code || echo "changed=1" | tee -a "$GITHUB_OUTPUT"
- name: Commit files (updated? = ${{ steps.check_update.outputs.changed == '1' }})
id: commit
run: |
if [[ "${{ steps.check_update.outputs.changed }}" == "1" ]]; then
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "feat: Update package.json to ${{ needs.validate.outputs.normalized_tag }}" -m "Commit by [GitHub Actions](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})" -a
echo "sha=$(git rev-parse HEAD)" | tee -a "$GITHUB_OUTPUT"
echo "is-branch-created=${{ inputs.dry-run }}" | tee -a "$GITHUB_OUTPUT"
elif [[ "${{ inputs.ref }}" != "" ]]; then
echo "sha=$(git rev-parse HEAD)" | tee -a "$GITHUB_OUTPUT"
echo "is-branch-created=false" | tee -a "$GITHUB_OUTPUT"
else
echo "sha=" | tee -a "$GITHUB_OUTPUT"
echo "is-branch-created=false" | tee -a "$GITHUB_OUTPUT"
fi
- name: Push changes
if: ${{ steps.check_update.outputs.changed == '1' }}
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }} # auto generated token
branch: ${{ inputs.ref }}
tags: false
force: ${{ inputs.dry-run }}