From f4e6372e068c306c7616ffc09d3f0377c18cb73e Mon Sep 17 00:00:00 2001 From: mehran-prs Date: Thu, 16 May 2024 19:37:49 +0330 Subject: [PATCH] ci: add ci to autobuild --- .github/workflows/ci.yaml | 150 ++++++++++++++++++++++++++++++++++++++ README.md | 1 - main.go | 14 +++- 3 files changed, 162 insertions(+), 3 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..1c08465 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,150 @@ +name: CI +on: + push: + tags: + - 'v*.*.*' + branches: + - main + - ci + pull_request: + types: [ opened, synchronize, reopened ] + workflow_dispatch: + inputs: + version: + description: 'Release version' + required: true + +jobs: + qa: + name: qa + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '1.21' + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.58 + - name: test + run: go test -race ./... + build: + name: Build + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + strategy: + matrix: + target: + - 'windows/amd64' + - 'windows/386' + - 'windows/arm64' + - 'linux/amd64' + - 'linux/386' + - 'linux/arm64' + - 'linux/arm' + - 'darwin/amd64' + - 'darwin/arm64' + - 'freebsd/386' + - 'freebsd/amd64' + - 'freebsd/arm' + - 'openbsd/amd64' + - 'openbsd/arm64' + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' + + - name: Set up GOOS and GOARCH + id: setup_env + run: | + echo "goos=$(echo ${{ matrix.target }} | cut -d'/' -f1)" >> $GITHUB_OUTPUT + echo "goarch=$(echo ${{ matrix.target }} | cut -d'/' -f2)" >> $GITHUB_OUTPUT + + - name: Build + env: + GOOS: ${{ steps.setup_env.outputs.goos }} + GOARCH: ${{ steps.setup_env.outputs.goarch }} + run: | + set -euo pipefail + + VERSION=${GITHUB_REF#refs/tags/v} + NAME="snip_${VERSION}_${GOOS}_${GOARCH}" + + go build CGO_ENABLED=0 -v \ + -ldflags '-X main.Version=`${VERSION}` -X main.Commit=`git rev-parse HEAD` -X main.Date=`date +'%FT%TZ%z'`'\ + -o ${NAME} . + + if [[ "$GOOS" != "windows" ]]; then + tar -czf "$NAME.tar.gz" snip + else + zip "$NAME.zip" snip.exe + fi + + - name: Upload Artifacts + uses: actions/upload-artifact@v4 + with: + name: snip_${{ steps.setup_env.outputs.goos }}_${{ steps.setup_env.outputs.goarch }} + path: | + *.zip + *.tar.gz + + release: + name: Release + if: startsWith(github.ref, 'refs/tags/v') + needs: [ qa, build ] + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Download Artifacts + uses: actions/download-artifact@v4 + + - name: Create checksums + id: checksums + run: | + set -euo pipefail + + mkdir dist + mv snip*/* dist/ + cd dist + + VERSION=${GITHUB_REF#refs/tags/v} + CHECKSUMS=snip_${VERSION}_checksums.txt + sha256sum * > $CHECKSUMS + + echo "version=${VERSION}" >> $GITHUB_OUTPUT + + - name: Generate release notes + id: release_notes + run: | + set -x + set -euo pipefail + + CURRENT_VERSION=${GITHUB_REF#refs/tags/} + PREV_VERSION=$(git describe --tags --abbrev=0 $CURRENT_VERSION^) + RELEASE_NOTES=${{ github.workspace }}/release-notes.txt + + printf "## Changelog\n\n" > $RELEASE_NOTES + git log ${PREV_VERSION}..${CURRENT_VERSION} --oneline --abbrev-commit >> $RELEASE_NOTES + cat $RELEASE_NOTES + + - name: Create GitHub Release + uses: softprops/action-gh-release@v4 + with: + name: ${{ steps.checksums.outputs.version }} + body_path: ${{ github.workspace }}/release-notes.txt + files: | + dist/* + tag_name: ${{ steps.checksums.outputs.version }} + draft: true + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index deb6de6..2574bc2 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,4 @@ golangci-lint run ./... # build go build -o snip . - ``` diff --git a/main.go b/main.go index e277817..9dd0a4f 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,11 @@ import ( "github.com/spf13/cobra" ) -const Version = "" // fill-in at compile time. +var ( + Version = "" // fill-in at compile time. + Commit = "" // fill-in at compile time. + Date = "" // fill-in at compile time. +) var completionCmd = &cobra.Command{ Use: "completion [bash|zsh|fish|powershell]", @@ -66,7 +70,7 @@ var editorCmd = &cobra.Command{ var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version", - Run: func(*cobra.Command, []string) { fmt.Println(DefaultStr(Version, "unknown")) }, + Run: CmdPrintVersion, } func init() { @@ -208,3 +212,9 @@ func CmdSync(_ *cobra.Command, args []string) error { func CmdOpenEditor(_ *cobra.Command, _ []string) error { return Command(Cfg.Editor, Cfg.Dir).Run() } + +func CmdPrintVersion(*cobra.Command, []string) { + fmt.Println("Version: ", DefaultStr(Version, "unknown")) + fmt.Println("Commit: ", DefaultStr(Commit, "unknown")) + fmt.Println("Release Date: ", DefaultStr(Date, "unknown")) +}