Skip to content

Commit daedf91

Browse files
committed
feat: add demo apps and workflows to build
1 parent e79b919 commit daedf91

File tree

13 files changed

+417
-0
lines changed

13 files changed

+417
-0
lines changed

.github/workflows/build-apps.yml

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
name: Build demo apps
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'apps/**'
9+
10+
jobs:
11+
hello-world:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
packages: write
16+
17+
steps:
18+
- name: checkout
19+
uses: actions/checkout@v4
20+
21+
- name: setup qemu
22+
uses: docker/setup-qemu-action@v3
23+
24+
- name: setup docker
25+
uses: docker/setup-buildx-action@v3
26+
27+
- name: login to ghcr.io
28+
uses: docker/login-action@v3
29+
with:
30+
registry: ghcr.io
31+
username: ${{ github.actor }}
32+
password: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: docker metadata
35+
uses: docker/metadata-action@v5
36+
id: hello_world_meta
37+
with:
38+
images: |
39+
ghcr.io/${{ github.repository }}/apps/hello-world
40+
tags: |
41+
type=sha,format=long
42+
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
43+
44+
- name: build and push
45+
uses: docker/build-push-action@v5
46+
with:
47+
context: /apps/hello-world
48+
platforms: linux/amd64,linux/arm64
49+
push: true
50+
tags: ${{ steps.hello_world_meta.outputs.tags }}
51+
labels: ${{ steps.hello_world_meta.outputs.labels }}
52+
53+
probes:
54+
runs-on: ubuntu-latest
55+
permissions:
56+
contents: read
57+
packages: write
58+
59+
steps:
60+
- name: checkout
61+
uses: actions/checkout@v4
62+
63+
- name: setup qemu
64+
uses: docker/setup-qemu-action@v3
65+
66+
- name: setup docker
67+
uses: docker/setup-buildx-action@v3
68+
69+
- name: login to ghcr.io
70+
uses: docker/login-action@v3
71+
with:
72+
registry: ghcr.io
73+
username: ${{ github.actor }}
74+
password: ${{ secrets.GITHUB_TOKEN }}
75+
76+
- name: docker metadata
77+
uses: docker/metadata-action@v5
78+
id: probe_meta
79+
with:
80+
images: |
81+
ghcr.io/${{ github.repository }}/apps/probes
82+
tags: |
83+
type=sha,format=long
84+
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
85+
86+
- name: build and push
87+
uses: docker/build-push-action@v5
88+
with:
89+
context: /apps/probes
90+
platforms: linux/amd64,linux/arm64
91+
push: true
92+
tags: ${{ steps.probe_meta.outputs.tags }}
93+
labels: ${{ steps.probe_meta.outputs.labels }}
94+
95+
blue-green:
96+
runs-on: ubuntu-latest
97+
permissions:
98+
contents: read
99+
packages: write
100+
101+
steps:
102+
- name: checkout
103+
uses: actions/checkout@v4
104+
105+
- name: setup qemu
106+
uses: docker/setup-qemu-action@v3
107+
108+
- name: setup docker
109+
uses: docker/setup-buildx-action@v3
110+
111+
- name: login to ghcr.io
112+
uses: docker/login-action@v3
113+
with:
114+
registry: ghcr.io
115+
username: ${{ github.actor }}
116+
password: ${{ secrets.GITHUB_TOKEN }}
117+
118+
- name: docker metadata (blue)
119+
uses: docker/metadata-action@v5
120+
id: blue_meta
121+
with:
122+
123+
images: |
124+
ghcr.io/${{ github.repository }}/apps/blue-green
125+
tags: |
126+
type=sha,format=long
127+
type=raw,value=blue
128+
129+
- name: docker metadata (green)
130+
uses: docker/metadata-action@v5
131+
id: green_meta
132+
with:
133+
images: |
134+
ghcr.io/${{ github.repository }}/apps/blue-green
135+
tags: |
136+
type=sha,format=long
137+
type=raw,value=green
138+
139+
- name: build and push (blue)
140+
uses: docker/build-push-action@v5
141+
with:
142+
context: /apps/blue-green
143+
platforms: linux/amd64,linux/arm64
144+
build-args: |
145+
IS_BLUE=true
146+
push: true
147+
tags: ${{ steps.green_meta.outputs.tags }}
148+
labels: ${{ steps.green_meta.outputs.labels }}
149+
150+
- name: build and push (green)
151+
uses: docker/build-push-action@v5
152+
with:
153+
context: /apps/blue-green
154+
platforms: linux/amd64,linux/arm64
155+
build-args: |
156+
IS_BLUE=false
157+
push: true
158+
tags: ${{ steps.green_meta.outputs.tags }}
159+
labels: ${{ steps.green_meta.outputs.labels }}
160+

apps/blue-green/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM golang:1.22 as builder
2+
3+
ARG IS_BLUE=true
4+
5+
WORKDIR /go/src/app
6+
7+
COPY . .
8+
9+
RUN go mod download
10+
11+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.IsBlue=${IS_BLUE}" -a -installsuffix cgo -o blue-green .
12+
13+
FROM debian:bookwork-slim as runtime
14+
15+
WORKDIR /app
16+
17+
COPY --from=builder /go/src/app/blue-green .
18+
19+
CMD [ "/app/blue-green" ]

apps/blue-green/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/vojtechmares/kubernetes-training/apps/blue-green
2+
3+
go 1.22.0
4+
5+
require github.com/google/uuid v1.6.0 // indirect

apps/blue-green/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

apps/blue-green/main.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
"strconv"
9+
"text/template"
10+
11+
"github.com/google/uuid"
12+
)
13+
14+
var IsBlue string = "true"
15+
16+
const html = `
17+
<!DOCTYPE html>
18+
<html>
19+
<head>
20+
<title>Hello, Kubernetes! | {{ .Hostname }}</title>
21+
<style>
22+
body {
23+
font-family: Helvetica;
24+
background-color: {{ .Color }};
25+
color: #fff;
26+
}
27+
pre {
28+
margin: 0.25rem 0;
29+
font-size: 1.5rem;
30+
}
31+
</style>
32+
</head>
33+
<body>
34+
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center;">
35+
<h1 style="font-size: 4rem;">Hello, Kubernetes! 👋</h1>
36+
<pre>hostname: {{ .Hostname }}</pre>
37+
<pre>instanceID: {{ .InstanceID }}</pre>
38+
</div>
39+
</body>
40+
</html>
41+
`
42+
43+
func main() {
44+
instanceID := uuid.New().String()
45+
hostname, err := os.Hostname()
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
var hexColor string = "#15803d" // green
51+
52+
isBlue, err := strconv.ParseBool(IsBlue)
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
57+
if isBlue {
58+
hexColor = "#1d4ed8"
59+
}
60+
61+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
62+
tpl, err := template.New("index.html").Parse(html)
63+
if err != nil {
64+
log.Println(err)
65+
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
66+
return
67+
}
68+
69+
err = tpl.Execute(w, map[string]string{"Hostname": hostname, "InstanceID": instanceID, "Color": hexColor})
70+
if err != nil {
71+
log.Println(err)
72+
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
73+
return
74+
}
75+
76+
w.Header().Set("Content-Type", "text/html")
77+
})
78+
79+
log.Println("Starting server on :8080")
80+
http.ListenAndServe(":8080", nil)
81+
}

apps/hello-world/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM golang:1.22 as builder
2+
3+
WORKDIR /go/src/app
4+
5+
COPY . .
6+
7+
RUN go mod download
8+
9+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o hello-world .
10+
11+
FROM debian:bookwork-slim as runtime
12+
13+
WORKDIR /app
14+
15+
COPY --from=builder /go/src/app/hello-world .
16+
17+
CMD [ "/app/hello-world" ]

apps/hello-world/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/vojtechmares/kubernetes-training/apps/probes
2+
3+
go 1.22.0
4+
5+
require github.com/google/uuid v1.6.0

apps/hello-world/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

apps/hello-world/main.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
"text/template"
9+
10+
"github.com/google/uuid"
11+
)
12+
13+
const html = `
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<title>Hello, Kubernetes! | {{ .Hostname }}</title>
18+
<style>
19+
body {
20+
font-family: Helvetica;
21+
}
22+
pre {
23+
margin: 0.25rem 0;
24+
font-size: 1.5rem;
25+
}
26+
</style>
27+
</head>
28+
<body style="font-family: Helvetica">
29+
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center;">
30+
<h1 style="font-size: 4rem;">Hello, Kubernetes! 👋</h1>
31+
<pre>hostname: {{ .Hostname }}</pre>
32+
<pre>instanceID: {{ .InstanceID }}</pre>
33+
</div>
34+
</body>
35+
</html>
36+
`
37+
38+
func main() {
39+
instanceID := uuid.New().String()
40+
hostname, err := os.Hostname()
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
45+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
46+
tpl, err := template.New("index.html").Parse(html)
47+
if err != nil {
48+
log.Println(err)
49+
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
50+
return
51+
}
52+
53+
err = tpl.Execute(w, map[string]string{"Hostname": hostname, "InstanceID": instanceID})
54+
if err != nil {
55+
log.Println(err)
56+
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
57+
return
58+
}
59+
60+
w.Header().Set("Content-Type", "text/html")
61+
})
62+
63+
log.Println("Starting server on :8080")
64+
http.ListenAndServe(":8080", nil)
65+
}

apps/probes/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM golang:1.22 as builder
2+
3+
WORKDIR /go/src/app
4+
5+
COPY . .
6+
7+
RUN go mod download
8+
9+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o probes .
10+
11+
FROM debian:bookwork-slim as runtime
12+
13+
WORKDIR /app
14+
15+
COPY --from=builder /go/src/app/probes .
16+
17+
CMD [ "/app/probes" ]

0 commit comments

Comments
 (0)