Skip to content

Commit

Permalink
Merge pull request #43 from Spacelocust/feature/deploy-api
Browse files Browse the repository at this point in the history
Feat / config deploy api
  • Loading branch information
Spacelocust authored Jul 16, 2024
2 parents 28520a2 + f07d3c8 commit 5018d69
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 8 deletions.
Empty file removed .dockerignore
Empty file.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
DB_USER=root
DB_PASSWORD=root
DB_NAME=app
DB_HOST=postgres
DB_URL="postgresql://root:root@postgres/app?sslmode=disable"
# Database url for migration creation purpose (e.g. https://atlasgo.io/concepts/dev-database)
DB_URL_DEV="postgresql://root:root@postgres/dev?sslmode=disable&search_path=public"
Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/docker-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Publish API

on:
workflow_run:
workflows: [Linting API, Testing API]
types: [completed]
branches: ["main"]

push:
branches:
- main

workflow_dispatch:

permissions:
contents: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-push-api:
name: Push Docker image API

runs-on: ubuntu-latest

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

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

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: docker/prod/golang/Dockerfile
builder: ${{ steps.buildx.outputs.name }}
push: ${{ github.event_name != 'pull_request' }}
tags: spacelocust/fd-api:latest
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54 changes: 54 additions & 0 deletions .github/workflows/docker-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Publish Migrations

on:
push:
branches:
- main
paths:
- "api/migrations/**"

workflow_dispatch:

permissions:
contents: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-push-migrations:
name: Push Docker image Migrations

runs-on: ubuntu-latest

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

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

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: docker/prod/atlas/Dockerfile
builder: ${{ steps.buildx.outputs.name }}
push: ${{ github.event_name != 'pull_request' }}
tags: spacelocust/fd-migrations:latest
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ dcm: db-create-migration ## Alias for db-create-migration
db-migrate-apply: ## Apply the migrations
$(EXECAPI) atlas migrate apply --dir "file://migrations" --url ${DB_URL} --allow-dirty

db-m-a:
docker run --rm -v $(PWD)/api/migrations:/migrations --network for-democracy_default arigaio/atlas migrate apply --dir "file://migrations" --url ${DB_URL} --allow-dirty

dma: db-migrate-apply ## Alias for db-migrate-apply

db-create-empty-migration: ## Create an empty migration
Expand Down
1 change: 1 addition & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func main() {

// Set up the Swagger documentation
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%s", os.Getenv("API_DOMAIN"), os.Getenv("API_PORT"))
docs.SwaggerInfo.Schemes = []string{"http", "https"}
Expand Down
10 changes: 2 additions & 8 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ services:
- path: ./.env
- path: ./.env.local
required: false
environment:
DB_HOST: "postgres"
ports:
- 5000:5000
volumes:
Expand All @@ -34,15 +32,11 @@ services:
environment:
POSTGRES_USER: "${DB_USER:?err}"
POSTGRES_PASSWORD: "${DB_PASSWORD:?err}"
POSTGRES_DB: '${DB_NAME:?err}'
POSTGRES_DB: "${DB_NAME:?err}"
volumes:
- ./data/postgresql:/var/lib/postgresql/data
healthcheck:
test:
[
'CMD-SHELL',
'pg_isready -U ${DB_USER} -d ${DB_PASSWORD}'
]
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_PASSWORD}"]
interval: 10s
timeout: 20s
retries: 5
Expand Down
3 changes: 3 additions & 0 deletions docker/dev/atlas/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM arigaio/atlas:latest

COPY migrations /migrations
Empty file removed docker/prod/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions docker/prod/atlas/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM alpine:3.20

COPY --from=arigaio/atlas:latest atlas /usr/bin/atlas

COPY ./api/migrations /migrations

# Use SHELL to explicitly execute the command with shell (e.g. https://docs.docker.com/reference/build-checks/json-args-recommended/)
SHELL ["/bin/sh", "-c"]

ENTRYPOINT atlas migrate apply --dir file://migrations --url $DATABASE_URL
8 changes: 8 additions & 0 deletions docker/prod/caddy/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
email {$ACME_EMAIL}
admin off
}

api.yam-yam.dev {
reverse_proxy * api:5000
}
28 changes: 28 additions & 0 deletions docker/prod/golang/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM golang:1.22-alpine AS builder

WORKDIR /go/src/app

COPY /api/go.* ./

# Install dependencies
RUN go mod download &&\
go mod verify

COPY /api .

# Remove the migrations folder and build the binary
RUN rm -rf /migrations && GOOS=linux GOARCH=amd64 go build -o fd-democracy main.go

FROM debian:buster-slim AS runner

RUN apt-get update && apt-get install -y \
curl \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*

# Copy the binary from the builder stage
COPY --from=builder /go/src/app/fd-democracy /go/bin/fd-democracy

ENV PATH="/go/bin:${PATH}"

ENTRYPOINT ["fd-democracy", "gin-server"]

0 comments on commit 5018d69

Please sign in to comment.