diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67844743..f0472492 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,26 +11,41 @@ # limitations under the License. name: Build + on: push: branches: - - master + - main pull_request: + +# Cancel redundant runs on the same ref (e.g. rapid pushes to a PR branch). +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +# Minimal read-only permissions; CI does not publish anything. +permissions: + contents: read + jobs: ci: runs-on: ubuntu-latest + timeout-minutes: 20 steps: - - name: Checkout - uses: actions/checkout@master - - name: Install Go - uses: actions/setup-go@v5 - with: - go-version-file: go.mod - cache: true - - name: Run make check - run: | - export PATH=${PATH}:`go env GOPATH`/bin - go version - make check - - name: Run unit tests - run: go test ./... + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Lint + uses: golangci/golangci-lint-action@v9 + with: + version: latest + args: --timeout=10m --modules-download-mode=vendor + + - name: Test + run: go test ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0d5c9923..0b53ae9b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,77 +1,58 @@ -name: releaser +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Release on: release: types: - published + +# Only allow one release workflow to run at a time; never cancel an in-flight +# publish since that could leave a partial release. +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +# Minimum permissions at workflow level; the release job declares what it needs. +permissions: + contents: read + jobs: - update_kswitch_in_homebrew_tap: + release: + name: Release environment: Release runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: + # Required to upload release assets + contents: write steps: - - uses: actions/checkout@v2 - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: '^1.21.0' - - name: Build the binary-files - id: build_binary_files - run: | - sudo apt-get update - sudo apt-get install make -y - export PATH=${PATH}:`go env GOPATH`/bin - make check - make build-switcher - echo ::set-output name=latest_release_filtered_tag::${GITHUB_REF##*/} - - name: Upload mac binaries to release - uses: AButler/upload-release-assets@v2.0 - with: - files: 'hack/switch/switcher_darwin_amd64' - repo-token: ${{ secrets.GITHUB_TOKEN }} - release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }} - - name: Upload mac (ARM) binaries to release - uses: AButler/upload-release-assets@v2.0 + - name: Checkout + uses: actions/checkout@v4 with: - files: 'hack/switch/switcher_darwin_arm64' - repo-token: ${{ secrets.GITHUB_TOKEN }} - release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }} - - name: Upload Linux binaries to release - uses: AButler/upload-release-assets@v2.0 - with: - files: 'hack/switch/switcher_linux_amd64' - repo-token: ${{ secrets.GITHUB_TOKEN }} - release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }} - - name: Upload Linux (ARM) binaries to release - uses: AButler/upload-release-assets@v2.0 - with: - files: 'hack/switch/switcher_linux_arm64' - repo-token: ${{ secrets.GITHUB_TOKEN }} - release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }} - - name: Upload Windows binaries to release - uses: AButler/upload-release-assets@v2.0 + # goreleaser needs the full tag history to generate changelogs + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v5 with: - files: 'hack/switch/switcher_windows_amd64.exe' - repo-token: ${{ secrets.GITHUB_TOKEN }} - release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }} - - name: Upload switch script to release - uses: AButler/upload-release-assets@v2.0 + go-version-file: go.mod + cache: true + + - name: Run goreleaser + uses: goreleaser/goreleaser-action@v7 with: - files: 'hack/switch/switch.sh' - repo-token: ${{ secrets.GITHUB_TOKEN }} - release-tag: ${{ steps.build_binary_files.outputs.latest_release_filtered_tag }} - - name: Send update with latest versions to MichaelSp/homebrew-switch + version: latest + args: release --clean env: - ACCESS_TOKEN: ${{ secrets.KSWITCH_ACCESS_TOKEN }} - run: | - mac_sha256sum=$(sha256sum hack/switch/switcher_darwin_amd64 | awk '{print $1}') - mac_arm_sha256sum=$(sha256sum hack/switch/switcher_darwin_arm64 | awk '{print $1}') - linux_sha256sum=$(sha256sum hack/switch/switcher_linux_amd64 | awk '{print $1}') - linux_arm_sha256sum=$(sha256sum hack/switch/switcher_linux_arm64 | awk '{print $1}') - switch_script_sha256sum=$(sha256sum hack/switch/switch.sh | awk '{print $1}') - - echo '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "linux_arm_sha": "'"$linux_arm_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}' - - curl -X POST https://api.github.com/repos/MichaelSp/homebrew-switch/dispatches \ - -H 'Accept: application/vnd.github.everest-preview+json' \ - -u "$ACCESS_TOKEN" \ - --data '{"event_type": "update", "client_payload": { "tag": "'"${{ steps.build_binary_files.outputs.latest_release_filtered_tag }}"'", "mac_sha": "'"$mac_sha256sum"'", "mac_arm_sha": "'"$mac_arm_sha256sum"'", "linux_sha": "'"$linux_sha256sum"'", "linux_arm_sha": "'"$linux_arm_sha256sum"'", "switch_script_sha": "'"$switch_script_sha256sum"'"}}' \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 00000000..dc258a97 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,56 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +version: 2 + +project_name: kswitch + +builds: + - id: switcher + main: ./cmd + binary: switcher + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + - windows + goarch: + - amd64 + - arm64 + ignore: + - goos: windows + goarch: arm64 + ldflags: + - -w + -X github.com/danielfoehrkn/kswitch/cmd/switcher.version={{.Version}} + -X github.com/danielfoehrkn/kswitch/cmd/switcher.buildDate={{.Date}} + +# Upload raw binaries (no archives/tarballs). +# name_template matches the original release asset naming: switcher_{os}_{arch} +# (goreleaser automatically appends .exe for windows) +archives: + - formats: + - binary + name_template: "switcher_{{ .Os }}_{{ .Arch }}" + +checksum: + name_template: checksums.txt + algorithm: sha256 + +release: + # Upload switch.sh alongside the binaries + extra_files: + - glob: hack/switch/switch.sh + +snapshot: + version_template: "{{ .Tag }}-next"