From dfd5149b51d68affe3212b23bf667b238412b318 Mon Sep 17 00:00:00 2001 From: Kornel David Date: Mon, 19 Aug 2024 22:20:42 +0200 Subject: [PATCH] Chore(CI/CD): Add new workflow to create image for each opened PR --- .github/workflows/pr-image-builder.yaml | 67 +++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .github/workflows/pr-image-builder.yaml diff --git a/.github/workflows/pr-image-builder.yaml b/.github/workflows/pr-image-builder.yaml new file mode 100644 index 0000000..a7b787f --- /dev/null +++ b/.github/workflows/pr-image-builder.yaml @@ -0,0 +1,67 @@ +name: Build and Push Docker Image for PR + +on: + pull_request: + types: [opened, synchronize, reopened] + +jobs: + + build_image_and_push_to_registry: + if: github.event.action != 'closed' && github.repository == 'l7mp/stunner' + name: Push Docker image to DockerHub + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USER }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build and Push Docker Image + id: docker_build + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: | + l7mp/stunner:pr-${{ github.event.pull_request.number }} + + - name: Comment on PR + if: success() && github.event_name == 'pull_request' + uses: actions/github-script@v6 + with: + script: | + const prNumber = context.issue.number; + const comment = `The Docker image for this pull request (#${prNumber}) has been successfully built and pushed to DockerHub as \`docker.io/l7mp/stunner:pr-${prNumber}\`.`; + github.rest.issues.createComment({ + issue_number: prNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: comment, + }); + + delete_docker_image_tag: + if: github.event.action == 'closed' && github.repository == 'l7mp/stunner' + name: Delete Docker Image Tag from DockerHub + runs-on: ubuntu-latest + steps: + - name: Delete Docker Image Tag + env: + DOCKER_USER: ${{ secrets.DOCKER_USER }} + DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} + run: | + PR_NUMBER=${{ github.event.pull_request.number }} + curl -s -u "$DOCKER_USER:$DOCKER_TOKEN" \ + -X DELETE "https://hub.docker.com/v2/repositories/l7mp/stunner/tags/pr-$PR_NUMBER/" \ + -w "HTTP Response Code: %{http_code}\n" \ No newline at end of file