Skip to content

Build and publish container images #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/build-and-push-stable-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Build and Push Release Image

# Configures this workflow to run for every new version tag
on:
push:
tags: ["v*.*.*"]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

# Log into GHCR
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract version from tag name
# example: tag name `v1.0.0` sets env.VERSION=1.0.0
- name: Extract build args
run: |
echo "VERSION=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV

# Set up QEMU for cross-compiling
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

# Set up docker-buildx for cross-compiling
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
balzack/databag
ghcr.io/balzack/databag
tags: |
type=semver,pattern={{version}},value=${{ env.VERSION }}
type=semver,pattern={{major}}.{{minor}},value=${{ env.VERSION }}
type=raw,value=stable
flavor: |
latest=false
labels: |
org.opencontainers.image.version=${{ env.VERSION }}

# Build and push container image with associated labels and tags.
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# Generate attestation
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
69 changes: 69 additions & 0 deletions .github/workflows/build-and-push-test-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Build and Push Test Image

# Configures this workflow to run for every PR
on:
pull_request:
branches: ["main"]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

# Log into GHCR
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Set up QEMU for cross-compiling
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

# Set up docker-buildx for cross-compiling
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
balzack/databag
ghcr.io/balzack/databag
tags: |
type=raw,value=test
flavor: |
latest=false

# Build and push container image with associated labels and tags.
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# Generate attestation
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
20 changes: 9 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ FROM node:22-alpine AS node
WORKDIR /app

# Download the node dependencies first before adding the rest for caching
# target=/usr/local/share/.cache/yarn/v6 is the default cache directory for yarn.
# This can be obtained via `docker run -it node:22-alpine yarn cache dir`
# sharing=locked: this will force docker to wait for each parallel build in sequence.
# This prevents multi-architecture builds from running over each otther.
COPY ./net/web/package.json ./net/web/yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn \
RUN --mount=type=cache,target=/usr/local/share/.cache/yarn/v6,sharing=locked \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using sharing=locked could be an issue if 2 PRs are created at once

yarn --frozen-lockfile

COPY ./net/web/ ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn \
RUN --mount=type=cache,target=/usr/local/share/.cache/yarn/v6,sharing=locked \
yarn run build

FROM golang:alpine AS go
Expand All @@ -24,22 +28,16 @@ COPY ./net/server /app/databag/net/server
COPY ./net/transform /opt/databag/transform

WORKDIR /app/databag/net/server
RUN --mount=type=cache,target=/go/pkg/mod \
if [ -n "${DATABAG_GOARCH}" ]; then GOARCH=${DATABAG_GOARCH}; fi; \
if [ -n "${DATABAG_GOOS}" ]; then GOOS=${DATABAG_GOOS}; fi; \
go mod download
RUN --mount=type=cache,target=/go/pkg/mod go mod download

ARG DATABAG_GOARCH
ARG DATABAG_GOOS
Comment on lines +31 to 34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the args be placed first or doesn't that matter?


RUN --mount=type=cache,target=/go/pkg/mod \
if [ -n "${DATABAG_GOARCH}" ]; then GOARCH=${DATABAG_GOARCH}; fi; \
if [ -n "${DATABAG_GOOS}" ]; then GOOS=${DATABAG_GOOS}; fi; \
CGO_ENABLED=1 go build -o databag .
RUN --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=1 go build -o databag .

COPY --from=node /app/build /app/databag/net/web/build

ENV DEV=0
ENV ADMIN=password

ENTRYPOINT /app/databag/net/server/entrypoint.sh
ENTRYPOINT ["/app/databag/net/server/entrypoint.sh"]
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,31 @@ Read the [Design Guidelines](https://github.com/balzack/databag/blob/main/.desig

## Installation

To use databag, you will need a DNS name pointing to your node with a certificate. You can deploy a node manually, but you will have a much easier time using a container service. Containers for arm64 and amd64 are available [here](https://hub.docker.com/r/balzack/databag/tags).
To use databag, you will need a DNS name pointing to your node with a certificate. You can deploy a node manually, but you will have a much easier time using a container service. Containers for arm64 and amd64 are available [here](https://github.com/balzack/databag/pkgs/container/databag).

### Docker Compose

Launch with dockerhub container using docker compose:
#### Example Compose File
```shell
services:
databag:
container_name: databag
image: ghcr.io/balzack/databag:main
ports:
- "7000:7000"
volumes:
- ./data:/var/lib/databag
restart: unless-stopped
```

#### Example Docker Run Command
```shell
docker run -d \
-v "$(pwd)/data:/var/lib/databag" \
-p "7000:7000" \
ghcr.io/balzack/databag:main
```


#### Standard launch
```shell
Expand Down
8 changes: 0 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ name: databag
services:
app:
build: .

# # For building cross environment containers
# build:
# context: .
# args:
# DATABAG_GOARCH: arm64
# DATABAG_GOOS: linux
# platform: "linux/arm64"
Comment on lines -6 to -13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

ports:
- 127.0.0.1:7000:7000
volumes:
Expand Down
2 changes: 1 addition & 1 deletion net/repeater/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN go mod download
COPY . .
RUN go build -o repeater .
EXPOSE 7878
ENTRYPOINT ./repeater
ENTRYPOINT ["./repeater"]
Loading