diff --git a/.github/workflows/Manual_Publish_Docker.yml b/.github/workflows/Manual_Publish_Docker.yml index 881867221..7a5fb0841 100644 --- a/.github/workflows/Manual_Publish_Docker.yml +++ b/.github/workflows/Manual_Publish_Docker.yml @@ -27,10 +27,15 @@ on: description: 'Release as latest' type: boolean default: false + pull_request: + types: [opened, synchronize, reopened] + pull_request_target: + types: [closed] permissions: contents: read packages: write + pull-requests: read env: REGISTRY: ghcr.io @@ -43,6 +48,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target' outputs: new_release_published: ${{ steps.semantic.outputs.new_release_published }} new_release_version: ${{ steps.semantic.outputs.new_release_version }} @@ -100,19 +106,16 @@ jobs: fi setup: - needs: semantic-release + needs: [semantic-release] + if: always() runs-on: ubuntu-latest outputs: - version: ${{ steps.gitversion.outputs.semVer }} - branchName: ${{ steps.gitversion.outputs.branchName }} - buildMeta: ${{ steps.gitversion.outputs.buildMetadata }} - build_base: ${{ inputs.build_base || inputs.build_all }} - build_build: ${{ inputs.build_build || inputs.build_all }} - build_sm: ${{ inputs.build_sm || inputs.build_all }} + version: ${{ steps.set-version.outputs.version }} + build_base: ${{ inputs.build_base || inputs.build_all || github.event_name == 'pull_request' }} + build_build: ${{ inputs.build_build || inputs.build_all || github.event_name == 'pull_request' }} + build_sm: ${{ inputs.build_sm || inputs.build_all || github.event_name == 'pull_request' }} skip_release_main_build: ${{ inputs.skip_release_main_build }} release_as_latest: ${{ inputs.release_as_latest }} - semantic_version: ${{ needs.semantic-release.outputs.new_release_version }} - is_release: ${{ needs.semantic-release.outputs.new_release_published }} steps: - uses: actions/checkout@v4 @@ -120,19 +123,33 @@ jobs: fetch-depth: 0 - name: Install GitVersion + if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target' uses: gittools/actions/gitversion/setup@v0 with: versionSpec: "5.x" - name: Determine Version id: gitversion + if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target' uses: gittools/actions/gitversion/execute@v0 + - name: Set version + id: set-version + run: | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "version=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT + else + echo "version=${{ needs.semantic-release.outputs.new_release_version || steps.gitversion.outputs.semVer }}" >> $GITHUB_OUTPUT + fi + test: needs: [setup] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 - name: Generate code hash id: hash @@ -162,22 +179,25 @@ jobs: . build: - needs: [setup, semantic-release, test] + needs: [setup, test] runs-on: ubuntu-latest + if: github.event_name != 'pull_request_target' env: - VERSION: ${{ needs.setup.outputs.semantic_version || needs.setup.outputs.version }} - BRANCH_NAME: ${{ needs.setup.outputs.branchName }} + VERSION: ${{ needs.setup.outputs.version }} BUILD_BASE: ${{ needs.setup.outputs.build_base }} BUILD_BUILD: ${{ needs.setup.outputs.build_build }} BUILD_SM: ${{ needs.setup.outputs.build_sm }} SKIP_RELEASE_MAIN_BUILD: ${{ needs.setup.outputs.skip_release_main_build }} - RELEASE_AS_LATEST: ${{ needs.setup.outputs.release_as_latest || needs.setup.outputs.is_release }} + RELEASE_AS_LATEST: ${{ needs.setup.outputs.release_as_latest }} steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 - name: Download AssemblyInfo.cs - if: needs.semantic-release.outputs.new_release_published == 'true' + if: github.event_name != 'pull_request' && needs.semantic-release.outputs.new_release_published == 'true' uses: actions/download-artifact@v4 with: name: assembly-info @@ -218,4 +238,25 @@ jobs: echo "FROM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-sm AS sm" > Dockerfile echo "FROM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-base AS base" >> Dockerfile cat Dockerfile.template >> Dockerfile - 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 . \ No newline at end of file + 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 . + + cleanup: + runs-on: ubuntu-latest + if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true + steps: + - name: Delete PR images + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + PR_NUMBER="${{ github.event.pull_request.number }}" + VERSION="pr-${PR_NUMBER}" + + for IMAGE_TYPE in "-base" "-build" "-sm" ""; do + IMAGE_TAG="${VERSION}${IMAGE_TYPE}" + + gh api --method DELETE "/user/packages/container/${BASE_IMAGE_NAME}/versions/${IMAGE_TAG}" || true + + if [ -z "${IMAGE_TYPE}" ]; then + gh api --method DELETE "/user/packages/container/${FINAL_IMAGE_NAME}/versions/${IMAGE_TAG}" || true + fi + done