Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 686a47b

Browse files
committed
feat: add docker images
Borrows the most minimal set of conventions from Kubo and IPFS Cluster: - git tags are translated to Docker tags with the same names - every commit on main branch is tagged 'main-YYYY-DD-MM-commit'
1 parent edc8e70 commit 686a47b

File tree

8 files changed

+237
-4
lines changed

8 files changed

+237
-4
lines changed

.github/workflows/docker-image.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Publish Docker image
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- 'main'
8+
tags:
9+
- 'v*'
10+
11+
jobs:
12+
push_to_registry:
13+
if: github.repository == 'ipfs/bifrost-gateway' || github.event_name == 'workflow_dispatch'
14+
name: Push Docker image to Docker Hub
15+
runs-on: ubuntu-latest
16+
env:
17+
IMAGE_NAME: ipfs/bifrost-gateway
18+
steps:
19+
- name: Check out the repo
20+
uses: actions/checkout@v2
21+
22+
- name: Set up QEMU
23+
uses: docker/setup-qemu-action@v1
24+
25+
- name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v1
27+
28+
- name: Get tags
29+
id: tags
30+
run: |
31+
echo "value<<EOF" >> $GITHUB_OUTPUT
32+
./docker/get-docker-tags.sh "$(date -u +%F)" >> $GITHUB_OUTPUT
33+
echo "EOF" >> $GITHUB_OUTPUT
34+
shell: bash
35+
36+
- name: Log in to Docker Hub
37+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
38+
with:
39+
username: ${{ vars.DOCKER_USERNAME }}
40+
password: ${{ secrets.DOCKER_PASSWORD }}
41+
42+
- name: Build Docker image and publish to Docker Hub
43+
uses: docker/build-push-action@v2
44+
with:
45+
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
46+
context: .
47+
push: true
48+
file: ./Dockerfile
49+
tags: "${{ steps.tags.outputs.value }}"

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.swp
2+
*.out
3+
*.coverprofile
4+
*.test
5+
*.orig
6+
*~
7+
8+
.tarball
9+
10+
bifrost-gateway

Dockerfile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
FROM golang:1.19-buster AS builder
2+
MAINTAINER IPFS Stewards <[email protected]>
3+
4+
# This dockerfile builds and runs bifrost-gateway
5+
6+
ENV GOPATH /go
7+
ENV SRC_PATH $GOPATH/src/github.com/ipfs/bifrost-gateway
8+
ENV GO111MODULE on
9+
ENV GOPROXY https://proxy.golang.org
10+
11+
ENV SUEXEC_VERSION v0.2
12+
ENV TINI_VERSION v0.19.0
13+
RUN set -eux; \
14+
dpkgArch="$(dpkg --print-architecture)"; \
15+
case "${dpkgArch##*-}" in \
16+
"amd64" | "armhf" | "arm64") tiniArch="tini-static-$dpkgArch" ;;\
17+
*) echo >&2 "unsupported architecture: ${dpkgArch}"; exit 1 ;; \
18+
esac; \
19+
cd /tmp \
20+
&& git clone https://github.com/ncopa/su-exec.git \
21+
&& cd su-exec \
22+
&& git checkout -q $SUEXEC_VERSION \
23+
&& make su-exec-static \
24+
&& cd /tmp \
25+
&& wget -q -O tini https://github.com/krallin/tini/releases/download/$TINI_VERSION/$tiniArch \
26+
&& chmod +x tini
27+
28+
# Get the TLS CA certificates, they're not provided by busybox.
29+
RUN apt-get update && apt-get install -y ca-certificates
30+
31+
COPY --chown=1000:users go.* $SRC_PATH/
32+
WORKDIR $SRC_PATH
33+
RUN go mod download
34+
35+
COPY --chown=1000:users . $SRC_PATH
36+
RUN git config --global --add safe.directory /go/src/github.com/ipfs/bifrost-gateway
37+
RUN go install
38+
39+
40+
#------------------------------------------------------
41+
FROM busybox:1-glibc
42+
MAINTAINER IPFS Stewards <[email protected]>
43+
44+
ENV GOPATH /go
45+
ENV SRC_PATH /go/src/github.com/ipfs/bifrost-gateway
46+
ENV BIFROST_GATEWAY_PATH /data/bifrost-gateway
47+
48+
EXPOSE 9094
49+
EXPOSE 9095
50+
EXPOSE 9096
51+
52+
COPY --from=builder $GOPATH/bin/bifrost-gateway /usr/local/bin/bifrost-gateway
53+
COPY --from=builder $SRC_PATH/docker/entrypoint.sh /usr/local/bin/entrypoint.sh
54+
COPY --from=builder /tmp/su-exec/su-exec-static /sbin/su-exec
55+
COPY --from=builder /tmp/tini /sbin/tini
56+
COPY --from=builder /etc/ssl/certs /etc/ssl/certs
57+
58+
RUN mkdir -p $BIFROST_GATEWAY_PATH && \
59+
adduser -D -h $BIFROST_GATEWAY_PATH -u 1000 -G users ipfs && \
60+
chown ipfs:users $BIFROST_GATEWAY_PATH
61+
62+
VOLUME $BIFROST_GATEWAY_PATH
63+
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/entrypoint.sh"]
64+
65+
# Defaults for bifrost-gateway go here
66+
CMD ["--todo", "${TODO}"]

docker/entrypoint.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
set -e
4+
user=ipfs
5+
6+
if [ -n "$DOCKER_DEBUG" ]; then
7+
set -x
8+
fi
9+
10+
if [ `id -u` -eq 0 ]; then
11+
echo "Changing user to $user"
12+
exec su-exec "$user" "$0" $@
13+
fi
14+
15+
# Only ipfs user can get here
16+
bifrost-gateway --version
17+
18+
exec bifrost-gateway $@

docker/get-docker-tags.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
3+
# get-docker-tags.sh produces Docker tags for the current build
4+
#
5+
# Usage:
6+
# ./get-docker-tags.sh <build number> <git commit sha1> <git branch name> [git tag name]
7+
#
8+
# Example:
9+
#
10+
# # get tag for the main branch
11+
# ./get-docker-tags.sh $(date -u +%F) testingsha main
12+
#
13+
# # get tag for a release tag
14+
# ./get-docker-tags.sh $(date -u +%F) testingsha release v0.5.0
15+
#
16+
# # Serving suggestion in CI
17+
# ./get-docker-tags.sh $(date -u +%F) "$CI_SHA1" "$CI_BRANCH" "$CI_TAG"
18+
#
19+
set -euo pipefail
20+
21+
if [[ $# -lt 1 ]] ; then
22+
echo 'At least 1 arg required.'
23+
echo 'Usage:'
24+
echo './get-docker-tags.sh <build number> [git commit sha1] [git branch name] [git tag name]'
25+
exit 1
26+
fi
27+
28+
BUILD_NUM=$1
29+
GIT_SHA1=${2:-$(git rev-parse HEAD)}
30+
GIT_SHA1_SHORT=$(echo "$GIT_SHA1" | cut -c 1-7)
31+
GIT_BRANCH=${3:-$(git symbolic-ref -q --short HEAD || echo "unknown")}
32+
GIT_TAG=${4:-$(git describe --tags --exact-match 2> /dev/null || echo "")}
33+
34+
IMAGE_NAME=${IMAGE_NAME:-ipfs/bifrost-gateway}
35+
36+
echoImageName () {
37+
local IMAGE_TAG=$1
38+
echo "$IMAGE_NAME:$IMAGE_TAG"
39+
}
40+
41+
if [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+-rc ]]; then
42+
echoImageName "$GIT_TAG"
43+
44+
elif [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
45+
echoImageName "$GIT_TAG"
46+
echoImageName "latest"
47+
echoImageName "release" # see: https://github.com/ipfs/go-ipfs/issues/3999#issuecomment-742228981
48+
49+
elif [ "$GIT_BRANCH" = "main" ]; then
50+
echoImageName "main-${BUILD_NUM}-${GIT_SHA1_SHORT}"
51+
echoImageName "main-latest"
52+
53+
else
54+
echo "Nothing to do. No docker tag defined for branch: $GIT_BRANCH, tag: $GIT_TAG"
55+
56+
fi

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/protocol/bifrost-gateway
1+
module github.com/ipfs/bifrost-gateway
22

33
go 1.19
44

@@ -12,7 +12,7 @@ require (
1212
github.com/ipfs/go-ipfs-exchange-offline v0.3.0
1313
github.com/ipfs/go-ipld-format v0.4.0
1414
github.com/ipfs/go-ipns v0.3.0
15-
github.com/ipfs/go-libipfs v0.4.1-0.20230207021459-1a932f7bb3c1
15+
github.com/ipfs/go-libipfs v0.4.1-0.20230208030004-93dd0a02a18b
1616
github.com/ipfs/go-merkledag v0.9.0
1717
github.com/ipfs/go-namesys v0.7.0
1818
github.com/ipfs/go-path v0.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ github.com/ipfs/go-ipld-legacy v0.1.1 h1:BvD8PEuqwBHLTKqlGFTHSwrwFOMkVESEvwIYwR2
392392
github.com/ipfs/go-ipld-legacy v0.1.1/go.mod h1:8AyKFCjgRPsQFf15ZQgDB8Din4DML/fOmKZkkFkrIEg=
393393
github.com/ipfs/go-ipns v0.3.0 h1:ai791nTgVo+zTuq2bLvEGmWP1M0A6kGTXUsgv/Yq67A=
394394
github.com/ipfs/go-ipns v0.3.0/go.mod h1:3cLT2rbvgPZGkHJoPO1YMJeh6LtkxopCkKFcio/wE24=
395-
github.com/ipfs/go-libipfs v0.4.1-0.20230207021459-1a932f7bb3c1 h1:drLEvJmJM0UrXvQfMF84EqSeGSXl5Elk/PAcc0XnNb4=
396-
github.com/ipfs/go-libipfs v0.4.1-0.20230207021459-1a932f7bb3c1/go.mod h1:XKRXmSlJ32qlpxGN+mdGJJXUl/Z055etN1xpMEaANQ8=
395+
github.com/ipfs/go-libipfs v0.4.1-0.20230208030004-93dd0a02a18b h1:Vuib04xACEG9UXmdbg+q2kojDDlSBe1vSyPW77Q1Y1U=
396+
github.com/ipfs/go-libipfs v0.4.1-0.20230208030004-93dd0a02a18b/go.mod h1:XKRXmSlJ32qlpxGN+mdGJJXUl/Z055etN1xpMEaANQ8=
397397
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
398398
github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk=
399399
github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A=

main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
_ "embed"
45
"errors"
56
"fmt"
67
"log"
78
"math/rand"
89
"net/http"
910
"os"
1011
"os/signal"
12+
"runtime/debug"
1113
"strconv"
1214
"strings"
1315
"sync"
@@ -42,6 +44,7 @@ func init() {
4244

4345
var rootCmd = &cobra.Command{
4446
Use: "bifrost-gateway",
47+
Version: buildVersion(),
4548
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
4649
Short: "IPFS Gateway implementation for https://github.com/protocol/bifrost-infra",
4750
RunE: func(cmd *cobra.Command, args []string) error {
@@ -69,7 +72,9 @@ var rootCmd = &cobra.Command{
6972
defer wg.Done()
7073

7174
log.Printf("Path gateway listening on http://127.0.0.1:%d", gatewayPort)
75+
log.Printf(" Smoke test (JPG): http://127.0.0.1:%d/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", gatewayPort)
7276
log.Printf("Subdomain gateway listening on dweb.link and http://localhost:%d", gatewayPort)
77+
log.Printf(" Smoke test (Subdomain+DNSLink+UnixFS+HAMT): http://localhost:%d/ipns/en.wikipedia-on-ipfs.org/", gatewayPort)
7378
log.Printf("Legacy RPC at /api/v0 provided by %s", strings.Join(kuboRPC, " "))
7479
err := gatewaySrv.ListenAndServe()
7580
if err != nil && !errors.Is(err, http.ErrServerClosed) {
@@ -170,3 +175,32 @@ func newAPIHandler(endpoints []string) http.Handler {
170175
http.Redirect(w, r, endpoint+r.URL.Path+"?"+r.URL.RawQuery, http.StatusFound)
171176
})
172177
}
178+
179+
func buildVersion() string {
180+
var revision string
181+
var day string
182+
var dirty bool
183+
184+
info, ok := debug.ReadBuildInfo()
185+
if !ok {
186+
return "(unknown)"
187+
}
188+
for _, kv := range info.Settings {
189+
switch kv.Key {
190+
case "vcs.revision":
191+
revision = kv.Value[:7]
192+
case "vcs.time":
193+
t, _ := time.Parse(time.RFC3339, kv.Value)
194+
day = t.UTC().Format("2006-01-02")
195+
case "vcs.modified":
196+
dirty = kv.Value == "true"
197+
}
198+
}
199+
if dirty {
200+
revision += "-dirty"
201+
}
202+
if revision != "" {
203+
return day + "-" + revision
204+
}
205+
return "(unknown)"
206+
}

0 commit comments

Comments
 (0)