Skip to content

Commit

Permalink
Add docker/docker-compose support and move GH actions to use docker-c…
Browse files Browse the repository at this point in the history
…ompose by default
  • Loading branch information
fussel178 committed Jan 4, 2024
1 parent 26ebaf8 commit 2054ee8
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 42 deletions.
56 changes: 14 additions & 42 deletions .github/workflows/backend-go-ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
name: Backend Go CI

# Events that trigger this workflow
on: [push, pull_request]

# Defines which tool versions should be used in all workflow jobs
env:
go: "1.21"
on: [ push, pull_request ]

defaults:
run:
Expand All @@ -18,44 +14,20 @@ jobs:
steps:
- name: Checkout 📥
uses: actions/[email protected]
- name: Setup Go 💿
uses: actions/[email protected]
with:
go-version: ${{ env.go }}
cache-dependency-path: backend-go/go.sum
- name: Install dependencies 📚
run: go get .
- name: Check code style 🧽
shell: bash
run: |
test "$(gofmt -e -l . | wc -l)" -eq 0
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout 📥
uses: actions/[email protected]
- name: Setup Go 💿
uses: actions/[email protected]
with:
go-version: ${{ env.go }}
cache-dependency-path: backend-go/go.sum
- name: Install dependencies 📚
run: go get .
- name: Build library 📦
run: go build -v ./...
unit-test:
name: Unit Test
- name: Check style 🧽
run: docker compose --file docker-compose.ci.yml --profile style up --abort-on-container-exit
- name: Stop containers 🛑
if: always()
run: docker compose --file docker-compose.ci.yml --profile style down

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout 📥
uses: actions/[email protected]
- name: Setup Go 💿
uses: actions/[email protected]
with:
go-version: ${{ env.go }}
cache-dependency-path: backend-go/go.sum
- name: Install dependencies 📚
run: go get .
- name: Run unit tests 🛃
run: go test
- name: Run tests 🛃
run: docker compose --file docker-compose.ci.yml --profile test up --abort-on-container-exit
- name: Stop containers 🛑
if: always()
run: docker compose --file docker-compose.ci.yml --profile test down
6 changes: 6 additions & 0 deletions backend-go/.nats.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
http_port: 8222

websocket: {
port: 9222
no_tls: true
}
9 changes: 9 additions & 0 deletions backend-go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:1.21-alpine

# creates an "invisible" docker volume during container startup
# by retaining the go builc cache from the image build
# to support different cpu architectures
VOLUME /go

# switch to app
WORKDIR /app
24 changes: 24 additions & 0 deletions backend-go/docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
##
## docker-compose configuration for GitHub Actions
##

services:
style:
build:
context: .
dockerfile: Dockerfile
command: >
sh -c 'gofmt -e -l . && test "$(gofmt -e -l . | wc -l)" -eq 0'
volumes:
- .:/app
profiles: [ 'style' ]

test:
build:
context: .
dockerfile: Dockerfile
command: [ 'go', 'test' ]
volumes:
- .:/app
- ../backend-features:/backend-features
profiles: [ 'test' ]
69 changes: 69 additions & 0 deletions backend-go/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
services:

##
## go commands
##

style:
build:
context: .
dockerfile: Dockerfile
command: [ "gofmt", "-w", "." ]
volumes:
- .:/app
profiles: [ 'style' ]

test:
build:
context: .
dockerfile: Dockerfile
command: [ 'go', 'test' ]
volumes:
- .:/app
- ../backend-features:/backend-features
profiles: [ 'test' ]

##
## end-to-end example
##

sub:
build:
context: .
dockerfile: Dockerfile
command: [ 'go', 'run', 'samples/sub/main.go', '--dev', '--NATS_URL=nats://nats:4222' ]
volumes:
- .:/app
networks: [ 'nats-network' ]
profiles: [ 'dev' ]
depends_on: [ 'nats' ]
restart: unless-stopped

pub:
build:
context: .
dockerfile: Dockerfile
command: [ 'go', 'run', 'samples/pub/main.go', '--dev', '--NATS_URL=nats://nats:4222' ]
volumes:
- .:/app
networks: [ 'nats-network' ]
profiles: [ 'dev' ]
depends_on: [ 'nats' ]

nats:
image: nats:latest
command: [ '-c', '/etc/nats/nats.conf' ]
volumes:
- ./.nats.conf:/etc/nats/nats.conf
ports:
- '0.0.0.0:4222:4222'
- '127.0.0.1:8222:8222'
- '127.0.0.1:6222:6222'
- '0.0.0.0:9222:9222'
networks: [ 'nats-network' ]
profiles: [ 'dev' ]
restart: unless-stopped

networks:
nats-network:
name: nats-network

0 comments on commit 2054ee8

Please sign in to comment.