From c46acb993dc4cfc2b2a38aeb2717cb812cfea7e4 Mon Sep 17 00:00:00 2001 From: xhe Date: Sat, 8 Oct 2022 15:12:45 +0800 Subject: [PATCH] *: improve docker scripts (#104) --- .dockerignore | 12 +++++++++- .github/workflows/common.yml | 2 +- Makefile | 18 +++++++-------- docker/Dockerfile | 12 +++------- docker/apk-fastest-mirror.sh | 44 ------------------------------------ 5 files changed, 23 insertions(+), 65 deletions(-) mode change 120000 => 100644 .dockerignore delete mode 100644 docker/apk-fastest-mirror.sh diff --git a/.dockerignore b/.dockerignore deleted file mode 120000 index 3e4e48b0..00000000 --- a/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -.gitignore \ No newline at end of file diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..f3339059 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +.git/ +**/bin +**/test +**/*.iml +**/.idea +**/*.swp +**/.DS_Store +**/vendor +**/work +**/.vscode/ +**/.cover* diff --git a/.github/workflows/common.yml b/.github/workflows/common.yml index ecedd36e..018c217f 100644 --- a/.github/workflows/common.yml +++ b/.github/workflows/common.yml @@ -55,7 +55,7 @@ jobs: path: | ${{ env.GOCACHE }} ${{ env.GOMODCACHE }} - key: ${{ runner.os }}-go-${{ inputs.ref }} + key: ${{ runner.os }}-go-${{ inputs.target == "cache" && github.run_id || hashFiles('**/go.sum')}} restore-keys: | ${{ runner.os }}-go- - name: make ${{ inputs.target }} diff --git a/Makefile b/Makefile index 83b954d2..ca067d13 100644 --- a/Makefile +++ b/Makefile @@ -13,24 +13,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -PROJECTNAME = $(shell basename "$(PWD)") GOBIN := $(shell pwd)/bin -DOCKERFLAG ?= -RELEASE ?= -ifneq ($(RELEASE), "") - DOCKERFLAG ?= --squash -endif +DEBUG ?= +DOCKERPREFIX ?= BUILD_TAGS ?= LDFLAGS ?= -DEBUG ?= BUILDFLAGS := $(BUILDFLAGS) -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' -ifeq ("$(WITH_RACE)", "1") +ifneq ("$(DEBUG)", "") BUILDFLAGS += -race endif IMAGE_TAG ?= latest EXECUTABLE_TARGETS := $(patsubst cmd/%,cmd_%,$(wildcard cmd/*)) -.PHONY: cmd_% test lint docker golangci-lint gocovmerge +.PHONY: cmd_% test lint docker docker-release golangci-lint gocovmerge default: cmd @@ -64,4 +59,7 @@ test: gocovmerge go tool cover -html=.cover -o .cover.html docker: - docker build $(DOCKERFLAG) -t "tiproxy:${IMAGE_TAG}" --build-arg='GOPROXY=$(shell go env GOPROXY),BUILDFLAGS=$(BUILDFLAGS),' -f docker/Dockerfile . + docker build -t "$(DOCKERPREFIX)tiproxy:$(IMAGE_TAG)" --build-arg 'GOPROXY=$(shell go env GOPROXY),BUILDFLAGS=$(BUILDFLAGS),' -f docker/Dockerfile . + +docker-release: + docker buildx build --platform linux/amd64,linux/arm64 --push -t "$(DOCKERPREFIX)tiproxy:$(IMAGE_TAG)" --build-arg 'GOPROXY=$(shell go env GOPROXY),BUILDFLAGS=$(BUILDFLAGS),' -f docker/Dockerfile . diff --git a/docker/Dockerfile b/docker/Dockerfile index 6d5f5b32..ee332eea 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,15 +4,9 @@ EXPOSE 3080 EXPOSE 3081 EXPOSE 6000 -ADD cmd proxy/cmd -ADD lib proxy/lib -ADD pkg proxy/pkg -ADD conf proxy/conf -ADD go.* docker/apk-fastest-mirror.sh Makefile proxy/ -RUN sh ./proxy/apk-fastest-mirror.sh -RUN apk add --no-cache --progress git make go +ADD . https://raw.githubusercontent.com/njhallett/apk-fastest-mirror/main/apk-fastest-mirror.sh /proxy +RUN sh ./proxy/apk-fastest-mirror.sh -t 50 && apk add --no-cache --progress git make go ARG BUILDFLAGS ARG GOPROXY -RUN export BUILDFLAGS=${BUILDFLAGS} && export GOPROXY=${GOPROXY} && cd proxy && make cmd && cp bin/* /bin/ && cp -a conf /etc/proxy && cd .. && rm -rf proxy -RUN rm -rf $(go env GOMODCACHE GOCACHE) && apk del git make go +RUN export BUILDFLAGS=${BUILDFLAGS} && export GOPROXY=${GOPROXY} && cd proxy && make cmd && cp bin/* /bin/ && cp -a conf /etc/proxy && cd .. && rm -rf proxy && rm -rf $(go env GOMODCACHE GOCACHE GOPATH) && apk del git make go ENTRYPOINT ["/bin/tiproxy", "-conf", "/etc/proxy/proxy.yaml"] diff --git a/docker/apk-fastest-mirror.sh b/docker/apk-fastest-mirror.sh deleted file mode 100644 index 4831f373..00000000 --- a/docker/apk-fastest-mirror.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/sh -# shellcheck disable=SC3043 - -get_hostname_url() { - local n - n="${1#*://}" - echo "${n%%/*}" -} - -time_cmd() { - local proc start end - - proc=$(cut -d ' ' -f1 /proc/uptime) - start="$(echo "$proc" | cut -d . -f1)$(echo "$proc" | cut -d . -f2)" - "$@" >/dev/null 2>&1 || return - proc=$(cut -d ' ' -f1 /proc/uptime) - end="$(echo "$proc" | cut -d . -f1)$(echo "$proc" | cut -d . -f2)" - echo $((end - start)) -} - -DATA="" -MIRRORS=$(wget -qO- "http://rsync.alpinelinux.org/alpine/MIRRORS.txt") -DST=/etc/apk/mirrors.txt - -#find best -for URL in $MIRRORS; do - TIME=$(time_cmd wget -T 1 -t 1 -q "${URL%/}" -O /dev/null) - - if [ -n "$TIME" ]; then - echo "$(get_hostname_url "$URL") was $TIME" - DATA="$DATA$TIME $URL\n" - if [ "$TIME" -lt "50" ]; then - break - fi - fi - -done - -BEST=$(printf '%b' "$DATA" | sort -n | head -n 1 | cut -d ' ' -f2) -echo "Best mirror is: $BEST" - -sed -i -r 's#^http.+/(.+/main)#'"${BEST%/}"'/\1#' /etc/apk/repositories -sed -i -r 's#^http.+/(.+/community)#'"${BEST%/}"'/\1#' /etc/apk/repositories -sed -i -r 's#^http.+/(.+/testing)#'"${BEST%/}"'/\1#' /etc/apk/repositories