diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 684a849..f1cc4db 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,6 +25,8 @@ jobs: - name: Docker tag id: vars run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10}) + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 - name: Docker Buildx uses: docker/setup-buildx-action@v1 - name: Docker login @@ -33,54 +35,17 @@ jobs: registry: registry.cn-hangzhou.aliyuncs.com password: ${{ secrets.DOCKER_PASSWORD }} username: ${{ secrets.DOCKER_USERNAME }} - - name: Build binary - run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build - - name: Build and push - uses: docker/build-push-action@v2 - with: - context: . - file: ./Dockerfile - push: true - tags: | - registry.cn-hangzhou.aliyuncs.com/deepzz/eiblog:latest - registry.cn-hangzhou.aliyuncs.com/deepzz/eiblog:${{ steps.vars.outputs.tag }} + - name: Build image + run: scripts/run_build.sh deepzz0 ${{ steps.vars.outputs.tag }} - name: Package tar - run: ./dist.sh - - name: Create release - id: create_release - uses: actions/create-release@v1 + run: scripts/dist_tar.sh ${{ steps.vars.outputs.tag }} + - name: Release push + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - tag_name: ${{ steps.vars.outputs.tag }} + files: | + *.tar.gz release_name: Release ${{ steps.vars.outputs.tag }} - draft: false - prerelease: false - - name: Upload asset darwin - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: eiblog-${{ steps.vars.outputs.tag }}.darwin-amd64.tar.gz - asset_name: eiblog-${{ steps.vars.outputs.tag }}.darwin-amd64.tar.gz - asset_content_type: application/gzip - - name: Upload asset linux - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: eiblog-${{ steps.vars.outputs.tag }}.linux-amd64.tar.gz - asset_name: eiblog-${{ steps.vars.outputs.tag }}.linux-amd64.tar.gz - asset_content_type: application/gzip - - name: Upload asset windows - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: eiblog-${{ steps.vars.outputs.tag }}.windows-amd64.tar.gz - asset_name: eiblog-${{ steps.vars.outputs.tag }}.windows-amd64.tar.gz - asset_content_type: application/gzip diff --git a/.gitignore b/.gitignore index 7a02538..001a28f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so *.dylib *.DS_Store +*.tar.gz # Test binary, built with `go test -c` *.test diff --git a/Makefile b/Makefile index 32836cf..13b61ce 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ .PHONY: demo build swag -REGISTRY=localhost -IMAGE_TAG=`git describe --tags` +tag=`git describe --abbrev=0 --tags` swag: @scripts/swag_init.sh @@ -19,9 +18,9 @@ blog: backup: @scripts/run_app.sh backup -# build docker -build: - @scripts/run_build.sh $(REGISTRY) $(IMAGE_TAG) +# dist tar +dist: + @scripts/dist_tar.sh $(tag) # protoc protoc: diff --git a/build/package/backup.Dockerfile b/build/package/backup.Dockerfile index 608a932..cc900b8 100644 --- a/build/package/backup.Dockerfile +++ b/build/package/backup.Dockerfile @@ -8,7 +8,7 @@ COPY LICENSE /app/LICENSE COPY bin/backend /app/backend COPY conf /app/conf -EXPOSE 9000 +EXPOSE 9001 WORKDIR /app CMD ["backend"] diff --git a/build/package/blog.Dockerfile b/build/package/blog.Dockerfile index 608a932..b565ba5 100644 --- a/build/package/blog.Dockerfile +++ b/build/package/blog.Dockerfile @@ -7,6 +7,8 @@ COPY LICENSE /app/LICENSE COPY bin/backend /app/backend COPY conf /app/conf +COPY website /app/website +COPY assets /app/assets EXPOSE 9000 diff --git a/cmd/backup/main.go b/cmd/backup/main.go new file mode 100644 index 0000000..dc684bd --- /dev/null +++ b/cmd/backup/main.go @@ -0,0 +1,8 @@ +// Package main provides ... +package main + +import "fmt" + +func main() { + fmt.Println("hello world!") +} diff --git a/scripts/dist_tar.sh b/scripts/dist_tar.sh new file mode 100755 index 0000000..ef54042 --- /dev/null +++ b/scripts/dist_tar.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env sh + +set -e + +_tag="$1" +_arch=$(go env GOARCH) + +# prepare dir ./bin +mkdir -p ./bin + +for file in pkg/core/*; do + app="$(basename $file)"; + # tar platform + for os in linux darwin windows; do + _target="$app-$_tag.$os-$_arch.tar.gz" + CGO_ENABLED=0 GOOS=$os GOARCH=$_arch \ + go build -o bin/backend "./cmd/$app" + if [ "$app" == "blog" ]; then + tar czf $_target conf website assets bin/backend + else + tar czf $_target conf bin/backend + fi + done +done + +# clean dir ./bin +rm -rf ./bin diff --git a/scripts/run_build.sh b/scripts/run_build.sh index 0ebdf4d..67028ad 100755 --- a/scripts/run_build.sh +++ b/scripts/run_build.sh @@ -4,6 +4,7 @@ set -e _registry="$1" _tag="$2" +_platform="linux/amd64,linux/arm64,linux/386" if [ -z "$_registry" ] || [ -z "$_tag" ]; then echo "Please specify image repository and tag." @@ -12,12 +13,18 @@ fi # prepare dir ./bin mkdir -p ./bin +# create builder +docker buildx create --use --name builder # build demo app for file in pkg/core/*; do app="$(basename $file)"; - GOOS=linux GOARCH=amd64 go build -o bin/backend "./cmd/$app" - docker build -f "build/package/$app.Dockerfile" -t "$_registry/$app:$_tag" . + CGO_ENABLED=0 go build -o bin/backend "./cmd/$app" + # docker image + docker buildx build --platform "$_platform" \ + -t "$_registry/$app:latest" \ + -t "$_registry/$app:$_tag" \ + --push . done # clean dir ./bin