Skip to content

Commit

Permalink
Add ARM64 Support and Optimize Build Process (#145)
Browse files Browse the repository at this point in the history
* Add ARM64 Support and Optimize Build Process

---------

Co-authored-by: Sam Lin <[email protected]>
  • Loading branch information
danielchristianschroeter and maxisam authored Nov 9, 2024
1 parent 10348e6 commit 89790e9
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-mongo-tool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ jobs:
tags: maxisam/mongo-tool:${{ env.VERSION }}
file: ./mongo-tool/Dockerfile
push: true
platforms: linux/amd64
platforms: linux/amd64,linux/arm64
cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/mongo-tool:buildcache-dev
cache-to: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/mongo-tool:buildcache-dev,mode=max
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
if: github.event_name == 'workflow_dispatch'
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
build-args: |
BUILD_DATE=${{ env.NOW }}
VCS_REF=${{ github.sha }}
Expand All @@ -99,6 +100,7 @@ jobs:
if: github.event_name == 'pull_request'
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
build-args: |
BUILD_DATE=${{ env.NOW }}
VCS_REF=${{ github.sha }}
Expand Down Expand Up @@ -140,6 +142,41 @@ jobs:
-v ${{ github.workspace }}/test/backups:/storage \
${{ github.repository }}:${{ env.APP_VERSION }}.${{ github.run_number }}
- name: Verify Mgob container is running
run: |
if [ "$(docker inspect -f '{{.State.Status}}' mgob)" != "running" ]; then
echo "Mgob container is not running!"
docker logs mgob
exit 1
fi
echo "Mgob container is running."
- name: Check Mgob container logs for startup errors
run: |
if docker logs mgob 2>&1 | grep -iq "error"; then
echo "Error found in Mgob startup logs!"
docker logs mgob
exit 1
fi
echo "No errors in Mgob startup logs."
- name: Wait for Mgob service to be ready
run: |
echo 'Waiting for Mgob to be ready...'
for i in {1..10}; do
if curl -s http://127.0.0.1:8090/version -o /dev/null; then
echo "Mgob is ready!"
break
elif [ "$i" -eq "10" ]; then
echo "Mgob not ready after multiple attempts."
docker logs mgob
exit 1
fi
echo "Waiting for Mgob... retrying in 5 seconds"
sleep 5
done
echo 'Mgob service is available.'
- name: Verify mgob backup for PR
run: |
sleep 90
Expand Down Expand Up @@ -252,7 +289,9 @@ jobs:
if: github.event_name == 'workflow_dispatch'
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
build-args: |
BUILDKIT_MULTI_PLATFORM=1
BUILD_DATE=${{ env.NOW }}
VCS_REF=${{ github.sha }}
VERSION=${{ env.APP_VERSION }}.${{ github.run_number }}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ github.repository }}:${{ needs.set-build-env.outputs.buildId }}-${{ matrix.feature }}
platforms: linux/amd64,linux/arm64
build-args: |
BUILDKIT_MULTI_PLATFORM=1
BUILD_DATE=${{ needs.set-build-env.outputs.now }}
VCS_REF=${{ github.sha }}
VERSION=${{ needs.set-build-env.outputs.buildId }}
Expand Down Expand Up @@ -101,7 +103,9 @@ jobs:
${{ github.repository }}:${{ needs.set-build-env.outputs.buildId }}-all
${{ github.repository }}:${{ needs.set-build-env.outputs.buildId }}
${{ github.repository }}:latest
platforms: linux/amd64,linux/arm64
build-args: |
BUILDKIT_MULTI_PLATFORM=1
BUILD_DATE=${{ env.NOW }}
VCS_REF=${{ github.sha }}
VERSION=${{ needs.set-build-env.outputs.buildId }}
Expand Down
101 changes: 67 additions & 34 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,70 +1,103 @@
# Define build arguments with default values
ARG MONGODB_TOOLS_VERSION=100.8.0
ARG EN_AWS_CLI=false
ARG AWS_CLI_VERSION=1.29.44
ARG EN_AZURE=false
ARG AZURE_CLI_VERSION=2.52.0
ARG AZURE_CLI_VERSION=2.63.0
ARG EN_GCLOUD=false
ARG GOOGLE_CLOUD_SDK_VERSION=445.0.0
ARG GOOGLE_CLOUD_SDK_VERSION=499.0.0
ARG EN_GPG=true
ARG GNUPG_VERSION="2.4.4-r0"
ARG EN_MINIO=false
ARG EN_RCLONE=false
ARG VERSION

# Stage 1: tools-builder stage for MongoDB tools
FROM maxisam/mongo-tool:${MONGODB_TOOLS_VERSION} AS tools-builder

FROM golang:1.21 AS mgob-builder
# Stage 2: mgob-builder stage for the mgob binary
FROM --platform=$BUILDPLATFORM golang:1.21 AS mgob-builder
ARG VERSION
COPY . /go/src/github.com/stefanprodan/mgob
ARG TARGETOS
ARG TARGETARCH

# Set environment variables for Go
ENV GOOS=${TARGETOS} \
GOARCH=${TARGETARCH} \
CGO_ENABLED=0

# Set working directory
WORKDIR /go/src/github.com/stefanprodan/mgob
RUN CGO_ENABLED=0 GOOS=linux go test ./pkg/... && \
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.version=$VERSION" -a -installsuffix cgo -o mgob github.com/stefanprodan/mgob/cmd/mgob

# Copy source code
COPY . .

# Build and test the mgob binary
RUN go test ./pkg/... && \
go build -ldflags "-X main.version=$VERSION" -o mgob ./cmd/mgob

# Stage 3: final image setup with Alpine
FROM alpine:3.18

# Define build arguments
ARG BUILD_DATE
ARG VCS_REF
ARG VERSION
ARG MONGODB_TOOLS_VERSION
ARG AWS_CLI_VERSION
ARG AZURE_CLI_VERSION
ARG GOOGLE_CLOUD_SDK_VERSION
ARG GNUPG_VERSION
ARG EN_AWS_CLI
ARG EN_AZURE
ARG EN_GCLOUD
ARG EN_GPG
ARG EN_MINIO
ARG EN_RCLONE
ENV MONGODB_TOOLS_VERSION=$MONGODB_TOOLS_VERSION \
GNUPG_VERSION=$GNUPG_VERSION \
GOOGLE_CLOUD_SDK_VERSION=$GOOGLE_CLOUD_SDK_VERSION \
AZURE_CLI_VERSION=$AZURE_CLI_VERSION \
AWS_CLI_VERSION=$AWS_CLI_VERSION \
MGOB_EN_AWS_CLI=$EN_AWS_CLI \
MGOB_EN_AZURE=$EN_AZURE \
MGOB_EN_GCLOUD=$EN_GCLOUD \
MGOB_EN_GPG=$EN_GPG \
MGOB_EN_MINIO=$EN_MINIO \
MGOB_EN_RCLONE=$EN_RCLONE

# Set environment variables
ENV MONGODB_TOOLS_VERSION=${MONGODB_TOOLS_VERSION} \
GOOGLE_CLOUD_SDK_VERSION=${GOOGLE_CLOUD_SDK_VERSION} \
AZURE_CLI_VERSION=${AZURE_CLI_VERSION} \
AWS_CLI_VERSION=${AWS_CLI_VERSION} \
MGOB_EN_AWS_CLI=${EN_AWS_CLI} \
MGOB_EN_AZURE=${EN_AZURE} \
MGOB_EN_GCLOUD=${EN_GCLOUD} \
MGOB_EN_GPG=${EN_GPG} \
MGOB_EN_MINIO=${EN_MINIO} \
MGOB_EN_RCLONE=${EN_RCLONE}

# Set working directory
WORKDIR /

COPY build.sh /tmp
RUN /tmp/build.sh
# Copy and run the build script
COPY build.sh /tmp/
RUN chmod +x /tmp/build.sh && /tmp/build.sh

# Set the PATH for Google Cloud SDK
ENV PATH="/google-cloud-sdk/bin:${PATH}"
COPY --from=mgob-builder /go/src/github.com/stefanprodan/mgob/mgob .
COPY --from=tools-builder /go/mongo-tools/bin/* /usr/bin/

# Copy the mgob binary from the builder
COPY --from=mgob-builder /go/src/github.com/stefanprodan/mgob/mgob /usr/local/bin/
RUN chmod +x /usr/local/bin/mgob

# Copy MongoDB tools from the tools-builder
COPY --from=tools-builder /usr/local/bin/ /usr/bin/

# Install necessary runtime dependencies for MongoDB tools
RUN apk add --no-cache krb5-libs

# Volumes for storage
VOLUME ["/storage", "/tmp", "/data"]

LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="mgob" \
org.label-schema.description="MongoDB backup automation tool" \
org.label-schema.url="https://github.com/stefanprodan/mgob" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/stefanprodan/mgob" \
org.label-schema.vendor="stefanprodan.com,maxisam" \
org.label-schema.version=$VERSION \
org.label-schema.schema-version="1.0"

ENTRYPOINT [ "./mgob" ]
# Labels for image metadata
LABEL org.label-schema.build-date=${BUILD_DATE} \
org.label-schema.name="mgob" \
org.label-schema.description="MongoDB backup automation tool" \
org.label-schema.url="https://github.com/stefanprodan/mgob" \
org.label-schema.vcs-ref=${VCS_REF} \
org.label-schema.vcs-url="https://github.com/stefanprodan/mgob" \
org.label-schema.vendor="stefanprodan.com,maxisam" \
org.label-schema.version=${VERSION} \
org.label-schema.schema-version="1.0"

# Entry point for the mgob application
ENTRYPOINT [ "/usr/local/bin/mgob" ]
Loading

0 comments on commit 89790e9

Please sign in to comment.