add Metric
All checks were successful
Build Docker Image / build (arm64) (push) Successful in 54s
Build Docker Image / build (amd64) (push) Successful in 2m13s
Build Docker Image / amend-manifest (push) Successful in 5s

This commit is contained in:
thuanle
2024-11-07 09:08:19 +07:00
parent b0ac68cbdb
commit 108458682e
5 changed files with 91 additions and 1 deletions

View File

@@ -0,0 +1,71 @@
name: Build Docker Image
on:
push:
branches:
- main
env:
IMAGE_PATH: git.thuanle.me/public/ip-info
jobs:
build:
strategy:
matrix:
arch:
- amd64
- arm64
runs-on: ${{ matrix.arch }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history, keeping all commits
- name: Get commit count
id: get_commit_count
run: |
COMMIT_COUNT=$(git rev-list --count HEAD)
echo "COMMIT_COUNT=${COMMIT_COUNT}" >> $GITHUB_ENV
- name: Login to Docker Registry
uses: docker/login-action@v3
with:
registry: git.thuanle.me
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker images
run: |
echo "Building image for architecture: ${{ matrix.arch }}"
docker build \
-f Dockerfile \
-t ${{ env.IMAGE_PATH }}:${{ matrix.arch }}-latest \
--label "version=${{ env.COMMIT_COUNT }}" \
--label "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
--label "commit_sha=${{ github.sha }}" \
.
docker push ${{ env.IMAGE_PATH }}:${{ matrix.arch }}-latest
amend-manifest:
runs-on: ubuntu-latest
needs: [build]
steps:
- name: Login to Docker Registry
uses: docker/login-action@v3
with:
registry: git.thuanle.me
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push Docker manifest
run: |
echo "Delete existing manifest"
docker manifest rm ${{ env.IMAGE_PATH }}:latest || true
echo "Create new manifest"
docker manifest create ${{ env.IMAGE_PATH }}:latest \
${{ env.IMAGE_PATH }}:amd64-latest \
${{ env.IMAGE_PATH }}:arm64-latest
docker manifest push ${{ env.IMAGE_PATH }}:latest

View File

@@ -1,5 +1,5 @@
# Cache dependency
FROM golang:1.22-alpine AS go-mod-cache
FROM golang:1.22 AS go-mod-cache
WORKDIR /app

View File

@@ -0,0 +1,13 @@
package api
import (
"github.com/gin-gonic/gin"
"net/http"
"thuanle.me/ip-info/internal/services/db_updater"
)
func HandleMetrics(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"updated_at": db_updater.DbUpdatedAt.UnixMilli(),
})
}

View File

@@ -24,6 +24,7 @@ func StartApiService() {
engine.GET("/", HandleIp)
engine.GET("/json", HandleJson)
engine.GET("/metrics", HandleMetrics)
port := os.Getenv(key.EnvApiPort)
if len(port) == 0 {

View File

@@ -5,8 +5,11 @@ import (
"github.com/rs/zerolog/log"
"thuanle.me/ip-info/configs"
"thuanle.me/ip-info/internal/data"
"time"
)
var DbUpdatedAt time.Time
func StartUpdateDbService() {
c := cron.New()
_, _ = c.AddFunc("@daily", fetchDbs)
@@ -29,5 +32,7 @@ func fetchDbs() {
log.Err(err).Msg("Failed to reload mmdb")
return
}
DbUpdatedAt = time.Now()
}
}