From 651db0aa37ab65519f0a17ec878127f1aebf6fc3 Mon Sep 17 00:00:00 2001 From: Owen Ou Date: Sun, 1 Oct 2023 19:11:50 -0700 Subject: [PATCH] Add release CI --- .github/workflows/release.yaml | 31 ++++++--- .gitignore | 3 +- .goreleaser.yml | 114 +++++++++++++++++++++++++++++++++ Makefile | 13 +++- 4 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 .goreleaser.yml diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 31820ffa..5f238fcc 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,18 +1,31 @@ name: Release on: + workflow_call: + workflow_dispatch: push: tags: 'v*' jobs: - homebrew: - name: Bump Homebrew formula + goreleaser: + name: GoReleaser runs-on: ubuntu-latest steps: - - uses: mislav/bump-homebrew-formula-action@v2 + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 with: - formula-name: candy - homebrew-tap: owenthereal/homebrew-candy - base-branch: master - commit-message: | - {{formulaName}} {{version}} + go-version-file: go.mod + check-latest: true + - name: Generate tag name + run: | + # tag > commit + echo "build_version=$(git describe --tags --exact-match 2> /dev/null || git rev-parse --short HEAD)" >> "$GITHUB_ENV" + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + version: v1.21.2 + args: release --clean env: - COMMITTER_TOKEN: ${{ secrets.GH_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + CI_GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + GORELEASER_CURRENT_TAG: ${{ env.build_version }} diff --git a/.gitignore b/.gitignore index 05ea9a5e..7a64fe87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -build +/bin +/dist c.out diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 00000000..199bd31e --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,114 @@ +project_name: candy +builds: + - <<: &build_defaults + goos: [darwin, linux] + goarch: ["386", "amd64", "arm", "arm64"] + env: + - CGO_ENABLED=0 + id: candy + binary: bin/candy + main: ./cmd/candy +archives: + - id: nix + builds: [candy] + name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}' + wrap_in_directory: false + format: tar.gz + files: + - LICENSE* + - README* +checksum: + name_template: "checksums.txt" +snapshot: + name_template: "{{ incpatch .Version }}-snapshot" +release: + prerelease: auto + name_template: "Candy {{.Version}}" + github: + owner: owenthereal + name: candy +brews: + - repository: + owner: owenthereal + name: homebrew-candy + token: "{{ .Env.GITHUB_TOKEN }}" + commit_author: + name: Owen Ou + email: o@owenout.com + homepage: https://github.com/owenthereal/candy + description: Zero-config reverse proxy server + folder: Formula + license: "Apache 2.0" + custom_block: | + head "https://github.com/owenthereal/candy.git" + install: | + bin.install "bin/candy" + prefix.install_metafiles + etc.install "example/candyconfig" => "candyconfig" + (etc/"resolver").install "example/mac/candy-test" => "candy-test" + service: | + run [opt_bin/"candy", "launch", "--dns-local-ip"] + keep_alive true + run_at_load true + sockets "Socket" => "tcp://0.0.0.0:80", "SocketTLS" => "tcp://0.0.0.0:443" + working_dir HOMEBREW_PREFIX + log_path var/"log/candy/output.log" + error_log_path var/"log/candy/output.log" + caveats: | + <<~EOS + To finish the installation, you need to create a DNS resolver file + in /etc/resolver/YOUR_DOMAIN. Creating the /etc/resolver directory + and the config file requires superuser privileges. You can set things + up with an one-liner + + sudo candy setup + + Alternatively, you can execute the following bash script + + sudo mkdir -p /etc/resolver && \\ + sudo chown -R $(whoami):$(id -g -n) /etc/resolver && \\ + cp #{etc/"resolver/candy-test"} /etc/resolver/candy-test + + To have launchd start Candy now and restart at login + + brew services start candy + + Or, if you don't want/need a background service you can just run + + candy run + + A sample Candy config file is in #{etc/"candyconfig"}. You can + copy it to your home to override Candy's default setting + + cp #{etc/"candyconfig"} ~/.candyconfig + EOS + test: | + http = free_port + https = free_port + dns = free_port + admin = free_port + + mkdir_p testpath/".candy" + (testpath/".candy/app").write(admin) + + (testpath/"candyconfig").write <<~EOS + { + "domain": ["brew-test"], + "http-addr": "127.0.0.1:#{http}", + "https-addr": "127.0.0.1:#{https}", + "dns-addr": "127.0.0.1:#{dns}", + "admin-addr": "127.0.0.1:#{admin}", + "host-root": "#{testpath/".candy"}" + } + EOS + puts shell_output("cat #{testpath/"candyconfig"}") + + fork do + exec bin/"candy", "run", "--config", testpath/"candyconfig" + end + + sleep 2 + + assert_match "\":#{http}\"", shell_output("curl -s http://127.0.0.1:#{admin}/config/apps/http/servers/candy/listen/0") + assert_match "\":#{https}\"", shell_output("curl -s http://127.0.0.1:#{admin}/config/apps/http/servers/candy/listen/1") + assert_match "127.0.0.1", shell_output("dig +short @127.0.0.1 -p #{dns} app.brew-test") diff --git a/Makefile b/Makefile index be12fd07..b996e3ba 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ install: .PHONY: build build: - go build -o build/candy ./cmd/candy + go build -o bin/candy ./cmd/candy .PHONY: vet vet: @@ -15,3 +15,14 @@ vet: .PHONY: test test: go test ./... -timeout=5m -coverprofile=c.out -covermode=atomic -count=1 -race -v + +BIN_DIR ?= $(CURDIR)/bin +export PATH := $(BIN_DIR):$(PATH) +.PHONY: tools +tools: + # goreleaser + GOBIN=$(BIN_DIR) go install github.com/goreleaser/goreleaser@latest + +.PHONY: goreleaser +goreleaser: + goreleaser release --clean --snapshot --skip=publish