Skip to content
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

chore: add docker builds #16

Merged
merged 10 commits into from
Dec 19, 2023
Merged
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
98 changes: 98 additions & 0 deletions .github/workflows/pre-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
on:
push:
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10
name: Create prerelease w/ binaries and static assets
jobs:
build-push-hatchet-api:
name: hatchet-api
runs-on: ubuntu-latest
steps:
- name: Get tag name
id: tag_name
run: echo "tag=${GITHUB_TAG/refs\/tags\//}" >> $GITHUB_OUTPUT
env:
GITHUB_TAG: ${{ github.ref }}
- name: Checkout
uses: actions/checkout@v3
- name: Login to GHCR
id: login-ghcr
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build
run: |
DOCKER_BUILDKIT=1 docker build -f ./build/package/servers.Dockerfile \
-t ghcr.io/hatchet-dev/hatchet/hatchet-api:${{steps.tag_name.outputs.tag}} \
--build-arg SERVER_TARGET=api \
--build-arg VERSION=${{steps.tag_name.outputs.tag}} \
.
- name: Push to GHCR
run: |
docker push ghcr.io/hatchet-dev/hatchet/hatchet-api:${{steps.tag_name.outputs.tag}}
build-push-hatchet-engine:
name: hatchet-engine
runs-on: ubuntu-latest
steps:
- name: Get tag name
id: tag_name
run: echo "tag=${GITHUB_TAG/refs\/tags\//}" >> $GITHUB_OUTPUT
env:
GITHUB_TAG: ${{ github.ref }}
- name: Checkout
uses: actions/checkout@v3
- name: Login to GHCR
id: login-ghcr
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build
run: |
DOCKER_BUILDKIT=1 docker build -f ./build/package/servers.Dockerfile \
-t ghcr.io/hatchet-dev/hatchet/hatchet-engine:${{steps.tag_name.outputs.tag}} \
--build-arg SERVER_TARGET=engine \
--build-arg VERSION=${{steps.tag_name.outputs.tag}} \
.
- name: Push to GHCR
run: |
docker push ghcr.io/hatchet-dev/hatchet/hatchet-engine:${{steps.tag_name.outputs.tag}}
build-push-hatchet-migrate:
name: hatchet-migrate
runs-on: ubuntu-latest
steps:
- name: Get tag name
id: tag_name
run: echo "tag=${GITHUB_TAG/refs\/tags\//}" >> $GITHUB_OUTPUT
env:
GITHUB_TAG: ${{ github.ref }}
- name: Checkout
uses: actions/checkout@v3
- name: Login to GHCR
id: login-ghcr
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build
run: |
DOCKER_BUILDKIT=1 docker build -f ./build/package/migrate.Dockerfile \
-t ghcr.io/hatchet-dev/hatchet/hatchet-migrate:${{steps.tag_name.outputs.tag}} \
.
- name: Push to GHCR
run: |
docker push ghcr.io/hatchet-dev/hatchet/hatchet-migrate:${{steps.tag_name.outputs.tag}}
build-push-hatchet-frontend:
name: hatchet-frontend
runs-on: ubuntu-latest
steps:
- name: Get tag name
id: tag_name
run: echo "tag=${GITHUB_TAG/refs\/tags\//}" >> $GITHUB_OUTPUT
env:
GITHUB_TAG: ${{ github.ref }}
- name: Checkout
uses: actions/checkout@v3
- name: Login to GHCR
id: login-ghcr
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build
run: |
DOCKER_BUILDKIT=1 docker build -f ./build/package/frontend.Dockerfile \
-t ghcr.io/hatchet-dev/hatchet/hatchet-frontend:${{steps.tag_name.outputs.tag}} \
.
- name: Push to GHCR
run: |
docker push ghcr.io/hatchet-dev/hatchet/hatchet-frontend:${{steps.tag_name.outputs.tag}}
39 changes: 39 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
on:
release:
types: [released]
name: Release
jobs:
push-hatchet-server:
name: Push latest
runs-on: ubuntu-latest
steps:
- name: Get tag name
id: tag_name
run: |
tag=${GITHUB_TAG/refs\/tags\//}
echo ::set-output name=tag::$tag
env:
GITHUB_TAG: ${{ github.ref }}
- name: Login to GHCR
id: login-ghcr
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Pull and push hatchet-api
run: |
docker pull ghcr.io/hatchet-dev/hatchet/hatchet-api:${{steps.tag_name.outputs.tag}}
docker tag ghcr.io/hatchet-dev/hatchet/hatchet-api:${{steps.tag_name.outputs.tag}} ghcr.io/hatchet-dev/hatchet/hatchet-server:latest
docker push ghcr.io/hatchet-dev/hatchet/hatchet-api:latest
- name: Pull and push hatchet-engine
run: |
docker pull ghcr.io/hatchet-dev/hatchet/hatchet-engine:${{steps.tag_name.outputs.tag}}
docker tag ghcr.io/hatchet-dev/hatchet/hatchet-engine:${{steps.tag_name.outputs.tag}} ghcr.io/hatchet-dev/hatchet/hatchet-engine:latest
docker push ghcr.io/hatchet-dev/hatchet/hatchet-engine:latest
- name: Pull and push hatchet-frontend
run: |
docker pull ghcr.io/hatchet-dev/hatchet/hatchet-frontend:${{steps.tag_name.outputs.tag}}
docker tag ghcr.io/hatchet-dev/hatchet/hatchet-frontend:${{steps.tag_name.outputs.tag}} ghcr.io/hatchet-dev/hatchet/hatchet-frontend:latest
docker push ghcr.io/hatchet-dev/hatchet/hatchet-frontend:latest
- name: Pull and push hatchet-migrate
run: |
docker pull ghcr.io/hatchet-dev/hatchet/hatchet-migrate:${{steps.tag_name.outputs.tag}}
docker tag ghcr.io/hatchet-dev/hatchet/hatchet-migrate:${{steps.tag_name.outputs.tag}} ghcr.io/hatchet-dev/hatchet/hatchet-migrate:latest
docker push ghcr.io/hatchet-dev/hatchet/hatchet-migrate:latest
2 changes: 1 addition & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tasks:
- go run github.com/steebchen/prisma-client-go generate
generate-proto:
cmds:
- sh ./hack/dev/proto.sh
- sh ./hack/proto/proto.sh
generate-sqlc:
cmds:
- npx --yes prisma migrate diff --from-empty --to-schema-datasource prisma/schema.prisma --script > internal/repository/prisma/dbsqlc/schema.sql
Expand Down
44 changes: 23 additions & 21 deletions api/v1/server/run/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run

import (
"context"
"fmt"

"github.com/hatchet-dev/hatchet/api/v1/server/authn"
Expand Down Expand Up @@ -47,7 +48,9 @@ func NewAPIServer(config *server.ServerConfig) *APIServer {
}
}

func (t *APIServer) Run() error {
func (t *APIServer) Run(ctx context.Context) error {
errCh := make(chan error)

oaspec, err := gen.GetSwagger()
if err != nil {
return err
Expand Down Expand Up @@ -140,28 +143,27 @@ func (t *APIServer) Run() error {

gen.RegisterHandlers(e, myStrictApiHandler)

if err := e.Start(fmt.Sprintf(":%d", t.config.Runtime.Port)); err != nil {
go func() {
if err := e.Start(fmt.Sprintf(":%d", t.config.Runtime.Port)); err != nil {
errCh <- err
}
}()

Loop:
for {
select {
case err := <-errCh:
return err
case <-ctx.Done():
break Loop
}
}

err = e.Shutdown(ctx)

if err != nil {
return err
}

return nil
}

// func IDGetter[T any](getter func(id string) (T, error), parentGetter func(val T) string) populator.PopulatorFunc {
// return func(config *server.ServerConfig, parent *populator.PopulatedResourceNode, id string) (res *populator.PopulatorResult, err error) {
// gotVal, err := getter(id)
// if err != nil {
// return nil, err
// }

// res = &populator.PopulatorResult{
// Resource: gotVal,
// }

// if parentGetter != nil {
// res.ParentID = parentGetter(gotVal)
// }

// return res, nil
// }
// }
18 changes: 18 additions & 0 deletions build/package/frontend.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:18-alpine as build

WORKDIR /app
COPY ./frontend/app/package.json ./frontend/app/package-lock.json ./
RUN npm install
COPY ./frontend/app ./
RUN npm run build

# Stage 2: Serve the built app with NGINX
FROM nginx:alpine

ARG APP_TARGET=client

COPY ./build/package/nginx.conf /etc/nginx/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
9 changes: 9 additions & 0 deletions build/package/frontend.Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/frontend/**/node_modules
*.log
/frontend/**/.DS_Store
/frontend/**/.env
/frontend/**/.cache
/frontend/**/public/build
/frontend/**/build
/frontend/**/dist
/frontend/**/.turbo
16 changes: 16 additions & 0 deletions build/package/migrate.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Base Go environment
# -------------------
FROM golang:1.21-alpine as base
WORKDIR /hatchet

RUN apk update && apk add --no-cache gcc musl-dev git

COPY go.mod go.sum ./
COPY /prisma ./prisma

RUN go install github.com/steebchen/prisma-client-go@v0.31.2
Copy link
Collaborator

Choose a reason for hiding this comment

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

@abelanger5 We should just do go mod download, otherwise this version will get out of sync with the one in go.mod.


# prefetch the binaries, so that they will be cached and not downloaded on each change
RUN go run github.com/steebchen/prisma-client-go prefetch

CMD go run github.com/steebchen/prisma-client-go migrate deploy
16 changes: 16 additions & 0 deletions build/package/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
worker_processes 4;

events { worker_connections 1024; }

http {
server {
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
index index.html index.htm;

location / {
try_files $uri /index.html;
}
}
}
81 changes: 81 additions & 0 deletions build/package/servers.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Base Go environment
# -------------------
FROM golang:1.21-alpine as base
WORKDIR /hatchet

RUN apk update && apk add --no-cache gcc musl-dev git protoc protobuf-dev

COPY go.mod go.sum ./
COPY /api ./api
COPY /api-contracts ./api-contracts
COPY /internal ./internal
COPY /pkg ./pkg
COPY /hack ./hack
COPY /prisma ./prisma
COPY /cmd ./cmd

RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
RUN go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest

RUN --mount=type=cache,target=$GOPATH/pkg/mod \
go mod download

# prefetch the binaries, so that they will be cached and not downloaded on each change
RUN go run github.com/steebchen/prisma-client-go prefetch

# generate the Prisma Client Go client
RUN go run github.com/steebchen/prisma-client-go generate --generator go

# OpenAPI bundle environment
# -------------------------
FROM node:16-alpine as build-openapi
WORKDIR /openapi

COPY /api-contracts/openapi ./openapi

RUN npm install -g npm@8.1

RUN npm install -g @apidevtools/swagger-cli prisma

RUN swagger-cli bundle ./openapi/openapi.yaml --outfile ./bin/oas/openapi.yaml --type yaml

# Go build environment
# --------------------
FROM base AS build-go

ARG VERSION=v0.1.0-alpha.0

# can be set to "api" or "engine"
ARG SERVER_TARGET

# check if the target is empty or not set to api or engine
RUN if [ -z "$SERVER_TARGET" ] || [ "$SERVER_TARGET" != "api" ] && [ "$SERVER_TARGET" != "engine" ]; then \
echo "SERVER_TARGET must be set to 'api' or 'engine'"; \
exit 1; \
fi

RUN sh ./hack/proto/proto.sh

COPY --from=build-openapi /openapi/bin/oas/openapi.yaml ./bin/oas/openapi.yaml

# build oapi
RUN oapi-codegen -config ./api/v1/server/oas/gen/codegen.yaml ./bin/oas/openapi.yaml

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=$GOPATH/pkg/mod \
go build -ldflags="-w -s -X 'main.Version=${VERSION}'" -a -o ./bin/hatchet-${SERVER_TARGET} ./cmd/hatchet-${SERVER_TARGET}

# Deployment environment
# ----------------------
FROM alpine AS deployment

# can be set to "api" or "engine"
ARG SERVER_TARGET=engine

RUN apk update && apk add --no-cache gcc musl-dev

COPY --from=build-go /hatchet/bin/hatchet-${SERVER_TARGET} /hatchet/

EXPOSE 8080
CMD /hatchet/hatchet-${SERVER_TARGET}
Loading