From 61ee070c08f14e63300803c60a764e0ca7306b65 Mon Sep 17 00:00:00 2001 From: Merith-TK Date: Tue, 4 Feb 2025 17:23:53 +0000 Subject: [PATCH] Add OCI Build Script --- .forgejo/workflows/build-on-commit.yml | 40 ++++++++++++++++++++++ .forgejo/workflows/build-on-tag.yml | 37 ++++++++++++++++++++ .gitignore | 2 +- Dockerfile | 28 +++++++++++++++ config.go => cmd/simpleproxy/config.go | 0 main.go => cmd/simpleproxy/main.go | 0 proxyTCP.go => cmd/simpleproxy/proxyTCP.go | 0 proxyUDP.go => cmd/simpleproxy/proxyUDP.go | 0 8 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 .forgejo/workflows/build-on-commit.yml create mode 100644 .forgejo/workflows/build-on-tag.yml create mode 100644 Dockerfile rename config.go => cmd/simpleproxy/config.go (100%) rename main.go => cmd/simpleproxy/main.go (100%) rename proxyTCP.go => cmd/simpleproxy/proxyTCP.go (100%) rename proxyUDP.go => cmd/simpleproxy/proxyUDP.go (100%) diff --git a/.forgejo/workflows/build-on-commit.yml b/.forgejo/workflows/build-on-commit.yml new file mode 100644 index 0000000..e8f0d2e --- /dev/null +++ b/.forgejo/workflows/build-on-commit.yml @@ -0,0 +1,40 @@ +name: Build Docker Image on Commit + +on: + push: + branches: + - main + tags: + - '!' # Exclude tags + +jobs: + build-and-publish: + runs-on: docker-builder + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set REPO_VARS + id: repo-url + run: | + echo "REPO_HOST=$(echo "${{ github.server_url }}" | sed 's~http[s]*://~~g')" >> $GITHUB_ENV + echo "REPO_PATH=${{ github.repository }}" >> $GITHUB_ENV + + - name: Login to OCI registry + run: | + echo "${{ secrets.OCI_TOKEN }}" | docker login $REPO_HOST -u "${{ secrets.OCI_USER }}" --password-stdin + + - name: Build and push Docker images + run: | + # Build Docker image with commit SHA + docker build -t $REPO_HOST/$REPO_PATH:${{ github.sha }} . + docker push $REPO_HOST/$REPO_PATH:${{ github.sha }} + + # Build Docker image with nightly tag + docker tag $REPO_HOST/$REPO_PATH:${{ github.sha }} $REPO_HOST/$REPO_PATH:nightly + docker push $REPO_HOST/$REPO_PATH:nightly + + # Remove local images to save storage + docker rmi $REPO_HOST/$REPO_PATH:${{ github.sha }} + docker rmi $REPO_HOST/$REPO_PATH:nightly diff --git a/.forgejo/workflows/build-on-tag.yml b/.forgejo/workflows/build-on-tag.yml new file mode 100644 index 0000000..888102b --- /dev/null +++ b/.forgejo/workflows/build-on-tag.yml @@ -0,0 +1,37 @@ +name: Build and Publish Docker Image on Tag + +on: + push: + tags: + - '*' + +jobs: + build-and-publish: + runs-on: docker-builder + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set REPO_VARS + id: repo-url + run: | + echo "REPO_HOST=$(echo "${{ github.server_url }}" | sed 's~http[s]*://~~g')" >> $GITHUB_ENV + echo "REPO_PATH=${{ github.repository }}" >> $GITHUB_ENV + + - name: Login to OCI registry + run: | + echo "${{ secrets.OCI_TOKEN }}" | docker login $REPO_HOST -u "${{ secrets.OCI_USER }}" --password-stdin + + - name: Build and push Docker image + run: | + TAG=${{ github.ref_name }} # Get the tag name from the context + # Build and push multi-platform Docker images + docker build -t $REPO_HOST/$REPO_PATH:$TAG --push . + # Tag and push latest + docker tag $REPO_HOST/$REPO_PATH:$TAG $REPO_HOST/$REPO_PATH:latest + docker push $REPO_HOST/$REPO_PATH:latest + + # Remove the local image to save storage + docker rmi $REPO_HOST/$REPO_PATH:$TAG + docker rmi $REPO_HOST/$REPO_PATH:latest \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4ad0474..9ad30cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -simpleproxy* +/simpleproxy *.exe \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dfc07bd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# Build Stage +FROM golang:1.22-alpine AS builder + +WORKDIR /app + +# Install dependencies +RUN apk add --no-cache git + +# Copy go modules and download dependencies +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source files +COPY ./cmd/simpleproxy ./cmd/simpleproxy + +# Build the binary with optimizations +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o /app/simpleproxy ./cmd/simpleproxy + +# Final Minimal Image +FROM alpine:latest + +WORKDIR /root/ + +# Copy the compiled binary from the builder stage +COPY --from=builder /app/simpleproxy . + +# Run the binary +ENTRYPOINT ["./simpleproxy"] diff --git a/config.go b/cmd/simpleproxy/config.go similarity index 100% rename from config.go rename to cmd/simpleproxy/config.go diff --git a/main.go b/cmd/simpleproxy/main.go similarity index 100% rename from main.go rename to cmd/simpleproxy/main.go diff --git a/proxyTCP.go b/cmd/simpleproxy/proxyTCP.go similarity index 100% rename from proxyTCP.go rename to cmd/simpleproxy/proxyTCP.go diff --git a/proxyUDP.go b/cmd/simpleproxy/proxyUDP.go similarity index 100% rename from proxyUDP.go rename to cmd/simpleproxy/proxyUDP.go