add docker and actions and compose (#5)

* Create docker-publish.yml

* Create Dockerfile

* Update Dockerfile

* Create docker-compose.yml
This commit is contained in:
tkizm1 2024-05-17 09:35:35 +08:00 committed by GitHub
parent 8161100c9d
commit f4b65d9144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 106 additions and 0 deletions

67
.github/workflows/docker-publish.yml vendored Normal file
View File

@ -0,0 +1,67 @@
name: Docker
on:
push:
branches: [ "master" ]
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "master" ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1
with:
cosign-release: 'v2.1.1'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=semver,pattern={{version}}
type=sha
type=raw,value=latest
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max

30
Dockerfile Normal file
View File

@ -0,0 +1,30 @@
# 使用 Go 1.22 官方镜像作为构建环境
FROM golang:1.22 AS builder
# 禁用 CGO
ENV CGO_ENABLED=0
# 设置工作目录
WORKDIR /app
# 复制 go.mod 和 go.sum 并下载依赖
COPY go.mod go.sum ./
RUN go mod download
# 复制源代码并构建应用
COPY . .
RUN go build -ldflags "-s -w" -o /app/override .
# 使用 Alpine Linux 作为最终镜像
FROM alpine:latest
# 设置工作目录
WORKDIR /app
# 从构建阶段复制编译好的应用和资源
COPY --from=builder /app/override /app/override
# 暴露端口
EXPOSE 8080
CMD ["/app/override"]

9
docker-compose.yml Normal file
View File

@ -0,0 +1,9 @@
version: '3.4'
services:
groq2api:
image: ghcr.io/linux-do/override:latest
container_name: override
restart: always
ports:
- "8181:8181"