From 90104e9db84ba490ae54b925377a705b100ddb17 Mon Sep 17 00:00:00 2001 From: peterxcli Date: Fri, 2 Feb 2024 22:16:22 +0800 Subject: [PATCH] feat: add action to build image on tag --- .github/workflows/build-backend-image.yaml | 39 +++++++++++++++++++++ .github/workflows/build-notifier-image.yaml | 39 +++++++++++++++++++++ Dockerfile => Dockerfile_Backend | 0 Dockerfile_Notifier | 26 ++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 .github/workflows/build-backend-image.yaml create mode 100644 .github/workflows/build-notifier-image.yaml rename Dockerfile => Dockerfile_Backend (100%) create mode 100644 Dockerfile_Notifier diff --git a/.github/workflows/build-backend-image.yaml b/.github/workflows/build-backend-image.yaml new file mode 100644 index 0000000..5ae51ab --- /dev/null +++ b/.github/workflows/build-backend-image.yaml @@ -0,0 +1,39 @@ +name: Build and Push Docker Image on Tag + +on: + push: + tags: + - '*' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }}-backend + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Log into the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for the Docker image + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push the Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile_Backend + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/build-notifier-image.yaml b/.github/workflows/build-notifier-image.yaml new file mode 100644 index 0000000..a226e6f --- /dev/null +++ b/.github/workflows/build-notifier-image.yaml @@ -0,0 +1,39 @@ +name: Build and Push Docker Image on Tag + +on: + push: + tags: + - '*' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }}-notifier + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Log into the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for the Docker image + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push the Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile_Notifier + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile_Backend similarity index 100% rename from Dockerfile rename to Dockerfile_Backend diff --git a/Dockerfile_Notifier b/Dockerfile_Notifier new file mode 100644 index 0000000..a39c3a5 --- /dev/null +++ b/Dockerfile_Notifier @@ -0,0 +1,26 @@ +# Build Stage +FROM golang:1.21 AS build + +# Set the working directory +WORKDIR /app + +# Copy all files to the working directory +COPY . . + +# Download all dependencies +RUN go mod download + +# Set CGO_ENABLED to 1 +# ENV CGO_ENABLED=1 + +# Build the Go application +RUN CGO_ENABLED=1 GOOS=linux go build -o /app/main ./cmd/notification.go + +# Runtime Stage +FROM busybox AS runtime + +# Copy the binary from the build stage to the runtime stage +COPY --from=build /app/main /app + +# Set the entry point to execute the binary directly +ENTRYPOINT ["/app"]