Skip to content

Commit

Permalink
Chore(CI/CD): Add new workflow to create image for each opened PR
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkornel committed Aug 19, 2024
1 parent f533fea commit dfd5149
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/pr-image-builder.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit dfd5149

Please sign in to comment.