Skip to content

Instantly share code, notes, and snippets.

@abdulsaheel
Created October 21, 2024 05:17
Show Gist options
  • Save abdulsaheel/4d94b200d6c1b894217af4447c2f018d to your computer and use it in GitHub Desktop.
Save abdulsaheel/4d94b200d6c1b894217af4447c2f018d to your computer and use it in GitHub Desktop.
Docker Buildx getting while compiling rust package for arm arch in amd64 host
# Build stage
FROM rust:1.81 as builder
WORKDIR /app
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
ENV RUSTFLAGS="-C opt-level=0 -C target-feature=+lse -C target-cpu=native"
COPY . .
RUN cargo fetch
# Build with profiling enabled
RUN cargo build --release --timings -j $(nproc)
RUN mv /app/docker.settings.toml /app/Settings.toml
# Final stage
FROM gcr.io/distroless/cc-debian12
COPY --from=builder /app/target/release/relayer /
COPY --from=builder /app/Settings.toml /
EXPOSE 4426
CMD ["./relayer"]
name: Manually Triggered Self-Hosted Docker Build with Custom Tag
on:
workflow_dispatch:
inputs:
tag:
description: 'Tag for Docker image'
required: true
default: 'latest'
env:
RESOURCE_GROUP: RESOURCE_GROUP
VM_NAME: VM_NAME
jobs:
start-vm:
name: Start Azure VM
runs-on: ubuntu-latest
steps:
# Log in to Azure
- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Start Azure VM
- name: Start Azure VM
id: start-vm
run: |
echo "Starting the VM..."
az vm start --resource-group $RESOURCE_GROUP --name $VM_NAME
# Wait for VM to be running
- name: Wait for VM to be running
id: wait-for-vm
run: |
MAX_RETRIES=5
RETRY_INTERVAL=15 # seconds
for ((i=0; i<MAX_RETRIES; i++)); do
STATUS=$(az vm show -d --resource-group $RESOURCE_GROUP --name $VM_NAME --query "powerState" -o tsv)
echo "Current VM Status: $STATUS"
if [ "$STATUS" == "VM running" ]; then
echo "VM is running."
exit 0
fi
echo "Waiting for VM to be running... ($((i + 1))/$MAX_RETRIES)"
sleep $RETRY_INTERVAL
done
echo "VM did not start within the expected time."
exit 1
run-job:
name: Build and Push Docker Image on Self-Hosted Azure VM
needs: start-vm
runs-on: self-hosted-azure
steps:
- name: 'Cleanup build folder'
run: |
ls -la ./
rm -rf ./* || true
rm -rf ./.??* || true
ls -la ./
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
- name: Build and push Docker images
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/arm64,linux/amd64
push: true
tags: |
ghcr.io/my_org_name/repo_name:latest
ghcr.io/my_org_name/repo_name:${{ github.event.inputs.tag }}
# Docker Cleanup Step
- name: Docker system cleanup
run: |
echo "Running Docker cleanup to free up space..."
docker system prune -af --volumes
docker rmi $(docker images -q -f "dangling=true") || true
# Workspace Cleanup Step
- name: Workspace cleanup
run: |
echo "Cleaning up the workspace to remove residual files..."
rm -rf ./* || true
rm -rf ./.??* || true
# Clear System Logs and Temporary Files
- name: Clear system logs and temp files
run: |
echo "Clearing system logs and temporary files to free up disk space..."
sudo journalctl --vacuum-time=1d
sudo rm -rf /var/tmp/*
sudo rm -rf /tmp/*
sudo rm -rf /var/log/*.log
stop-vm:
name: Stop Azure VM
needs: run-job
runs-on: ubuntu-latest
if: always() # Ensure that VM is stopped even if the build fails
steps:
- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deallocate Azure VM
id: deallocate-vm
run: |
echo "Deallocating the VM..."
az vm deallocate --resource-group $RESOURCE_GROUP --name $VM_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment