Skip to content

Commit a11b5c3

Browse files
committed
feat: Support for PR images
1 parent bd60a1d commit a11b5c3

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

.github/workflows/Manual_Publish_Docker.yml

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ on:
2727
description: 'Release as latest'
2828
type: boolean
2929
default: false
30+
pull_request:
31+
types: [opened, synchronize, reopened]
32+
pull_request_target:
33+
types: [closed]
3034

3135
permissions:
3236
contents: read
3337
packages: write
38+
pull-requests: read
3439

3540
env:
3641
REGISTRY: ghcr.io
@@ -43,6 +48,7 @@ jobs:
4348
runs-on: ubuntu-latest
4449
permissions:
4550
contents: write
51+
if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target'
4652
outputs:
4753
new_release_published: ${{ steps.semantic.outputs.new_release_published }}
4854
new_release_version: ${{ steps.semantic.outputs.new_release_version }}
@@ -100,39 +106,50 @@ jobs:
100106
fi
101107
102108
setup:
103-
needs: semantic-release
109+
needs: [semantic-release]
110+
if: always()
104111
runs-on: ubuntu-latest
105112
outputs:
106-
version: ${{ steps.gitversion.outputs.semVer }}
107-
branchName: ${{ steps.gitversion.outputs.branchName }}
108-
buildMeta: ${{ steps.gitversion.outputs.buildMetadata }}
109-
build_base: ${{ inputs.build_base || inputs.build_all }}
110-
build_build: ${{ inputs.build_build || inputs.build_all }}
111-
build_sm: ${{ inputs.build_sm || inputs.build_all }}
113+
version: ${{ steps.set-version.outputs.version }}
114+
build_base: ${{ inputs.build_base || inputs.build_all || github.event_name == 'pull_request' }}
115+
build_build: ${{ inputs.build_build || inputs.build_all || github.event_name == 'pull_request' }}
116+
build_sm: ${{ inputs.build_sm || inputs.build_all || github.event_name == 'pull_request' }}
112117
skip_release_main_build: ${{ inputs.skip_release_main_build }}
113118
release_as_latest: ${{ inputs.release_as_latest }}
114-
semantic_version: ${{ needs.semantic-release.outputs.new_release_version }}
115-
is_release: ${{ needs.semantic-release.outputs.new_release_published }}
116119

117120
steps:
118121
- uses: actions/checkout@v4
119122
with:
120123
fetch-depth: 0
121124

122125
- name: Install GitVersion
126+
if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target'
123127
uses: gittools/actions/gitversion/setup@v0
124128
with:
125129
versionSpec: "5.x"
126130

127131
- name: Determine Version
128132
id: gitversion
133+
if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target'
129134
uses: gittools/actions/gitversion/execute@v0
130135

136+
- name: Set version
137+
id: set-version
138+
run: |
139+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
140+
echo "version=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
141+
else
142+
echo "version=${{ needs.semantic-release.outputs.new_release_version || steps.gitversion.outputs.semVer }}" >> $GITHUB_OUTPUT
143+
fi
144+
131145
test:
132146
needs: [setup]
133147
runs-on: ubuntu-latest
134148
steps:
135149
- uses: actions/checkout@v4
150+
with:
151+
ref: ${{ github.event.pull_request.head.sha }}
152+
fetch-depth: 0
136153

137154
- name: Generate code hash
138155
id: hash
@@ -162,22 +179,25 @@ jobs:
162179
.
163180
164181
build:
165-
needs: [setup, semantic-release, test]
182+
needs: [setup, test]
166183
runs-on: ubuntu-latest
184+
if: github.event_name != 'pull_request_target'
167185
env:
168-
VERSION: ${{ needs.setup.outputs.semantic_version || needs.setup.outputs.version }}
169-
BRANCH_NAME: ${{ needs.setup.outputs.branchName }}
186+
VERSION: ${{ needs.setup.outputs.version }}
170187
BUILD_BASE: ${{ needs.setup.outputs.build_base }}
171188
BUILD_BUILD: ${{ needs.setup.outputs.build_build }}
172189
BUILD_SM: ${{ needs.setup.outputs.build_sm }}
173190
SKIP_RELEASE_MAIN_BUILD: ${{ needs.setup.outputs.skip_release_main_build }}
174-
RELEASE_AS_LATEST: ${{ needs.setup.outputs.release_as_latest || needs.setup.outputs.is_release }}
191+
RELEASE_AS_LATEST: ${{ needs.setup.outputs.release_as_latest }}
175192

176193
steps:
177194
- uses: actions/checkout@v4
195+
with:
196+
ref: ${{ github.event.pull_request.head.sha }}
197+
fetch-depth: 0
178198

179199
- name: Download AssemblyInfo.cs
180-
if: needs.semantic-release.outputs.new_release_published == 'true'
200+
if: github.event_name != 'pull_request' && needs.semantic-release.outputs.new_release_published == 'true'
181201
uses: actions/download-artifact@v4
182202
with:
183203
name: assembly-info
@@ -218,4 +238,25 @@ jobs:
218238
echo "FROM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-sm AS sm" > Dockerfile
219239
echo "FROM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-base AS base" >> Dockerfile
220240
cat Dockerfile.template >> Dockerfile
221-
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.FINAL_IMAGE_NAME }}:${{ env.VERSION }} -t ${{ env.REGISTRY }}/${{ env.FINAL_IMAGE_NAME }}:${{ env.RELEASE_AS_LATEST == 'true' && 'latest' || env.BRANCH_NAME }} -f Dockerfile --push .
241+
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.FINAL_IMAGE_NAME }}:${{ env.VERSION }} -t ${{ env.REGISTRY }}/${{ env.FINAL_IMAGE_NAME }}:${{ env.RELEASE_AS_LATEST == 'true' && 'latest' || env.BRANCH_NAME }} -f Dockerfile --push .
242+
243+
cleanup:
244+
runs-on: ubuntu-latest
245+
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true
246+
steps:
247+
- name: Delete PR images
248+
env:
249+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
250+
run: |
251+
PR_NUMBER="${{ github.event.pull_request.number }}"
252+
VERSION="pr-${PR_NUMBER}"
253+
254+
for IMAGE_TYPE in "-base" "-build" "-sm" ""; do
255+
IMAGE_TAG="${VERSION}${IMAGE_TYPE}"
256+
257+
gh api --method DELETE "/user/packages/container/${BASE_IMAGE_NAME}/versions/${IMAGE_TAG}" || true
258+
259+
if [ -z "${IMAGE_TYPE}" ]; then
260+
gh api --method DELETE "/user/packages/container/${FINAL_IMAGE_NAME}/versions/${IMAGE_TAG}" || true
261+
fi
262+
done

0 commit comments

Comments
 (0)