add GitHub Actions workflow and Dockerfile for building and pushing container image
Some checks failed
Build and Push Container Image / build-and-push-and-deploy (push) Failing after 1m51s

This commit is contained in:
2025-06-16 00:43:17 +08:00
commit abfbb698f2
6 changed files with 126 additions and 0 deletions

41
.github/workflows/build.yaml vendored Normal file
View File

@ -0,0 +1,41 @@
name: Build and Push Container Image
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: write
concurrency:
group: docker-build
cancel-in-progress: false
env:
CONTAINER_TAG: liteyuki/new-domain-redir:latest
REGISTRY: reg.liteyuki.org
jobs:
build-and-push-and-deploy:
runs-on: liteyukios-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Log in to Liteyuki Harbor
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.LCR_USERNAME }}
password: ${{ secrets.LCR_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push container image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.CONTAINER_TAG }}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea/
.vscode/

33
Dockerfile Normal file
View File

@ -0,0 +1,33 @@
# build
FROM reg.liteyuki.org/dockerhub/golang:1.24.2-alpine3.21 AS builder
ENV TZ=Asia/Chongqing
WORKDIR /app
RUN apk --no-cache add build-base git tzdata
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main main.go
# production
FROM reg.liteyuki.org/dockerhub/alpine:latest AS prod
ENV TZ=Asia/Chongqing
WORKDIR /app
RUN apk --no-cache add tzdata ca-certificates libc6-compat libgcc libstdc++
COPY --from=builder /app/main /app/main
EXPOSE 8888
RUN chmod +x ./main
ENTRYPOINT ["./main"]

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.liteyuki.org/LiteyukiStudio/new-domain-redir
go 1.23.3
require github.com/cloudwego/hertz v0.10.0 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/cloudwego/hertz v0.10.0 h1:V0vmBaLdQPlgL6w2TA6PZL1g6SGgQznFx6vqxWdCcKw=
github.com/cloudwego/hertz v0.10.0/go.mod h1:lRBohmcDkGx5TLK6QKFGdzJ6n3IXqGueHsOiXcYgXA4=

43
main.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"strings"
)
const (
OldDomain = "liteyuki.icu"
NewDomain = "liteyuki.org"
)
func main() {
h := server.Default()
h.Use(func(ctx context.Context, c *app.RequestContext) {
host := string(c.Host())
if strings.HasSuffix(host, OldDomain) {
newHost := strings.TrimSuffix(host, OldDomain) + NewDomain
scheme := "https"
if proto := c.Request.Header.Get("X-Forwarded-Proto"); len(proto) > 0 {
scheme = string(proto)
} else if forwarded := c.Request.Header.Get("Forwarded"); len(forwarded) > 0 {
// Forwarded: proto=https;host=xxx
for _, part := range strings.Split(string(forwarded), ";") {
if strings.HasPrefix(part, "proto=") {
scheme = strings.TrimPrefix(part, "proto=")
break
}
}
}
newURL := scheme + "://" + newHost + string(c.Request.RequestURI())
c.Redirect(301, []byte(newURL))
c.Abort()
return
}
})
h.GET("*", func(ctx context.Context, c *app.RequestContext) {
c.JSON(200, map[string]interface{}{"message": "pong"})
})
h.Spin()
}