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

Sample of building with ko #346

Merged
merged 10 commits into from
Jul 7, 2023
Merged
5 changes: 4 additions & 1 deletion .github/workflows/compose-migrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Install ko
uses: ko-build/[email protected]

- name: Start containers
run: docker-compose up -d --build
run: KO_DOCKER_REPO=ko.local make run-docker services="-d postgres migrate"

- name: Wait for the migrations to complete
timeout-minutes: 1
Expand Down
44 changes: 4 additions & 40 deletions .github/workflows/image-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Login to ghcr.io
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: ko-build/[email protected]

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- run: |
ko build --platform=linux/amd64,linux/arm64 --push=${{ github.ref == 'refs/heads/main' }} ./cmd/server \
--image-label=org.opencontainers.image.source=https://github.com/stacklok/mediator,org.opencontainers.image.title="Stacklok Mediator",org.opencontainers.image.licenses=Apache-2.0,org.opencontainers.image.vendor=Stacklok

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

- name: Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/stacklok/mediator
tags: |
type=semver,prefix=v,pattern={{version}}
type=sha,format=long
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
labels: |
org.opencontainers.image.source=https://github.com/stacklok/mediator
org.opencontainers.image.title=Stacklok Mediator
org.opencontainers.image.licenses=Apache-2.0
org.opencontainers.image.vendor=Stacklok

- name: Build container images and push
id: docker_build
uses: docker/build-push-action@v4
with:
context: .
file: Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Only push to the registry if we're building the default branch
push: ${{ github.ref == 'refs/heads/main' }}
# ARM builds take a long time due to QEMU emulation. For PRs
# we only build amd64, and for the default branch we build both.
platforms: linux/amd64${{ github.event_name != 'pull_request' && ', linux/arm64' || '' }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ Thumbs.db
# asdf
.tool-versions

# Mediator-specific files
.ssh/
cmd/server/__debug_bin
.resolved-compose.yaml

# docusaurus
docs/node_modules
docs/build
docs/.docusaurus
docs/.docusaurus
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ run-cli: ## run the CLI, needs additional arguments
run-server: ## run the app
@go run -ldflags "-X main.version=$(shell git describe --abbrev=0 --tags)" ./cmd/server serve

# Unfortunately, we need OS detection for docker-compose
OS := $(shell uname -s)
DOCKERARCH := $(shell uname -m | sed 's/x86_64/amd64/' | sed 's/aarch64/arm64/')

run-docker: ## run the app under docker. If you use podman, maybe try a symlink?
# podman (at least) doesn't seem to like multi-arch images, and sometimes picks the wrong one (e.g. amd64 on arm64)
# ko resolve will fill in the image: field in the compose file, but it adds a yaml document separator
ko resolve --platform linux/$(DOCKERARCH) -f docker-compose.yaml | sed 's/^--*$$//' > .resolved-compose.yaml
# MacOS can't tolerate the ":z" flag, Linux needs it. https://github.com/containers/podman-compose/issues/509
ifeq ($(OS),Darwin)
sed -i '' 's/:z$$//' .resolved-compose.yaml
endif
docker-compose -f .resolved-compose.yaml down && docker-compose -f .resolved-compose.yaml up $(services)
rm .resolved-compose.yaml*

bootstrap: ## install build deps
go generate -tags tools tools/tools.go
# N.B. each line runs in a different subshell, so we don't need to undo the 'cd' here
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ make run-server

The application will be available on `http://localhost:8080` and gRPC on `localhost:8090`.

## Running the server under Compose:

**NOTE: the command will be either `docker-compose` or `podman-compose`, depending on which tool you installed.** You'll need to install the [`ko`](https://ko.build/install/) tool do the build and run.

```bash
# The repo to push to; "ko.local" is a special string meaning your local Docker repo
KO_DOCKER_REPO=ko.local
# ko adds YAML document separators at the end of each document, which docker-compose doesn't like
docker-compose -f <(ko resolve -f docker-compose.yaml | sed 's/^---$//') up
```

## Run the tests

Note that you need to have [started the database and loaded the schema](#initialize-the-database) before running the tests:
Expand Down
7 changes: 3 additions & 4 deletions cmd/server/app/migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ var downCmd = &cobra.Command{
}
}

m, err := migrate.New(
"file://database/migrations",
connString)
configPath := os.ExpandEnv("file://${KO_DATA_PATH}/database/migrations")
m, err := migrate.New(configPath, connString)
if err != nil {
fmt.Printf("Error while creating migration instance: %v\n", err)
fmt.Printf("Error while creating migration instance (%s): %v\n", configPath, err)
os.Exit(1)
}
if err := m.Down(); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cmd/server/app/migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ var upCmd = &cobra.Command{
}
}

m, err := migrate.New(
"file://database/migrations",
connString)
configPath := os.ExpandEnv("file://${KO_DATA_PATH}database/migrations")
m, err := migrate.New(configPath, connString)
if err != nil {
fmt.Printf("Error while creating migration instance: %v\n", err)
fmt.Printf("Error while creating migration instance (%s): %v\n", configPath, err)
os.Exit(1)
}
if err := m.Up(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/server/kodata/config.yaml
1 change: 1 addition & 0 deletions cmd/server/kodata/database/migrations
28 changes: 22 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,51 @@
version: '3.2'
services:
mediator:
build: .
command: [
"serve",
"--grpc-host=0.0.0.0",
"--http-host=0.0.0.0",
"--db-host=postgres",
"--config=/app/config.yaml"

]
restart: always # keep the server running
ports:
- "8080:8080"
- "8090:8090"
image: ghcr.io/stacklok/mediator:latest
image: ko://github.com/stacklok/mediator/cmd/server
volumes:
- ./config.yaml:/app/config.yaml:z
- .ssh:/app/.ssh:z
- ./.ssh:/app/.ssh:z
environment:
- KO_DATA_PATH=/app/
# Use viper environment variables to set specific paths to keys;
# these values are relative paths in config.yaml, but it's not clear
# what they are relative _to_...
- AUTH.ACCESS_TOKEN_PRIVATE_KEY=/app/.ssh/access_token_rsa
- AUTH.ACCESS_TOKEN_PUBLIC_KEY=/app/.ssh/access_token_rsa.pub
- AUTH.REFRESH_TOKEN_PRIVATE_KEY=/app/.ssh/refresh_token_rsa
- AUTH.REFRESH_TOKEN_PUBLIC_KEY=/app/.ssh/refresh_token_rsa.pub
networks:
- app_net
depends_on:
- postgres
postgres:
condition: service_healthy
# migrate:
# condition: service_completed_successfully
migrate:
build: .
command: [
"migrate",
"up",
"--yes",
"--db-host=postgres",
]
image: ghcr.io/stacklok/mediator:latest
image: ko://github.com/stacklok/mediator/cmd/server
volumes:
- ./config.yaml:/app/config.yaml:z
- ./database/migrations:/app/database/migrations:z
environment:
- KO_DATA_PATH=/app/
networks:
- app_net
deploy:
Expand Down