Skip to content

Commit

Permalink
Create Docker-image & CI
Browse files Browse the repository at this point in the history
  • Loading branch information
qjoly committed Aug 25, 2024
1 parent 961935e commit c7c9b6b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 7 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build Docker Images for GHCR

on:
push:
branches:
- main

jobs:
build-image:
name: Build Docker Image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
matrix:
include:
- Dockerfile: coffee-shop/coffee-maker/Dockerfile
context: coffee-shop/coffee-maker
tag: coffee-maker

steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_PAT }} # Use the new secret here

- name: Checkout code
uses: actions/checkout@v4

- name: set repository name to lowercase
run: |
echo "reponame=${reponame,,}" >>${GITHUB_ENV}
env:
reponame: '${{ github.repository }}'

- name: Build and Push Docker Image
uses: docker/build-push-action@v6
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
push: ${{ github.event_name != 'pull_request' }}
tags: ghcr.io/une-tasse-de.cafe/${{ env.reponame }}:${{ matrix.tag }}
platforms: linux/amd64,linux/arm64
12 changes: 12 additions & 0 deletions coffee-maker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.23.0-alpine3.19 as builder
WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download
COPY . .

RUN go build

FROM scratch
COPY --from=builder /app/coffee-maker /app
ENTRYPOINT ["/app"]
11 changes: 11 additions & 0 deletions coffee-maker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

version: '3.8'
services:
coffee-makers:
image: ghcr.io/une-tasse-de-cafe/coffee-shop/coffeemakers:latest
build: .
environment:
- "NATS_URL=192.168.128.51:4222"
deploy:
mode: replicated
replicas: 5
1 change: 1 addition & 0 deletions coffee-maker/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ require (
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
2 changes: 2 additions & 0 deletions coffee-maker/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
66 changes: 59 additions & 7 deletions coffee-maker/main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,82 @@
package main

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)

const (
consumerName = "coffeeMakers"
subjects = "coffee.orders.*"
streamName = "coffee-orders"
)

func main() {

nc, err := nats.Connect(os.Getenv("NATS_URL"))
natsUrl := os.Getenv("NATS_URL")
if natsUrl == "" {
fmt.Println("Please, provide the NATS URL in NATS_URL")
os.Exit(1)
}

nc, _ := nats.Connect(os.Getenv("NATS_URL"))

defer nc.Close()

js, err := jetstream.New(nc)

if err != nil {
log.Fatal(err)
}
defer nc.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

fmt.Println("waiting for new tasks...")
if _, err = nc.Subscribe("coffee.*", func(m *nats.Msg) {
fmt.Println("New order received")
fmt.Println(string(m.Data))
cfgStream := jetstream.StreamConfig{
Replicas: 3,
Name: streamName,
Subjects: []string{subjects},
Storage: jetstream.FileStorage,
Retention: jetstream.InterestPolicy,
AllowDirect: true,
}

}); err != nil {
_, err = js.CreateOrUpdateStream(ctx, cfgStream)
if err != nil {
log.Fatal(err)
}

cfgConsu := jetstream.ConsumerConfig{
Name: consumerName,
FilterSubject: subjects,
Durable: consumerName,
}

cons, err := js.CreateConsumer(ctx, cfgStream.Name, cfgConsu)
if err != nil {
log.Fatal(err)
}

cc, err := cons.Consume(func(msg jetstream.Msg) {
fmt.Printf("New message from %s : %s - ", msg.Subject(), string(msg.Data()))
msg.InProgress()
time.Sleep(500 * time.Millisecond)
msg.Ack()
fmt.Printf("\n")
})

if err != nil {
log.Fatal(err)
}
defer cc.Drain()

fmt.Println("wait forever")
for {
}

}

0 comments on commit c7c9b6b

Please sign in to comment.