From e4bf70a99739284cb44ad811dc49890a4ba63d4a Mon Sep 17 00:00:00 2001 From: Michael Hertig <3533648+hertg@users.noreply.github.com> Date: Sat, 17 Sep 2022 22:06:17 +0000 Subject: [PATCH] Add Continuous Integration (#89) --- .ci/generate-deb.sh | 5 + .ci/generate-pkgbuild.sh | 43 +++++++++ .github/workflows/aur-release.yml | 72 ++++++++++++++ .github/workflows/ci.yml | 44 +-------- .github/workflows/github-release.yml | 31 ++++++ .github/workflows/go-build.yml | 52 ++++++++++ .github/workflows/go-test.yml | 45 +++++++++ .github/workflows/release.yml | 94 +++++++++++-------- .gitignore | 3 +- .pkg/aur/egpu-switcher-bin/PKGBUILD.template | 16 ++++ .../egpu-switcher-bin.install | 15 +++ .pkg/aur/egpu-switcher/PKGBUILD.template | 26 +++++ .pkg/aur/egpu-switcher/egpu-switcher.install | 15 +++ Makefile | 2 +- 14 files changed, 383 insertions(+), 80 deletions(-) create mode 100755 .ci/generate-deb.sh create mode 100755 .ci/generate-pkgbuild.sh create mode 100644 .github/workflows/aur-release.yml create mode 100644 .github/workflows/github-release.yml create mode 100644 .github/workflows/go-build.yml create mode 100644 .github/workflows/go-test.yml create mode 100644 .pkg/aur/egpu-switcher-bin/PKGBUILD.template create mode 100644 .pkg/aur/egpu-switcher-bin/egpu-switcher-bin.install create mode 100644 .pkg/aur/egpu-switcher/PKGBUILD.template create mode 100644 .pkg/aur/egpu-switcher/egpu-switcher.install diff --git a/.ci/generate-deb.sh b/.ci/generate-deb.sh new file mode 100755 index 0000000..d74d770 --- /dev/null +++ b/.ci/generate-deb.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +# mkdir -p .debpkg/usr/bin +# mkdir -p .debpkg/usr/lib/egpu-switcher + diff --git a/.ci/generate-pkgbuild.sh b/.ci/generate-pkgbuild.sh new file mode 100755 index 0000000..86b6e99 --- /dev/null +++ b/.ci/generate-pkgbuild.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +version=$1 +checksum=$2 + +#### egpu-switcher #### + +mkdir -p ./.pkgbuild/egpu-switcher +cp ./.pkg/aur/egpu-switcher/* ./.pkgbuild/egpu-switcher + +cat << EOF > ./.pkgbuild/egpu-switcher/PKGBUILD +# Maintainer: hertg +# This file is generated automatically +_version=$version +_versionWithoutPrefix=${version#v} +_pkgname=egpu-switcher +_pkgver=$(echo $version | sed 's/\([^-]*-g\)/r\1/;s/-/./g') +_source=\${_pkgname}-\${_version}::https://github.com/hertg/egpu-switcher/archive/refs/tags/$version.tar.gz +EOF + +cat ./.pkgbuild/egpu-switcher/PKGBUILD.template >> ./.pkgbuild/egpu-switcher/PKGBUILD +rm ./.pkgbuild/egpu-switcher/PKGBUILD.template + + +#### egpu-switcher-bin #### + +mkdir -p ./.pkgbuild/egpu-switcher-bin +cp ./.pkg/aur/egpu-switcher-bin/* ./.pkgbuild/egpu-switcher-bin + +cat << EOF > ./.pkgbuild/egpu-switcher-bin/PKGBUILD +# Maintainer: hertg +# This file is generated automatically +_version=$version +_pkgname=egpu-switcher-bin +_pkgver=$(echo $version | sed 's/\([^-]*-g\)/r\1/;s/-/./g') +_sha256sum=$checksum +_source=\${_pkgname}-\${_pkgver}::https://github.com/hertg/egpu-switcher/releases/download/$version/egpu-switcher-amd64 +EOF + +cat ./.pkgbuild/egpu-switcher-bin/PKGBUILD.template >> ./.pkgbuild/egpu-switcher-bin/PKGBUILD +rm ./.pkgbuild/egpu-switcher-bin/PKGBUILD.template + + diff --git a/.github/workflows/aur-release.yml b/.github/workflows/aur-release.yml new file mode 100644 index 0000000..9e5e7b5 --- /dev/null +++ b/.github/workflows/aur-release.yml @@ -0,0 +1,72 @@ +# reusable workflow +name: Publish to AUR + +on: + workflow_call: + inputs: + version: + required: true + type: string + secrets: + AUR_USERNAME: + required: true + AUR_EMAIL: + required: true + AUR_PRIVATE_KEY: + required: true + +jobs: + pkgbuild: + runs-on: ubuntu-latest + name: Generate AUR PKGBUILD + steps: + + - name: Checkout + uses: actions/checkout@v2 + + - name: Download binary + uses: actions/download-artifact@v3 + with: + name: egpu-switcher-amd64 + path: ./download + + - name: Export Checksum + run: echo "SHA256=$(cat ./download/sha256sum.txt | cut -d' ' -f1)" >> $GITHUB_ENV + + - name: Generate PKGBUILDs + run: ./.ci/generate-pkgbuild.sh ${{ inputs.version }} ${{ env.SHA256 }} + + - name: Upload egpu-switcher PKGBUILD + uses: actions/upload-artifact@v3 + with: + name: egpu-switcher-PKGBUILD + path: ./.pkgbuild/egpu-switcher/* + + - name: Upload egpu-switcher-bin PKGBUILD + uses: actions/upload-artifact@v3 + with: + name: egpu-switcher-bin-PKGBUILD + path: ./.pkgbuild/egpu-switcher-bin/* + + - name: Publish egpu-switcher + uses: KSXGitHub/github-actions-deploy-aur@v2.5.0 + with: + pkgname: egpu-switcher + pkgbuild: ./.pkgbuild/egpu-switcher/PKGBUILD + force_push: true + commit_username: ${{ secrets.AUR_USERNAME }} + commit_email: ${{ secrets.AUR_EMAIL }} + ssh_private_key: ${{ secrets.AUR_PRIVATE_KEY }} + commit_message: New release via github actions + + - name: Publish egpu-switcher-bin + uses: KSXGitHub/github-actions-deploy-aur@v2.5.0 + with: + pkgname: egpu-switcher-bin + pkgbuild: ./.pkgbuild/egpu-switcher-bin/PKGBUILD + force_push: true + commit_username: ${{ secrets.AUR_USERNAME }} + commit_email: ${{ secrets.AUR_EMAIL }} + ssh_private_key: ${{ secrets.AUR_PRIVATE_KEY }} + commit_message: New release via github actions + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9672336..bdcea50 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,48 +6,14 @@ on: - '*' tags-ignore: - '*' + jobs: test: - runs-on: ubuntu-latest + name: Test strategy: matrix: go: [ '1.19' ] - name: Test (Go ${{ matrix.go }}) - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - - name: Run linters - run: make lint - - - name: Run tests - run: make test - - build: - needs: test - runs-on: ubuntu-latest - strategy: - matrix: - go: [ '1.19' ] - goosarch: - - linux/amd64 - - linux/arm64 - name: Build (Go ${{ matrix.go }} / Arch ${{ matrix.goosarch }}) - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} + uses: ./.github/workflows/go-test.yml + with: + go-version: ${{ matrix.go }} - - name: Run build - run: make build diff --git a/.github/workflows/github-release.yml b/.github/workflows/github-release.yml new file mode 100644 index 0000000..f004c77 --- /dev/null +++ b/.github/workflows/github-release.yml @@ -0,0 +1,31 @@ +# reusable workflow +name: Create Github Release + +on: + workflow_call: + inputs: + binary: + type: string + required: true + +jobs: + release: + runs-on: ubuntu-latest + name: Release ${{ inputs.binary }} + steps: + + - name: Checkout + uses: actions/checkout@v2 + + - uses: actions/download-artifact@v3 + with: + name: ${{ inputs.binary }} + path: ./download + + - name: Create Github Release + uses: softprops/action-gh-release@v1 + with: + draft: false + files: download/${{ inputs.binary }} + prerelease: ${{ contains(github.ref, '-rc.') }} + diff --git a/.github/workflows/go-build.yml b/.github/workflows/go-build.yml new file mode 100644 index 0000000..b04f1da --- /dev/null +++ b/.github/workflows/go-build.yml @@ -0,0 +1,52 @@ +# reusable workflow +name: Build + +on: + workflow_call: + inputs: + go-version: + required: true + type: string + go-arch: + required: true + type: string + filename: + required: true + type: string + +jobs: + build: + runs-on: ubuntu-latest + name: Run build (Go ${{ inputs.go-version }}) + env: + GOARCH: ${{ inputs.go-arch }} + BINARY_NAME: ${{ format('{0}-{1}', inputs.filename, inputs.go-arch) }} + ORIGIN: gh + steps: + + - name: Checkout + uses: actions/checkout@v2 + with: + # without that 'git describe --tags' may result + # in 'fatal: No names found, cannot describe anything.' + # see https://stackoverflow.com/a/71721059/2726733 + fetch-depth: 0 + + - name: Setup go + uses: actions/setup-go@v3 + with: + go-version: ${{ inputs.go-version }} + cache: true + + - name: Run build + run: make build + + - name: Create SHA256 Checksum + run: sha256sum ./bin/${{ env.BINARY_NAME }} > ./bin/sha256sum.txt || exit $? + + - name: Upload binary + uses: actions/upload-artifact@v3 + with: + name: ${{ env.BINARY_NAME }} + path: ./bin/* + diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 0000000..bf2f100 --- /dev/null +++ b/.github/workflows/go-test.yml @@ -0,0 +1,45 @@ +# reusable workflow +name: Test + +on: + workflow_call: + inputs: + go-version: + required: true + type: string + +jobs: + test: + runs-on: ubuntu-latest + name: Run tests (Go ${{ inputs.go-version }}) + steps: + + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup go + uses: actions/setup-go@v3 + with: + go-version: ${{ inputs.go-version }} + cache: true + + - name: Run tests + run: make test + + lint: + runs-on: ubuntu-latest + name: Run linters (Go ${{ inputs.go-version }}) + steps: + + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup go + uses: actions/setup-go@v3 + with: + go-version: ${{ inputs.go-version }} + cache: true + + - name: Run linters + run: make lint + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 25b5f48..04a7dbc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,50 +1,66 @@ name: Release + on: push: tags: - - 'v*' + - '*' + jobs: - release: + + test: + name: Test + strategy: + matrix: + go: [ '1.19' ] + uses: ./.github/workflows/go-test.yml + with: + go-version: ${{ matrix.go }} + + build: + name: Build + needs: test strategy: matrix: go: [ '1.19' ] - goosarch: - - linux/amd64 - - linux/arm64 - env: - FILENAME: egpu-switcher - ORIGIN: gh + arch: [ 'amd64', 'arm64' ] + uses: ./.github/workflows/go-build.yml + with: + go-version: ${{ matrix.go }} + go-arch: ${{ matrix.arch }} + filename: egpu-switcher + + version: + name: Extract Version + if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest - name: Build and Publish Binaries (Go ${{ matrix.go }} / Arch ${{ matrix.goosarch }}) + outputs: + tag: ${{ steps.vars.outputs.tag }} steps: + - id: vars + run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} + + release: + name: Github Release + needs: [build, version] + strategy: + matrix: + binary: [ 'egpu-switcher-amd64', 'egpu-switcher-arm64' ] + uses: ./.github/workflows/github-release.yml + with: + binary: ${{ matrix.binary }} + + aur: + name: Publish to AUR + needs: [build, version, release] + uses: ./.github/workflows/aur-release.yml + with: + version: ${{ needs.version.outputs.tag }} + secrets: + AUR_EMAIL: ${{ secrets.AUR_EMAIL }} + AUR_USERNAME: ${{ secrets.AUR_USERNAME }} + AUR_PRIVATE_KEY: ${{ secrets.AUR_PRIVATE_KEY }} + + # TODO: Debian package + # package-deb: + # needs: build - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - # the following steps only run on tags - # credits: https://stackoverflow.com/a/66959862 - - name: Get OS and arch info - if: startsWith(github.ref, 'refs/tags/') - run: | - GOOSARCH=${{matrix.goosarch}} - GOARCH=${GOOSARCH#*/} - BINARY_NAME=${{env.FILENAME}}-$GOARCH - echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV - echo "GOARCH=$GOARCH" >> $GITHUB_ENV - - - name: Run build - if: startsWith(github.ref, 'refs/tags/') - run: make build - - - name: Create Github Release - uses: softprops/action-gh-release@v1 - if: startsWith(github.ref, 'refs/tags/') - with: - draft: false - files: bin/${{env.BINARY_NAME}} - prerelease: ${{ contains(github.ref, '-rc.') }} diff --git a/.gitignore b/.gitignore index 93c020c..760b25d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ +/.pkgbuild /tmp /egpu-switcher /bin -/docs \ No newline at end of file +/docs diff --git a/.pkg/aur/egpu-switcher-bin/PKGBUILD.template b/.pkg/aur/egpu-switcher-bin/PKGBUILD.template new file mode 100644 index 0000000..8551e29 --- /dev/null +++ b/.pkg/aur/egpu-switcher-bin/PKGBUILD.template @@ -0,0 +1,16 @@ +pkgname=$_pkgname +pkgver=$_pkgver +pkgdesc='Automatically detect and use eGPU on startup' +pkgrel=1 +arch=(x86_64) +license=('GPL') +url='https://github.com/hertg/egpu-switcher' +provides=(egpu-switcher) +conflicts=(egpu-switcher) +install=${pkgname}.install +source=($_source) +sha256sums=($_sha256sum) + +package() { + install -Dm755 "${pkgname}-${pkgver}" "$pkgdir/usr/bin/egpu-switcher" +} diff --git a/.pkg/aur/egpu-switcher-bin/egpu-switcher-bin.install b/.pkg/aur/egpu-switcher-bin/egpu-switcher-bin.install new file mode 100644 index 0000000..2cb293c --- /dev/null +++ b/.pkg/aur/egpu-switcher-bin/egpu-switcher-bin.install @@ -0,0 +1,15 @@ +pre_remove() { + egpu-switcher disable +} + +post_install() { + egpu-switcher enable --no-prompt +} + +pre_upgrade() { + egpu-switcher cleanup +} + +post_upgrade() { + egpu-switcher enable --no-prompt +} diff --git a/.pkg/aur/egpu-switcher/PKGBUILD.template b/.pkg/aur/egpu-switcher/PKGBUILD.template new file mode 100644 index 0000000..3e301e2 --- /dev/null +++ b/.pkg/aur/egpu-switcher/PKGBUILD.template @@ -0,0 +1,26 @@ +pkgname=$_pkgname +pkgver=$_pkgver +pkgdesc='Automatically detect and use eGPU on startup' +pkgrel=1 +arch=(x86_64) +license=('GPL') +url='https://github.com/hertg/egpu-switcher' +makedepends=('go') +provides=(egpu-switcher) +conflicts=(egpu-switcher) +install=${pkgname}.install +source=($_source) +sha256sums=('SKIP') + +# NOTE: github will strip the 'v' prefix from the tag when +# creating the source tarball, see https://stackoverflow.com/a/5796902 + +build() { + cd "$srcdir/$pkgname-$_versionWithoutPrefix" + make VERSION="$_version" ORIGIN="aur" build -s +} + +package() { + cd "$srcdir/$pkgname-$_versionWithoutPrefix" + make DESTDIR="$pkgdir/" install -s +} diff --git a/.pkg/aur/egpu-switcher/egpu-switcher.install b/.pkg/aur/egpu-switcher/egpu-switcher.install new file mode 100644 index 0000000..2cb293c --- /dev/null +++ b/.pkg/aur/egpu-switcher/egpu-switcher.install @@ -0,0 +1,15 @@ +pre_remove() { + egpu-switcher disable +} + +post_install() { + egpu-switcher enable --no-prompt +} + +pre_upgrade() { + egpu-switcher cleanup +} + +post_upgrade() { + egpu-switcher enable --no-prompt +} diff --git a/Makefile b/Makefile index 76d6bf2..446a5e3 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ OUT_DIR := ./bin BINARY_NAME ?= egpu-switcher OUT_BIN := ${OUT_DIR}/${BINARY_NAME} -VERSION := $(shell git describe --tags) +VERSION ?= $(shell git describe --tags) DATE := $(shell date -u +%Y%m%d.%H%M%S) ORIGIN ?= make