Skip to content

Commit

Permalink
chore: Update build and workflow script
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsang committed Nov 22, 2024
1 parent ab11c19 commit be98475
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 207 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
.github
example*
44 changes: 21 additions & 23 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
name: docker
name: build

on:
create:
tags:
- v*

jobs:
docker:
build:
# https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#standard-github-hosted-runners-for-public-repositories
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@main

- name: Get Version
id: get_version
run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
shell: bash
run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

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

- name: Login Docker Hub
uses: docker/login-action@master
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and Push Docker
shell: bash
run: |
SERVICE_NAME=ddns
TARGET_PATH=build
DOCKER_REPO=gitsang
VERSION=${{ steps.get_version.outputs.VERSION }}
mkdir -p ${TARGET_PATH} ${TARGET_PATH}/bin ${TARGET_PATH}/conf ${TARGET_PATH}/log
CGO_ENABLED=0 go build \
-ldflags "-X ddns/pkg/config.Version=${VERSION}" \
-o ${TARGET_PATH}/bin/${SERVICE_NAME} cmd/${SERVICE_NAME}.go
cp configs/template.yml ${TARGET_PATH}/conf
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v6/linux/arm/v7,linux/arm/v8 \
-f Dockerfile --no-cache \
--build-arg DOCKER_PACKAGE_PATH=${TARGET_PATH} \
-t ${DOCKER_REPO}/${SERVICE_NAME}:${VERSION} \
--push .
- name: Build and Push
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
push: true
tags: 'gitsang/ddns:latest, gitsang/ddns:${{ steps.get_version.outputs.VERSION }}'
platforms: linux/amd64, linux/arm64, linux/arm/v7
build-args: VERSION=${{ steps.get_version.outputs.VERSION }}
24 changes: 2 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,2 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# make target
/bin
/target
/build
/ddns
*.tar.gz

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# dist
/dist
30 changes: 19 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
FROM ubuntu
FROM golang:1.22-bookworm AS builder

ARG DOCKER_PACKAGE_PATH
ENV DOCKER_PACKAGE_PATH ${DOCKER_PACKAGE_PATH}
ARG VERSION=latest

ENV SERVICE_NAME=ddns
ENV SERVICE_HOME=/opt/${SERVICE_NAME}
ENV PATH=$PATH:${SERVICE_HOME}/bin
ENV GO111MODULE=on

RUN apt update \
&& apt install -y --no-install-recommends git \
&& rm -rf /var/lib/apt/lists/* \
&& go install github.com/gitsang/ddns@${VERSION}

FROM debian:bookworm AS dist

LABEL maintainer="sang <[email protected]>"

RUN apt update \
&& apt install -y --no-install-recommends apt-transport-https ca-certificates curl \
&& rm -rf /var/lib/apt/lists/*

COPY ${DOCKER_PACKAGE_PATH} ${SERVICE_HOME}
RUN apt update
RUN apt install -y --no-install-recommends ca-certificates curl
RUN update-ca-certificates

WORKDIR ${SERVICE_HOME}/bin
ENTRYPOINT [ "ddns" ]
COPY --from=builder /go/bin/ddns /usr/bin/ddns

ENTRYPOINT [ "/usr/bin/ddns" ]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 sangria
Copyright (c) 2024 Sang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
156 changes: 103 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,77 +1,127 @@
.PHONY: default help build docker
# repo
NAME=ddns

SERVICE_NAME=ddns
TARGET_PATH=build
DOCKER_REPO=gitsang
VERSION=$(shell git describe --tags)
# version
VERSION=$(shell git describe --tags --always --dirty)
BUILD_DATE=$(shell date -u --iso-8601=seconds)

default: help
# system
SHELL := /usr/bin/env bash

help: ## show help
# go
GO := go
GOHOSTOS := $(shell $(GO) env GOHOSTOS)
GOPATH := $(shell $(GO) env GOPATH)
GO_VERSION := $(shell $(GO) version | awk '{print $$3}')
GOOS := $(if $(GOOS),$(GOOS),$(shell $(GO) env GOOS))
GOARCH := $(if $(GOARCH),$(GOARCH),$(shell $(GO) env GOARCH))
GO_MODULE := $(shell awk '/module/{print $$2}' go.mod)
GIT_COMMIT := $(shell git rev-parse HEAD)
DIST := dist/$(NAME)/$(GOOS)/$(GOARCH)

@echo -e "Usage: \n\tmake \033[36m[option]\033[0m"
@echo -e "Options:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\t\033[36m%-20s\033[0m %s\n", $$1, $$2}'
# build
LD_FLAGS += -X "main.Version=$(VERSION)"
LD_FLAGS += -X "main.BuildDate=$(BUILD_DATE)"
LD_FLAGS += -X "main.GoVersion=$(GO_VERSION)"
LD_FLAGS += -X "main.GOOS=$(GOOS)"
LD_FLAGS += -X "main.GOARCH=$(GOARCH)"
LD_FLAGS += -X "main.GitCommit=$(shell git rev-parse HEAD)"

# docker
DOCKER = docker
DOCKER_REGISTRY = gitsang

LD_FLAGS=-ldflags "-X ddns/pkg/config.Version=$(VERSION)"

clean: ## clean build target
#------------------------------------------------------------------------------#
##@ Debug
#------------------------------------------------------------------------------#

rm -fr $(TARGET_PATH)
rm -f *.tar.gz

build: clean ## build target
.PHONY: test
## run tests
test:
$(GO) test -race ./...

mkdir -p $(TARGET_PATH) $(TARGET_PATH)/bin $(TARGET_PATH)/conf $(TARGET_PATH)/log
go build $(LD_FLAGS) -o $(TARGET_PATH)/bin/$(SERVICE_NAME) cmd/$(SERVICE_NAME).go
cp configs/template.yml $(TARGET_PATH)/conf
cp configs/ddns.service $(TARGET_PATH)/conf

download: ## download package
.PHONY: run
## run
run:
$(GO) run . \
$(filter-out $@, $(MAKECMDGOALS))

mkdir -p $(TARGET_PATH) $(TARGET_PATH)/bin $(TARGET_PATH)/conf $(TARGET_PATH)/log
wget -c https://github.com/gitsang/ddns/releases/download/v0.0.11/ddns -P $(TARGET_PATH)/bin/
wget -c https://github.com/gitsang/ddns/releases/download/v0.0.11/template.yml -P $(TARGET_PATH)/conf/
wget -c https://github.com/gitsang/ddns/releases/download/v0.0.11/ddns.service -P $(TARGET_PATH)/conf/

tag: build ## make tgz package
#------------------------------------------------------------------------------#
##@ Build
#------------------------------------------------------------------------------#

cp -r $(TARGET_PATH) $(SERVICE_NAME)
tar zcvf $(SERVICE_NAME).$(VERSION).tar.gz $(SERVICE_NAME)
rm -fr $(SERVICE_NAME)

docker: build ## build docker
BUILD_DIRS := $(DIST)

docker build -f Dockerfile --no-cache \
--build-arg DOCKER_PACKAGE_PATH=$(TARGET_PATH) \
-t $(DOCKER_REPO)/$(SERVICE_NAME):$(VERSION) .
docker tag $(DOCKER_REPO)/$(SERVICE_NAME):$(VERSION) $(DOCKER_REPO)/$(SERVICE_NAME):latest

publish: docker ## publish docker to docker repo
$(BUILD_DIRS):
@mkdir -p $@

docker push $(DOCKER_REPO)/$(SERVICE_NAME):$(VERSION)
docker push $(DOCKER_REPO)/$(SERVICE_NAME):latest

pull: ## pull latest published docker
.PHONY: build
## build
build:
$(GO) build -o $(DIST)/$(NAME) .

docker pull $(DOCKER_REPO)/$(SERVICE_NAME):latest

install: ## install by systemd
.PHONY: docker
DOCKERFILE ?= Dockerfile
## build docker
docker: $(BUILD_DIRS)
$(DOCKER) build \
--no-cache \
--build-arg VERSION=$(shell git rev-parse HEAD) \
-t $(DOCKER_REGISTRY)/$(NAME):$(VERSION) \
-f $(DOCKERFILE) .

cp $(TARGET_PATH)/bin/$(SERVICE_NAME) /usr/local/bin/
cp configs/ddns.service /etc/systemd/system/
cp configs/template.yml /usr/local/etc/ddns/ddns.yml
mkdir -p /var/log/ddns/

docker-install: docker ## build docker and run
.PHONY: publish
## publish docker
publish:
$(DOCKER) push $(DOCKER_REGISTRY)/$(NAME):$(VERSION)

mkdir -p /data/ddns/conf /data/ddns/log
docker rm -f ddns
docker run -d \
--name ddns \
--restart always \
--network host \
-v /data/ddns/conf/ddns.yml:/opt/ddns/conf/ddns.yml \
-v /data/ddns/log:/opt/ddns/log \
$(DOCKER_REPO)/$(SERVICE_NAME):latest


#------------------------------------------------------------------------------#
##@ Clean
#------------------------------------------------------------------------------#


.PHONY: clean
## clean up git repo
clean:
git clean -xfd
rm -fr $(DIST)


#------------------------------------------------------------------------------#
##@ Help
#------------------------------------------------------------------------------#


## display help
help:
@awk 'BEGIN \
{ \
FS = ":.*##"; \
printf "\nUsage:\n make \033[36m<target>\033[0m\n" \
} \
/^[0-9a-zA-Z\_\-]+:/ \
{ \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf " \033[36m%-24s\033[0m %s\n", helpCommand,helpMessage; \
} \
} { lastLine = $$0 } \
/^##@/ \
{ \
printf "\n\033[1m%s\033[0m\n", substr($$0, 5) \
} ' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help
59 changes: 16 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,22 @@
# ddns
# DDNS

ddns service

## run docker
## Run with Compose

```sh
mkdir -p /data/ddns/conf /data/ddns/log
docker rm -f ddns
docker run -d \
--name ddns \
--restart always \
--network host \
-v ./conf:/opt/ddns/conf \
-v ./log:/opt/ddns/log \
gitsang/ddns:latest
```
Docker image from [https://hub.docker.com/r/gitsang/ddns](https://hub.docker.com/r/gitsang/ddns)

## config

example of `./conf/ddns.yml`

```yml
accesskeyid: <aliyun_ram_account_accesskeyid>
accesskeysecret: <aliyun_ram_account_accesskeysecret>
domain: <main_domain_name>
updateintervalmin: 60
ddnss:
- enable: false
type: "A"
rr: "template.home"
interface: "ens18"
prefix: "192.168"
- enable: false
type: "A"
rr: "*.template.home"
interface: "ens18"
prefix: "192.168"
- enable: false
type: "AAAA"
rr: "template6.home"
interface: "ens18"
prefix: "240e"
- enable: false
type: "AAAA"
rr: "*.template6.home"
interface: "ens18"
prefix: "240e"
```
services:
ddns:
container_name: ddns
image: gitsang/ddns:latest
restart: always
network_mode: host
volumes:
- ./config.yaml:/etc/ddns/config.yaml
- /var/log/ddns:/var/log/ddns
command: -c /etc/ddns/config.yaml
```

Config file example see [./configs/example.yaml](./configs/example.yaml)
File renamed without changes.
Loading

0 comments on commit be98475

Please sign in to comment.