up to git
This commit is contained in:
+65
-19
@@ -1,40 +1,86 @@
|
|||||||
# Multi-stage build yang lebih optimal
|
# # Multi-stage build yang lebih optimal
|
||||||
FROM golang:1.22-alpine AS build
|
# FROM golang:1.22-alpine AS build
|
||||||
|
|
||||||
# Install build dependencies
|
# # Install build dependencies
|
||||||
RUN apk add --no-cache git ca-certificates tzdata
|
# RUN apk add --no-cache git ca-certificates tzdata
|
||||||
|
|
||||||
|
# WORKDIR /build
|
||||||
|
|
||||||
|
# # Cache go mod dependencies
|
||||||
|
# COPY go.mod go.sum ./
|
||||||
|
# RUN go mod download && go mod verify
|
||||||
|
|
||||||
|
# # Copy source code
|
||||||
|
# COPY . .
|
||||||
|
|
||||||
|
# # Build dengan optimasi
|
||||||
|
# RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
||||||
|
# go build -a -installsuffix cgo \
|
||||||
|
# -ldflags='-w -s -extldflags "-static"' \
|
||||||
|
# -o main cmd/api/main.go
|
||||||
|
|
||||||
|
# # Final stage - distroless untuk keamanan
|
||||||
|
# FROM gcr.io/distroless/static:nonroot
|
||||||
|
|
||||||
|
# # Copy timezone data
|
||||||
|
# COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
|
||||||
|
|
||||||
|
# # Copy binary
|
||||||
|
# COPY --from=build /build/main /app/main
|
||||||
|
|
||||||
|
# # Use non-root user
|
||||||
|
# USER nonroot:nonroot
|
||||||
|
|
||||||
|
# # Health check
|
||||||
|
# HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||||
|
# CMD ["/app/main", "-health"] || exit 1
|
||||||
|
|
||||||
|
# EXPOSE 8080
|
||||||
|
|
||||||
|
# ENTRYPOINT ["/app/main"]
|
||||||
|
|
||||||
|
# syntax=docker/dockerfile:1
|
||||||
|
|
||||||
|
# Build stage
|
||||||
|
FROM golang:1.25-alpine AS build
|
||||||
|
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
git \
|
||||||
|
ca-certificates \
|
||||||
|
tzdata
|
||||||
|
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
|
|
||||||
# Cache go mod dependencies
|
# Cache dependency layer
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download && go mod verify
|
RUN go mod download && go mod verify
|
||||||
|
|
||||||
# Copy source code
|
# Copy source code
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Build dengan optimasi
|
# Build seluruh package, bukan hanya satu file main.go
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
RUN CGO_ENABLED=0 \
|
||||||
go build -a -installsuffix cgo \
|
GOOS=linux \
|
||||||
-ldflags='-w -s -extldflags "-static"' \
|
GOARCH=amd64 \
|
||||||
-o main cmd/api/main.go
|
go build \
|
||||||
|
-trimpath \
|
||||||
|
-ldflags="-s -w" \
|
||||||
|
-o /out/main \
|
||||||
|
./cmd/api
|
||||||
|
|
||||||
# Final stage - distroless untuk keamanan
|
# Runtime stage
|
||||||
FROM gcr.io/distroless/static:nonroot
|
FROM gcr.io/distroless/static:nonroot
|
||||||
|
|
||||||
# Copy timezone data
|
# Timezone dan CA certificates
|
||||||
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
|
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
|
||||||
|
COPY --from=build /etc/ssl/certs/ca-certificates.crt \
|
||||||
|
/etc/ssl/certs/ca-certificates.crt
|
||||||
|
|
||||||
# Copy binary
|
# Application binary
|
||||||
COPY --from=build /build/main /app/main
|
COPY --from=build /out/main /app/main
|
||||||
|
|
||||||
# Use non-root user
|
|
||||||
USER nonroot:nonroot
|
USER nonroot:nonroot
|
||||||
|
|
||||||
# Health check
|
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
||||||
CMD ["/app/main", "-health"] || exit 1
|
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
ENTRYPOINT ["/app/main"]
|
ENTRYPOINT ["/app/main"]
|
||||||
Executable
+28
@@ -0,0 +1,28 @@
|
|||||||
|
!/bin/bash
|
||||||
|
|
||||||
|
#get image name
|
||||||
|
remote_url=$(git remote get-url origin)
|
||||||
|
image=$(echo $remote_url | sed 's|https://||g; s|.git||g')
|
||||||
|
|
||||||
|
#get branch name
|
||||||
|
branch_name=$(git rev-parse --abbrev-ref HEAD)
|
||||||
|
clean_branch_name=${branch_name##*/}
|
||||||
|
|
||||||
|
#get timestamp for the tag
|
||||||
|
timestamp=$(date +%Y%m%d%H%M%S)
|
||||||
|
|
||||||
|
app_version=$clean_branch_name-$timestamp
|
||||||
|
tag=$image:$timestamp-$clean_branch_name
|
||||||
|
latest=$image:latest-$clean_branch_name
|
||||||
|
|
||||||
|
#build image
|
||||||
|
docker build --build-arg APP_VERSION=$app_version -t $tag .
|
||||||
|
docker tag $tag $latest
|
||||||
|
|
||||||
|
#push to dockerhub
|
||||||
|
docker login git.rssa.top -u stim -p 4fde63b07906e7bfa6b3493d76d153a3981039b9
|
||||||
|
docker push $tag
|
||||||
|
docker push $latest
|
||||||
|
|
||||||
|
#remove dangling images
|
||||||
|
docker system prune -f
|
||||||
Reference in New Issue
Block a user