Skip to content

Commit

Permalink
Build Docker image in CI
Browse files Browse the repository at this point in the history
Adds a workflow to build the included Dockerfile on PRs and merges to
`main`. Additionally, adds a `-version` flag to the binary, which makes
it print out `argv[0]` and the `debug.BuildInfo` ([1]) embedded in the
binary. This is just so that there's some way to run the binary such
that it will exit with status 0 immediately, which lets us verify in CI
that the container was built properly. We also embed the git SHA into a
label on the container image.

[1]: https://pkg.go.dev/runtime/debug#BuildInfo
  • Loading branch information
tgeoghegan committed May 9, 2024
1 parent 17d3a61 commit c98c15e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: docker

on:
push:
branches: [ main ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

env:
DOCKER_BUILDKIT: 1

jobs:
ohttp-gateway:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "GIT_REVISION=$(git describe --always --dirty=-modified)" >> $GITHUB_ENV
- run: docker build \
--tag "privacy-gateway-server-go" \
--build-arg GIT_REVISION=${GIT_REVISION} \
-f Dockerfile \
.
- run: docker run --rm --tag "privacy-gateway-server-go" -version
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \

FROM gcr.io/distroless/static

ARG GIT_REVISION=unknown
LABEL revision ${GIT_REVISION}
COPY --from=build /privacy-gateway-server /privacy-gateway-server

EXPOSE 8080
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"bytes"
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"runtime/debug"
"strconv"
"strings"

Expand Down Expand Up @@ -59,6 +61,8 @@ const (
logSecretsEnvironmentVariable = "LOG_SECRETS"
)

var versionFlag = flag.Bool("version", false, "print name and version to stdout")

type gatewayServer struct {
requestLabel string
responseLabel string
Expand Down Expand Up @@ -124,6 +128,18 @@ func getStringEnv(key string, defaultVal string) string {
}

func main() {
flag.Parse()

if *versionFlag {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
log.Printf("could not determine build info")
os.Exit(1)
}
fmt.Printf("%s\n%+v", os.Args[0], buildInfo)
os.Exit(0)
}

port := os.Getenv("PORT")
if port == "" {
port = defaultPort
Expand Down

0 comments on commit c98c15e

Please sign in to comment.