Skip to content

Commit 98b2278

Browse files
authored
Add new pipeline and docker stack (#3)
* Add new pipeline and docker stack * Remove reference to watchtower
1 parent 3878fca commit 98b2278

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

.github/workflows/deploy.yaml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Deploy to Server
2+
3+
on:
4+
push:
5+
branches:
6+
- deploytest
7+
8+
permissions:
9+
packages: write
10+
11+
jobs:
12+
commit-hash:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
commit_hash: ${{ steps.get_commit.outputs.commit_hash }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Get commit hash
19+
id: get_commit
20+
run: echo "::set-output name=commit_hash::$(git rev-parse HEAD)"
21+
22+
build-and-test:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Setup Go
28+
uses: actions/setup-go@v5
29+
with:
30+
go-version: '1.22.x'
31+
- name: Install dependencies
32+
run: go get .
33+
- name: Build
34+
run: go build -v ./...
35+
- name: Test with the Go CLI
36+
run: go test
37+
38+
build-and-push-image:
39+
needs:
40+
- build-and-test
41+
- commit-hash
42+
runs-on: ubuntu-latest
43+
steps:
44+
- name: Checkout repository
45+
uses: actions/checkout@v3
46+
47+
- name: Set short git commit SHA
48+
id: vars
49+
run: |
50+
calculatedSha=$(git rev-parse --short ${{ github.sha }})
51+
echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV
52+
53+
- name: Log in to the Container registry
54+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
55+
with:
56+
registry: https://ghcr.io
57+
username: ${{ github.actor }}
58+
password: ${{ secrets.GITHUB_TOKEN }}
59+
60+
- name: Build and push Docker image
61+
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
62+
with:
63+
context: .
64+
push: true
65+
tags: ghcr.io/dreamsofcode-io/guestbook:${{ needs.commit-hash.outputs.commit_hash }}
66+
67+
deploy:
68+
runs-on: ubuntu-latest
69+
needs:
70+
- build-and-push-image
71+
- commit-hash
72+
73+
steps:
74+
- name: Checkout code
75+
uses: actions/checkout@v2
76+
- name: create env file
77+
run: |
78+
echo "GIT_COMMIT_HASH=${{ github.sha }}" >> env
79+
- name: 'Docker Stack Deploy'
80+
uses: cssnr/stack-deploy-action@v1
81+
with:
82+
name: 'guestbook'
83+
file: 'docker-stack.yaml'
84+
host: zenful.cloud
85+
user: deploytest
86+
ssh_key: ${{ secrets.DEPLOY_SSH_KEY }}
87+
env_file: './env'

docker-stack.yaml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
services:
2+
reverse-proxy:
3+
image: traefik:v3.1
4+
command:
5+
- "--providers.docker"
6+
- "--providers.docker.exposedbydefault=false"
7+
- "--entryPoints.websecure.address=:443"
8+
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
9+
- "--certificatesresolvers.myresolver.acme.email=elliott@zenful.cloud"
10+
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
11+
- "--entrypoints.web.address=:80"
12+
- "--entrypoints.web.http.redirections.entrypoint.to=websecure"
13+
- "--entrypoints.web.http.redirections.entrypoint.scheme=https"
14+
ports:
15+
- mode: host
16+
protocol: tcp
17+
published: 80
18+
target: 80
19+
- mode: host
20+
protocol: tcp
21+
published: 443
22+
target: 443
23+
volumes:
24+
- letsencrypt:/letsencrypt
25+
- /var/run/docker.sock:/var/run/docker.sock
26+
guestbook:
27+
image: ghcr.io/dreamsofcode-io/guestbook:${GIT_COMMIT_HASH:-prod}
28+
labels:
29+
- "traefik.enable=true"
30+
- "traefik.http.middlewares.guestbook-ratelimit.ratelimit.average=20"
31+
- "traefik.http.routers.guestbook.rule=Host(`zenful.cloud`) && !Method(`POST`)"
32+
- "traefik.http.services.guestbook.loadbalancer.server.port=8080"
33+
- "traefik.http.routers.guestbook.entrypoints=websecure"
34+
- "traefik.http.routers.guestbook.tls.certresolver=myresolver"
35+
- "traefik.http.routers.guestbook.middlewares=guestbook-ratelimit"
36+
# Define separate router for POST methods
37+
- "traefik.http.middlewares.guestbook-ratelimit-post.ratelimit.average=1"
38+
- "traefik.http.middlewares.guestbook-ratelimit-post.ratelimit.period=1m"
39+
- "traefik.http.routers.guestbook-post.rule=Host(`zenful.cloud`) && Method(`POST`)"
40+
- "traefik.http.routers.guestbook-post.middlewares=guestbook-ratelimit-post"
41+
- "traefik.http.routers.guestbook-post.entrypoints=websecure"
42+
- "traefik.http.routers.guestbook-post.tls.certresolver=myresolver"
43+
# Proxy
44+
- "traefik.http.routers.proxy.rule=Host(`proxy.dreamsofcode.io`)"
45+
- "traefik.http.routers.proxy.entrypoints=websecure"
46+
- "traefik.http.routers.proxy.tls.certresolver=myresolver"
47+
secrets:
48+
- db-password
49+
environment:
50+
- POSTGRES_HOST=db
51+
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
52+
- POSTGRES_USER=postgres
53+
- POSTGRES_DB=guestbook
54+
- POSTGRES_PORT=5432
55+
- POSTGRES_SSLMODE=disable
56+
deploy:
57+
mode: replicated
58+
replicas: 3
59+
restart: always
60+
depends_on:
61+
- db
62+
db:
63+
image: postgres:16
64+
restart: always
65+
user: postgres
66+
volumes:
67+
- db-data:/var/lib/postgresql/data
68+
secrets:
69+
- db-password
70+
environment:
71+
- POSTGRES_DB=guestbook
72+
- POSTGRES_PASSWORD_FILE=/run/secrets/db-password
73+
expose:
74+
- 5432
75+
healthcheck:
76+
test: [ "CMD", "pg_isready" ]
77+
interval: 10s
78+
timeout: 5s
79+
retries: 5
80+
volumes:
81+
db-data:
82+
letsencrypt:
83+
secrets:
84+
db-password:
85+
external: true

0 commit comments

Comments
 (0)