From 9c15001372458e032b6b5ef5d4948ec620800c33 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 11 Jun 2023 10:42:27 +0300 Subject: [PATCH] Add GitHub actions (#4) * Minor changes in README.md * Eathfile: add +multi-build and release +targets * Add ci github action * Get rid of tweeter leftovers * Skip validate_mr job during releases --- .github/workflows/ci.yaml | 68 +++++++++++++++++++++++++++++++++++++++ .gitignore | 2 ++ Earthfile | 66 +++++++++++++++++++++++++++++++++++-- 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..96dd0e3 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,68 @@ +name: frp-port-keeper-ci +on: + workflow_dispatch: + push: + paths-ignore: + - './*.md' + tags: + - 'v[0-9]+.[0-9]+.*' + branches: + - master + pull_request: + branches: + - master + +env: + FORCE_COLOR: 1 + +jobs: + # TODO: Add test job + # test: + + validate_pr: + permissions: + contents: read + packages: read + if: ${{ !startsWith(github.ref, 'refs/tags/v') }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Put back the git branch into git (Earthly uses it for tagging) + run: | + branch="" + if [ -n "$GITHUB_HEAD_REF" ]; then + branch="$GITHUB_HEAD_REF" + else + branch="${GITHUB_REF##*/}" + fi + git checkout -b "$branch" || true + - name: Download Earthly + run: "sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/download/v0.7.8/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly'" + - name: Validate PR + run: | + earthly --ci +validate-pr + + release: + permissions: + contents: write + # needs: test + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Put back the git branch into git (Earthly uses it for tagging) + run: | + branch="" + if [ -n "$GITHUB_HEAD_REF" ]; then + branch="$GITHUB_HEAD_REF" + else + branch="${GITHUB_REF##*/}" + fi + git checkout -b "$branch" || true + - name: Download Earthly + run: "sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/download/v0.7.8/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly'" + - name: Build and create release + run: | + earthly --ci +release \ + --RELEASE_VERSION=${GITHUB_REF:10} \ + --GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 5f0fe25..1dbc82c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ gokv bin build +.arg +.env diff --git a/Earthfile b/Earthfile index cdb8a94..b601f8d 100644 --- a/Earthfile +++ b/Earthfile @@ -1,12 +1,74 @@ -VERSION 0.6 +VERSION 0.7 FROM golang:1.18-bullseye -build: +validate-pr: WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o ./build/frp-port-keeper ./main.go +multi-build: + ARG --required RELEASE_VERSION + ENV PLATFORMS="darwin/amd64 darwin/arm64 windows/amd64 linux/amd64 linux/arm64" + ENV VERSION_INJECT="github.com/librepod/frp-port-keeper/main.Version" + ENV OUTPUT_PATH_FORMAT="./build/${RELEASE_VERSION}/{{.OS}}/{{.Arch}}/frp-port-keeper" + + WORKDIR /app + COPY go.mod go.sum ./ + RUN go mod download + COPY . . + RUN go install github.com/mitchellh/gox@v1.0.1 \ + && gox -osarch="${PLATFORMS}" -ldflags "-X ${VERSION_INJECT}=${RELEASE_VERSION}" -output "${OUTPUT_PATH_FORMAT}" + SAVE ARTIFACT build /build AS LOCAL ./build + +release: + FROM +multi-build + + ARG --required GITHUB_TOKEN + ARG --required RELEASE_VERSION + ARG OUT_BASE="./build/${RELEASE_VERSION}" + + # Install gh-cli + RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ + && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ + && apt-get update && apt-get install gh jq -y \ + && gh --version + + # Generate release notes + RUN gh api -X POST 'repos/librepod/frp-port-keeper/releases/generate-notes' \ + -F commitish=${RELEASE_VERSION} \ + -F tag_name=${RELEASE_VERSION} \ + > tmp-release-notes.json \ + && cat ./tmp-release-notes.json | jq + + # Gzip the bins + RUN tar -czvf "${OUT_BASE}/darwin/amd64/frp-port-keeper_darwin_amd64.tar.gz" -C "${OUT_BASE}/darwin/amd64" frp-port-keeper \ + && tar -czvf "${OUT_BASE}/darwin/arm64/frp-port-keeper_darwin_arm64.tar.gz" -C "${OUT_BASE}/darwin/arm64" frp-port-keeper \ + && tar -czvf "${OUT_BASE}/windows/amd64/frp-port-keeper_windows_amd64.tar.gz" -C "${OUT_BASE}/windows/amd64" frp-port-keeper.exe \ + && tar -czvf "${OUT_BASE}/linux/amd64/frp-port-keeper_linux_amd64.tar.gz" -C "${OUT_BASE}/linux/amd64" frp-port-keeper \ + && tar -czvf "${OUT_BASE}/linux/arm64/frp-port-keeper_linux_arm64.tar.gz" -C "${OUT_BASE}/linux/arm64" frp-port-keeper + + # Create release + RUN jq -r .body tmp-release-notes.json > tmp-release-notes.md \ + && gh release create ${RELEASE_VERSION} \ + --title "$(jq -r .name tmp-release-notes.json)" \ + --notes-file tmp-release-notes.md \ + --verify-tag \ + --draft \ + "${OUT_BASE}/darwin/amd64/frp-port-keeper_darwin_amd64.tar.gz#frp-port-keeper_osx_amd64" \ + "${OUT_BASE}/darwin/arm64/frp-port-keeper_darwin_arm64.tar.gz#frp-port-keeper_osx_arm64" \ + "${OUT_BASE}/windows/amd64/frp-port-keeper_windows_amd64.tar.gz#frp-port-keeper_windows_amd64" \ + "${OUT_BASE}/linux/amd64/frp-port-keeper_linux_amd64.tar.gz#frp-port-keeper_linux_amd64" \ + "${OUT_BASE}/linux/arm64/frp-port-keeper_linux_arm64.tar.gz#frp-port-keeper_linux_arm64" + + SAVE ARTIFACT build /build AS LOCAL ./build + +validate-mr: + # Smoke test the application + # TODO: set proper validation + BUILD +build +