Skip to content

Commit b9968ec

Browse files
author
w7years
committed
🚀 feat(Makefile): update Docker image build and push to use GitHub Container Registry
🔧 fix(main.go): log successful server shutdown message 🔄 chore(docker-image.yml): enhance CI workflow with caching and build optimizations
1 parent c1202e5 commit b9968ec

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

.github/workflows/docker-image.yml

+51-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,58 @@ on:
77
branches: [ "main" ]
88

99
jobs:
10-
1110
build:
12-
1311
runs-on: ubuntu-latest
1412

1513
steps:
16-
- uses: actions/checkout@v4
17-
- name: Build the Docker image
18-
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up QEMU
18+
uses: docker/setup-qemu-action@v2
19+
20+
- name: Set up Docker Buildx
21+
uses: docker/setup-buildx-action@v2
22+
23+
- name: Cache Go modules
24+
uses: actions/cache@v3
25+
with:
26+
path: |
27+
~/go/pkg/mod
28+
~/.cache/go-build
29+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
30+
restore-keys: |
31+
${{ runner.os }}-go-
32+
33+
- name: Cache Docker build
34+
uses: actions/cache@v3
35+
with:
36+
path: /tmp/.buildx-cache
37+
key: ${{ runner.os }}-buildx-${{ github.sha }}
38+
restore-keys: |
39+
${{ runner.os }}-buildx-
40+
41+
- name: Log in to the Container registry
42+
uses: docker/login-action@v2
43+
with:
44+
registry: ghcr.io
45+
username: ${{ github.actor }}
46+
password: ${{ secrets.GITHUB_TOKEN }}
47+
48+
- name: Install dependencies
49+
run: make tidy
50+
51+
- name: Format code
52+
run: make fmt
53+
54+
- name: Run static code analysis
55+
run: make lint
56+
57+
- name: Run tests
58+
run: make test
59+
60+
- name: Build and push Docker image
61+
run: make docker-push
62+
env:
63+
VERSION: ${{ github.sha }}
64+
GITHUB_USERNAME: ${{ github.actor }}

Makefile

+8-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Project related variables
55
PROJECT_NAME := ai-api-proxy
66
VERSION ?= v0.0.1
7-
DOCKER_USERNAME ?= wangwei01
7+
GITHUB_USERNAME ?= wangweix
88

99
BASE_PATH := $(shell pwd)
1010
BUILD_PATH := $(BASE_PATH)/build
@@ -73,16 +73,15 @@ run: build
7373
# Build Docker image
7474
docker:
7575
@echo "Build Docker image..."
76-
@docker build -t $(PROJECT_NAME):$(VERSION) .
77-
@echo "Add tag to docker image..."
78-
@docker tag $(PROJECT_NAME):$(VERSION) $(DOCKER_USERNAME)/$(PROJECT_NAME):$(VERSION)
79-
@docker tag $(PROJECT_NAME):$(VERSION) $(DOCKER_USERNAME)/$(PROJECT_NAME):latest
80-
76+
@docker build . --file Dockerfile \
77+
--tag ghcr.io/$(GITHUB_USERNAME)/$(PROJECT_NAME):latest \
78+
--tag ghcr.io/$(GITHUB_USERNAME)/$(PROJECT_NAME):$(VERSION)
79+
8180
# Push Docker image to registry
8281
docker-push: docker
83-
@echo "Push Docker image to registry..."
84-
@docker push $(DOCKER_USERNAME)/$(PROJECT_NAME):$(VERSION)
85-
@docker push $(DOCKER_USERNAME)/$(PROJECT_NAME):latest
82+
@echo "Push Docker image to GitHub Container Registry..."
83+
@docker push ghcr.io/$(GITHUB_USERNAME)/$(PROJECT_NAME):latest
84+
@docker push ghcr.io/$(GITHUB_USERNAME)/$(PROJECT_NAME):$(VERSION)
8685

8786
# Display help information
8887
help:
@@ -97,8 +96,4 @@ help:
9796
@echo " run - Run program"
9897
@echo " docker - Build Docker image"
9998
@echo " docker-push - Push Docker image to registry"
100-
@echo " docker-run - Run Docker container with external configuration file"
10199
@echo " help - Display help information"
102-
103-
104-

cmd/proxy/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ func main() {
114114
logger.Logger.Info("Received shutdown signal, shutting down server...")
115115
case err := <-errChan:
116116
logger.Logger.Errorf("Server error: %v", err)
117-
quit <- syscall.SIGINT
118117
}
119118

120119
// Graceful shutdown
@@ -123,5 +122,5 @@ func main() {
123122
if err := srv.Shutdown(ctx); err != nil {
124123
logger.Logger.Fatalf("Server shutdown failed: %v", err)
125124
}
126-
logger.Logger.Info("Server shutdown")
125+
logger.Logger.Info("Server shutdown successfully")
127126
}

0 commit comments

Comments
 (0)