Skip to content

Commit

Permalink
ci: docker镜像构建
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jul 5, 2024
1 parent 2bad258 commit 421c58f
Show file tree
Hide file tree
Showing 28 changed files with 454 additions and 97 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
/runtime
/bin

data.db

/frontend/node_modules
/frontend/dist
/data
75 changes: 75 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Docker

on:
push:
branches:
- main
tags:
- 'v*'

env:
APP_NAME: ${{ github.event.repository.name }}
NAMESPACE: app
REGISTRY: ${{ secrets.DOCKER_REGISTRY && secrets.DOCKER_REGISTRY || 'docker.io' }}
REPOSITORY: ${{ github.repository }}
IMAGE_LATEST_TAG: ${{ startsWith(github.ref, 'refs/tags/') && 'latest' || 'main' }}

jobs:
docker:
runs-on: ubuntu-latest
strategy:
matrix:
goos: [ linux ]
goarch: [ amd64, arm, arm64 ]
fail-fast: true
steps:
- uses: actions/checkout@v4

- name: Set environment variables
run: |
if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
VERSION_TAG=${GITHUB_REF#refs/tags/}
else
VERSION_TAG=${IMAGE_LATEST_TAG}
fi
echo "VERSION_TAG=${VERSION_TAG}" >> $GITHUB_ENV
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22'

- name: Mkdir
run: |
mkdir -p ./frontend/dist
echo "" > ./frontend/dist/index.html
- name: Lint
uses: golangci/golangci-lint-action@v6

- name: Tests
run: |
go test $(go list ./...)
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Docker build and push
uses: docker/build-push-action@v5
with:
platforms: ${{ matrix.goos }}/${{ matrix.goarch }}
push: true
file: Dockerfile
# 如果IMAGE_TAG为空,则使用${{ steps.vars.outputs.sha_short }}
tags: codfrm/dns-kit:${{ env.IMAGE_LATEST_TAG }},codfrm/dns-kit:${{ env.VERSION_TAG }}
context: .
100 changes: 100 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Release

on:
push:
tags:
- 'v*'

env:
APP_NAME: "dns-kit"
BINARY_SUFFIX: ""
COMMIT_ID: "${{ github.sha }}"

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows, darwin]
goarch: ["386", amd64, arm, arm64]
exclude:
- goos: darwin
goarch: arm
- goos: darwin
goarch: "386"
fail-fast: true
steps:
- uses: actions/checkout@v3

- name: Set environment variables
run: |
if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}-${COMMIT_ID::7}
else
VERSION=${GITHUB_REF#refs/heads/}-${COMMIT_ID::7}
fi
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Tests
run: |
go test $(go list ./...)
- name: Build binary file
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
run: |
if [ $GOOS = "windows" ]; then export BINARY_SUFFIX="$BINARY_SUFFIX.exe"; fi
export CGO_ENABLED=0
export LD_FLAGS="-w -s -X github.com/codfrm/cago/configs.Version=${VERSION}"
go build -o "bin/${APP_NAME}${BINARY_SUFFIX}" -trimpath -ldflags "$LD_FLAGS" ./cmd/app
cd bin
if [ "${{ matrix.goos }}" = "windows" ]; then
zip -j "${APP_NAME}_${GOOS}_${GOARCH}.zip" "${APP_NAME}.exe" "${APP_NAME}.exe"
else
tar czvf "${APP_NAME}_${GOOS}_${GOARCH}.tar.gz" "${APP_NAME}" "${APP_NAME}"
fi
- name: Upload artifact
uses: actions/upload-artifact@v3
if: ${{ matrix.goos != 'windows' }}
with:
name: ${{ matrix.goos }}_${{ matrix.goarch }}
path: bin/*.tar.gz

- name: Upload windows artifact
uses: actions/upload-artifact@v3
if: ${{ matrix.goos == 'windows' }}
with:
name: ${{ matrix.goos }}_${{ matrix.goarch }}
path: bin/*.zip

release:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# 拿到build产物
- uses: actions/download-artifact@v3
with:
path: bin/

- uses: ncipollo/release-action@v1
with:
artifacts: "bin/*/*.tar.gz,bin/*/*.zip"
body: "no describe"
# 判断是否为预发布(包含alpha、beta等关键字)
prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') }}
34 changes: 34 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Test

on:
push:
branches:
- !main
- !release/*
- !test/*

pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
install-mode: goinstall
args: --timeout=30m

- name: Tests
run: |
go test $(go list ./...)
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@

/runtime
/bin

data.db
/datworkflowsa
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ linters:
- nolintlint

run:
timeout: 10m
timeout: 10m

linters-settings:
stylecheck:
Expand Down
14 changes: 11 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM golang:1.22-alpine3.19 as build

# 设置国内镜像
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
go env -w GOPROXY=https://goproxy.cn,direct
#RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
# go env -w GOPROXY=https://goproxy.cn,direct

RUN apk add --no-cache nodejs npm yarn make

Expand All @@ -20,4 +20,12 @@ WORKDIR /app

COPY --from=build /app/bin /app/dns-kit

CMD ["/app/dns-kit"]
COPY ./configs/config.yaml.example /app/configs/config.yaml.example

COPY ./deploy/entrypoint.sh /app/entrypoint.sh

RUN chmod +x /app/entrypoint.sh && chmod +x /app/dns-kit

ENTRYPOINT ["/app/entrypoint.sh"]

CMD ["/app/dns-kit","--config","./runtime/config.yaml"]
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ build:
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/$(APP_NAME)_v$(APP_VERSION)$(SUFFIX) ./cmd/app

docker:
docker build -t $(APP_NAME):$(APP_VERSION) .
docker build --platform=$(GOOS)/$(GOARCH) -t $(APP_NAME):$(APP_VERSION) .

docker-push:
docker tag $(APP_NAME):$(APP_VERSION) codfrm/$(APP_NAME):$(APP_VERSION)
#docker push codfrm/$(APP_NAME):$(APP_VERSION)
docker tag $(APP_NAME):$(APP_VERSION) codfrm/$(APP_NAME):latest
#docker push codfrm/$(APP_NAME):latest
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
# DNS Kit
# DNS Kit
> 一个可以申请SSL证书部署到CDN的工具
![32c977b8bcba2d7b5b7062c589a3889c](img/README.assets/32c977b8bcba2d7b5b7062c589a3889c.png)

## 功能

- 支持管理多平台DNS记录
- 支持管理多平台CDN
- 支持自动申请SSL证书
- 支持自动部署SSL证书到CDN

## 支持平台

- 腾讯云(CDN、DNS)
- 阿里云(DNS)
- 七牛云(CDN)
- Cloudflare(DNS)
- Kubernetes(CDN)

## 部署方式

### Docker(推荐)

```
docker run -d \
-v $PWD/data:/app/runtime \
-p 5174:5174 \
--name dns-kit \
--restart unless-stopped \
codfrm/dns-kit:latest
```

### docker-compose(推荐)

```yaml
version: '3'

services:
dns-kit:
image: codfrm/dns-kit:latest
container_name: dns-kit
restart: unless-stopped
volumes:
- ./data:/app/runtime
ports:
- "5174:5174"
```
```bash
docker-compose up -d
```

### 从源代码

从源代码构建需要预先准备好以下环境:

- go 1.22+
- node 20+
- yarn

```bash
git clone https://github.com/CodFrm/dns-kit.git
cd dns-kit
make build
# 创建配置文件
cp ./configs/config.example.yaml ./configs/config.yaml
# 启动, 默认端口为5174, 版本号根据实际情况修改
./bin/dns-kit_v1.0.0
```

## 使用

访问 http://localhost:5174 默认账号密码为 `admin` `123456`

7 changes: 6 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"log"

"github.com/codfrm/cago"
Expand Down Expand Up @@ -29,8 +30,12 @@ import (
)

func main() {
var configFile string
flag.StringVar(&configFile, "config", "./configs/config.yaml", "config file")
flag.Parse()

ctx := context.Background()
cfg, err := configs.NewConfig("dns-kit")
cfg, err := configs.NewConfig("dns-kit", configs.WithConfigFile(configFile))
if err != nil {
log.Fatalf("load config err: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/codfrm/cago/configs"
)

var Version = ""

func EncryptKey() string {
return configs.Default().String(context.Background(), "encrypt_key")
}
Loading

0 comments on commit 421c58f

Please sign in to comment.