Skip to content

Commit 86038d0

Browse files
authored
add package permission (#11)
1 parent 8836d96 commit 86038d0

1 file changed

Lines changed: 123 additions & 122 deletions

File tree

.github/workflows/release-zip.yml

Lines changed: 123 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,128 @@
11
name: Build and Release ZIP
22

33
on:
4-
pull_request:
5-
types: [closed]
6-
branches: [main]
4+
pull_request:
5+
types: [closed]
6+
branches: [main]
77

88
jobs:
9-
release:
10-
if: github.event.pull_request.merged == true
11-
runs-on: ubuntu-latest
12-
permissions:
13-
contents: write
14-
env:
15-
DOTNET_VERSION: 9.0
16-
PROJECT: StarMapLoader/StarMapLoader.csproj
17-
OUTPUT_PATH: ./publish
18-
NUGET_SOURCE: "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
19-
EXCLUDE: "*.pdb *.xml DummyProgram.deps.json DummyProgram.runtimeconfig.json DummyProgram.pdb DummyProgram.exe Tomlyn.dll"
20-
21-
steps:
22-
- uses: actions/checkout@v4
23-
with:
24-
fetch-depth: 0
25-
fetch-tags: true
26-
27-
- uses: actions/setup-dotnet@v4
28-
with:
29-
dotnet-version: ${{ env.DOTNET_VERSION }}
30-
31-
- name: Determine version bump
32-
id: version
33-
run: |
34-
# Be defensive and robust: use awk to split version numbers and always quote $GITHUB_OUTPUT
35-
git fetch --tags || true
36-
current=$(git describe --tags --abbrev=0 2>/dev/null || true)
37-
if [ -z "$current" ]; then
38-
echo "No tags found, defaulting to v0.0.0"
39-
current="v0.0.0"
40-
fi
41-
42-
# Remove leading 'v' then extract parts using awk
43-
ver=${current#v}
44-
major=$(printf "%s" "$ver" | awk -F. '{print $1+0}')
45-
minor=$(printf "%s" "$ver" | awk -F. '{print $2+0}')
46-
patch=$(printf "%s" "$ver" | awk -F. '{print $3+0}')
47-
48-
# Determine bump type from PR labels (safe interpolation)
49-
LABELS="${{ join(github.event.pull_request.labels.*.name, ' ') }}"
50-
if printf "%s" "$LABELS" | grep -q "major"; then
51-
major=$((major+1)); minor=0; patch=0; type="major"
52-
elif printf "%s" "$LABELS" | grep -q "minor"; then
53-
minor=$((minor+1)); patch=0; type="minor"
54-
else
55-
patch=$((patch+1)); type="patch"
56-
fi
57-
58-
new_version="${major}.${minor}.${patch}"
59-
echo "Next version: $new_version"
60-
61-
# Export outputs for later steps
62-
echo "prev=$prev_version" >> $GITHUB_OUTPUT
63-
echo "type=$bump_type" >> $GITHUB_OUTPUT
64-
echo "new=$new_version" >> $GITHUB_OUTPUT
65-
66-
- name: Tag and push new version
67-
run: |
68-
git config user.name "github-actions"
69-
git config user.email "actions@github.com"
70-
71-
NEW_VERSION="${{ steps.version.outputs.new }}"
72-
git tag "$NEW_VERSION"
73-
git push origin "$NEW_VERSION"
74-
75-
- name: Build
76-
run: dotnet publish ${{ env.PROJECT }} -c Release -o ${{ env.OUTPUT_PATH }}
77-
78-
- name: Ensure zip is available
79-
run: |
80-
sudo apt-get update -y
81-
sudo apt-get install -y zip
82-
83-
- name: Package ZIP
84-
run: |
85-
cd ${{ env.OUTPUT_PATH }}
86-
zip -r $GITHUB_WORKSPACE/StarMap-${{ steps.version.outputs.new }}.zip . -x ${{ env.EXCLUDE }}
87-
cd -
88-
89-
- name: Create GitHub Release
90-
uses: softprops/action-gh-release@v2
91-
with:
92-
tag_name: ${{ steps.version.outputs.new }}
93-
name: Release ${{ steps.version.outputs.new }}
94-
body: |
95-
Automated release for version ${{ steps.version.outputs.new }}
96-
Triggered by PR #${{ github.event.pull_request.number }}
97-
files: StarMap-${{ steps.version.outputs.new }}.zip
98-
99-
- name: Check whether StarMap.API changed since previous tag
100-
id: api_check
101-
run: |
102-
current="${{ steps.version.outputs.new }}"
103-
prev="${{ steps.version.outputs.prev }}"
104-
105-
echo "previous_tag=$prev" >> $GITHUB_OUTPUT
106-
107-
if [ -z "$prev" ]; then
108-
echo "changed=true" >> $GITHUB_OUTPUT
109-
exit 0
110-
fi
111-
112-
diff=$(git diff --name-only "$prev" "$current")
113-
echo "diff_files=$diff" >> $GITHUB_OUTPUT
114-
115-
if echo "$diff" | grep -qE '^StarMap.API/|^StarMap.API.csproj'; then
116-
echo "changed=true" >> $GITHUB_OUTPUT
117-
else
118-
echo "changed=false" >> $GITHUB_OUTPUT
119-
fi
120-
121-
- name: Pack and push StarMap.API (if changed)
122-
if: steps.api_check.outputs.changed == 'true'
123-
run: |
124-
dotnet restore StarMap.API/StarMap.API.csproj
125-
dotnet pack StarMap.API/StarMap.API.csproj -c Release -o ./nupkg /p:PackageVersion=${{ steps.version.outputs.new }}
126-
dotnet nuget add source --username "${{ github.actor }}" --password "${{ secrets.GITHUB_TOKEN }}" --store-password-in-clear-text --name github "${{ env.NUGET_SOURCE }}"
127-
dotnet nuget push ./nupkg/*.nupkg --source github --api-key "${{ secrets.GITHUB_TOKEN }}" --skip-duplicate
9+
release:
10+
if: github.event.pull_request.merged == true
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
packages: write
15+
env:
16+
DOTNET_VERSION: 9.0
17+
PROJECT: StarMapLoader/StarMapLoader.csproj
18+
OUTPUT_PATH: ./publish
19+
NUGET_SOURCE: "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
20+
EXCLUDE: "*.pdb *.xml DummyProgram.deps.json DummyProgram.runtimeconfig.json DummyProgram.pdb DummyProgram.exe Tomlyn.dll"
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
fetch-tags: true
27+
28+
- uses: actions/setup-dotnet@v4
29+
with:
30+
dotnet-version: ${{ env.DOTNET_VERSION }}
31+
32+
- name: Determine version bump
33+
id: version
34+
run: |
35+
# Be defensive and robust: use awk to split version numbers and always quote $GITHUB_OUTPUT
36+
git fetch --tags || true
37+
current=$(git describe --tags --abbrev=0 2>/dev/null || true)
38+
if [ -z "$current" ]; then
39+
echo "No tags found, defaulting to v0.0.0"
40+
current="v0.0.0"
41+
fi
42+
43+
# Remove leading 'v' then extract parts using awk
44+
ver=${current#v}
45+
major=$(printf "%s" "$ver" | awk -F. '{print $1+0}')
46+
minor=$(printf "%s" "$ver" | awk -F. '{print $2+0}')
47+
patch=$(printf "%s" "$ver" | awk -F. '{print $3+0}')
48+
49+
# Determine bump type from PR labels (safe interpolation)
50+
LABELS="${{ join(github.event.pull_request.labels.*.name, ' ') }}"
51+
if printf "%s" "$LABELS" | grep -q "major"; then
52+
major=$((major+1)); minor=0; patch=0; type="major"
53+
elif printf "%s" "$LABELS" | grep -q "minor"; then
54+
minor=$((minor+1)); patch=0; type="minor"
55+
else
56+
patch=$((patch+1)); type="patch"
57+
fi
58+
59+
new_version="${major}.${minor}.${patch}"
60+
echo "Next version: $new_version"
61+
62+
# Export outputs for later steps
63+
echo "prev=$prev_version" >> $GITHUB_OUTPUT
64+
echo "type=$bump_type" >> $GITHUB_OUTPUT
65+
echo "new=$new_version" >> $GITHUB_OUTPUT
66+
67+
- name: Tag and push new version
68+
run: |
69+
git config user.name "github-actions"
70+
git config user.email "actions@github.com"
71+
72+
NEW_VERSION="${{ steps.version.outputs.new }}"
73+
git tag "$NEW_VERSION"
74+
git push origin "$NEW_VERSION"
75+
76+
- name: Build
77+
run: dotnet publish ${{ env.PROJECT }} -c Release -o ${{ env.OUTPUT_PATH }}
78+
79+
- name: Ensure zip is available
80+
run: |
81+
sudo apt-get update -y
82+
sudo apt-get install -y zip
83+
84+
- name: Package ZIP
85+
run: |
86+
cd ${{ env.OUTPUT_PATH }}
87+
zip -r $GITHUB_WORKSPACE/StarMap-${{ steps.version.outputs.new }}.zip . -x ${{ env.EXCLUDE }}
88+
cd -
89+
90+
- name: Create GitHub Release
91+
uses: softprops/action-gh-release@v2
92+
with:
93+
tag_name: ${{ steps.version.outputs.new }}
94+
name: Release ${{ steps.version.outputs.new }}
95+
body: |
96+
Automated release for version ${{ steps.version.outputs.new }}
97+
Triggered by PR #${{ github.event.pull_request.number }}
98+
files: StarMap-${{ steps.version.outputs.new }}.zip
99+
100+
- name: Check whether StarMap.API changed since previous tag
101+
id: api_check
102+
run: |
103+
current="${{ steps.version.outputs.new }}"
104+
prev="${{ steps.version.outputs.prev }}"
105+
106+
echo "previous_tag=$prev" >> $GITHUB_OUTPUT
107+
108+
if [ -z "$prev" ]; then
109+
echo "changed=true" >> $GITHUB_OUTPUT
110+
exit 0
111+
fi
112+
113+
diff=$(git diff --name-only "$prev" "$current")
114+
echo "diff_files=$diff" >> $GITHUB_OUTPUT
115+
116+
if echo "$diff" | grep -qE '^StarMap.API/|^StarMap.API.csproj'; then
117+
echo "changed=true" >> $GITHUB_OUTPUT
118+
else
119+
echo "changed=false" >> $GITHUB_OUTPUT
120+
fi
121+
122+
- name: Pack and push StarMap.API (if changed)
123+
if: steps.api_check.outputs.changed == 'true'
124+
run: |
125+
dotnet restore StarMap.API/StarMap.API.csproj
126+
dotnet pack StarMap.API/StarMap.API.csproj -c Release -o ./nupkg /p:PackageVersion=${{ steps.version.outputs.new }}
127+
dotnet nuget add source --username "${{ github.actor }}" --password "${{ secrets.GITHUB_TOKEN }}" --store-password-in-clear-text --name github "${{ env.NUGET_SOURCE }}"
128+
dotnet nuget push ./nupkg/*.nupkg --source github --api-key "${{ secrets.GITHUB_TOKEN }}" --skip-duplicate

0 commit comments

Comments
 (0)