Skip to content

Commit

Permalink
Fix push images failing (#14)
Browse files Browse the repository at this point in the history
* Add start script

* Fix ui Dockerfile not building correctly

* Remove cache steps

* Update builds to run separately

* Re-add caching step

* Add cleanup-registry workflow
  • Loading branch information
sneakycrow authored Nov 9, 2024
1 parent ada0269 commit 826fc9b
Show file tree
Hide file tree
Showing 5 changed files with 389 additions and 42 deletions.
80 changes: 65 additions & 15 deletions .github/workflows/build-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ on:
workflow_dispatch:

jobs:
build-and-push:
setup:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

outputs:
version: ${{ steps.version.outputs.VERSION }}
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -22,6 +20,16 @@ jobs:
id: version
run: echo "VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

build-api:
needs: setup
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
Expand All @@ -33,7 +41,6 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

# Build and push API image
- name: Build and push API image
uses: docker/build-push-action@v5
with:
Expand All @@ -43,11 +50,34 @@ jobs:
platforms: linux/amd64
tags: |
${{ secrets.DO_REGISTRY }}/${{ vars.API_IMAGE }}:latest
${{ secrets.DO_REGISTRY }}/${{ vars.API_IMAGE }}:${{ steps.version.outputs.VERSION }}
${{ secrets.DO_REGISTRY }}/${{ vars.API_IMAGE }}:${{ needs.setup.outputs.version }}
cache-from: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ vars.API_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ vars.API_IMAGE }}:buildcache,mode=max

# Build and push Queue image
- name: Image digest
run: echo "API image digest $(doctl registry repository digest-list ${{ vars.API_IMAGE }} --format Tag,Digest --no-header | grep latest)"

build-queue:
needs: setup
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DO_REGISTRY_KEY }}

- name: Log in to DO Container Registry
run: doctl registry login --expiry-seconds 600

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Queue image
uses: docker/build-push-action@v5
with:
Expand All @@ -57,11 +87,34 @@ jobs:
platforms: linux/amd64
tags: |
${{ secrets.DO_REGISTRY }}/${{ vars.QUEUE_IMAGE }}:latest
${{ secrets.DO_REGISTRY }}/${{ vars.QUEUE_IMAGE }}:${{ steps.version.outputs.VERSION }}
${{ secrets.DO_REGISTRY }}/${{ vars.QUEUE_IMAGE }}:${{ needs.setup.outputs.version }}
cache-from: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ vars.QUEUE_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ vars.QUEUE_IMAGE }}:buildcache,mode=max

# Build and push UI image
- name: Image digest
run: echo "Queue image digest $(doctl registry repository digest-list ${{ vars.QUEUE_IMAGE }} --format Tag,Digest --no-header | grep latest)"

build-ui:
needs: setup
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DO_REGISTRY_KEY }}

- name: Log in to DO Container Registry
run: doctl registry login --expiry-seconds 600

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push UI image
uses: docker/build-push-action@v5
with:
Expand All @@ -71,12 +124,9 @@ jobs:
platforms: linux/amd64
tags: |
${{ secrets.DO_REGISTRY }}/${{ vars.UI_IMAGE }}:latest
${{ secrets.DO_REGISTRY }}/${{ vars.UI_IMAGE }}:${{ steps.version.outputs.VERSION }}
${{ secrets.DO_REGISTRY }}/${{ vars.UI_IMAGE }}:${{ needs.setup.outputs.version }}
cache-from: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ vars.UI_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ vars.UI_IMAGE }}:buildcache,mode=max

- name: Image digest
run: |
echo "API image digest: $(doctl registry repository digest-list ${{ vars.API_IMAGE }} --format Tag,Digest --no-header | grep latest)"
echo "Queue image digest: $(doctl registry repository digest-list ${{ vars.QUEUE_IMAGE }} --format Tag,Digest --no-header | grep latest)"
echo "UI image digest: $(doctl registry repository digest-list ${{ vars.UI_IMAGE }} --format Tag,Digest --no-header | grep latest)"
run: echo "UI image digest $(doctl registry repository digest-list ${{ vars.UI_IMAGE }} --format Tag,Digest --no-header | grep latest)"
54 changes: 54 additions & 0 deletions .github/workflows/cleanup-registry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: cleanup registry

on:
schedule:
- cron: "0 0 * * 0" # Run weekly at midnight on Sunday
workflow_dispatch: # Allow manual triggers

jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DO_REGISTRY_KEY }}

- name: Clean up API images
run: |
# Get all tags except 'latest' and 'buildcache', sort by creation date, and keep only the newest 3
TAGS_TO_DELETE=$(doctl registry repository list-tags ${{ vars.API_IMAGE }} --format Tag --no-header | grep -v -E '^(latest|buildcache)$' | sort -r | tail -n +4)
# Delete old tags if they exist
if [ ! -z "$TAGS_TO_DELETE" ]; then
echo "Deleting old API tags: $TAGS_TO_DELETE"
for TAG in $TAGS_TO_DELETE; do
doctl registry repository delete-tag ${{ vars.API_IMAGE }} $TAG --force
done
fi
- name: Clean up Queue images
run: |
TAGS_TO_DELETE=$(doctl registry repository list-tags ${{ vars.QUEUE_IMAGE }} --format Tag --no-header | grep -v -E '^(latest|buildcache)$' | sort -r | tail -n +4)
if [ ! -z "$TAGS_TO_DELETE" ]; then
echo "Deleting old Queue tags: $TAGS_TO_DELETE"
for TAG in $TAGS_TO_DELETE; do
doctl registry repository delete-tag ${{ vars.QUEUE_IMAGE }} $TAG --force
done
fi
- name: Clean up UI images
run: |
TAGS_TO_DELETE=$(doctl registry repository list-tags ${{ vars.UI_IMAGE }} --format Tag --no-header | grep -v -E '^(latest|buildcache)$' | sort -r | tail -n +4)
if [ ! -z "$TAGS_TO_DELETE" ]; then
echo "Deleting old UI tags: $TAGS_TO_DELETE"
for TAG in $TAGS_TO_DELETE; do
doctl registry repository delete-tag ${{ vars.UI_IMAGE }} $TAG --force
done
fi
39 changes: 21 additions & 18 deletions config/ui.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
# Build stage
FROM --platform=$BUILDPLATFORM node:20-slim as builder
FROM --platform=$BUILDPLATFORM node:22-slim as builder

# Set working directory
WORKDIR /app

# Copy package files
COPY services/barn-ui/package.json services/barn-ui/yarn.lock ./
# Copy root package.json and yarn.lock
COPY package.json yarn.lock ./

# Install dependencies
RUN yarn install --frozen-lockfile
# Copy UI package.json
COPY services/barn-ui/package.json services/barn-ui/
COPY services/barn-ui/yarn.lock services/barn-ui/

# Copy source files
COPY services/barn-ui .
# Install all dependencies (including dev dependencies)
RUN yarn install

# Build the application
RUN yarn build
# Copy UI source code
COPY services/barn-ui/ services/barn-ui/

# Build the UI application
RUN yarn workspace web build

# Runtime stage
FROM --platform=$TARGETPLATFORM node:20-slim
FROM --platform=$TARGETPLATFORM node:22-slim

# Set working directory
WORKDIR /app

# Copy package files for production dependencies
COPY services/barn-ui/package.json services/barn-ui/yarn.lock ./

# Install production dependencies only
RUN yarn install --production --frozen-lockfile
# Copy only production dependencies
COPY --from=builder /app/services/barn-ui/package.json ./
COPY --from=builder /app/services/barn-ui/yarn.lock ./
RUN yarn install --production

# Copy built application from builder
COPY --from=builder /app/build ./build
# Copy built application
COPY --from=builder /app/services/barn-ui/build ./build

# Expose default SvelteKit port
# Expose port
EXPOSE 3000

# Start the application
Expand Down
1 change: 1 addition & 0 deletions services/barn-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"start": "node build",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"format": "prettier --write .",
Expand Down
Loading

0 comments on commit 826fc9b

Please sign in to comment.