add dockerfile
This commit is contained in:
@@ -1,24 +1,43 @@
|
|||||||
name: Go build
|
name: Go build
|
||||||
on: [push]
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build and test
|
name: Build and test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
container:
|
|
||||||
image: golang:latest
|
|
||||||
env:
|
|
||||||
GO111MODULE: on
|
|
||||||
GOPROXY: https://goproxy.cn,direct
|
|
||||||
ports:
|
|
||||||
- 8080
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
- name: Checkout
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Build
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v4
|
||||||
run: go build -v
|
with:
|
||||||
|
go-version: 1.21
|
||||||
|
|
||||||
|
- name: Build project
|
||||||
|
run: |
|
||||||
|
go build -v ./...
|
||||||
- name: Test with the Go CLI
|
- name: Test with the Go CLI
|
||||||
run: go test
|
run: go test
|
||||||
|
|
||||||
|
docker:
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Build Docker image
|
||||||
|
run: |
|
||||||
|
docker build -t your-image-name:latest -f ./Dockerfile .
|
||||||
|
|
||||||
|
- name: Run Docker container
|
||||||
|
run: |
|
||||||
|
docker run -d -p 8080:8080 your-image-name:latest
|
||||||
26
Dokerfile.yaml
Normal file
26
Dokerfile.yaml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# 使用官方的 Go 运行时作为基础镜像
|
||||||
|
FROM golang:1.18-alpine AS builder
|
||||||
|
|
||||||
|
# 设置工作目录
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 将当前目录下的所有文件复制到工作目录中
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# 构建可执行文件
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -v -o app .
|
||||||
|
|
||||||
|
# 使用一个更小的基础镜像来减小最终镜像的大小
|
||||||
|
FROM alpine:latest
|
||||||
|
|
||||||
|
# 设置工作目录
|
||||||
|
WORKDIR /root/
|
||||||
|
|
||||||
|
# 从构建阶段复制可执行文件
|
||||||
|
COPY --from=builder /app/app .
|
||||||
|
|
||||||
|
# 暴露端口
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
# 运行可执行文件
|
||||||
|
CMD ["./app"]
|
||||||
Reference in New Issue
Block a user