From 7e9bfd6fd394c419b81255d38979b53f4de9b9da Mon Sep 17 00:00:00 2001 From: alt-art Date: Wed, 15 Dec 2021 19:35:52 -0300 Subject: [PATCH 1/2] chore(CI): Setup gh-actions to CI --- .github/workflows/ci.yml | 37 +++++++++++ .github/workflows/release.yml | 46 ++++++++++++++ .github/workflows/upload_asset.sh | 102 ++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/upload_asset.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e228c77 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,37 @@ +name: CI + +on: + [push, pull_request] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + strategy: + matrix: + os: [windows-latest, ubuntu-latest] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + components: clippy + override: true + - uses: Swatinem/rust-cache@v1 + - name: Run cargo fmt + uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + - name: Run tests + run: cargo test --verbose + - name: Annotate commit with clippy warnings + uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: --all-features \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..3f43a9e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,46 @@ +name: Release + +on: + push: + tags: ["[0-9]+.[0-9]+.[0-9]+*"] + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CARGO_TERM_COLOR: always + +jobs: + linux: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Install cargo-deb + run: cargo install cargo-deb + - name: Test + run: cargo test --release + - name: Build + run: cargo deb --verbose + - name: Upload .deb package + run: | + cp ./target/debian/*.deb ./commit_${GITHUB_REF##*/}_amd64.deb + ./.github/workflows/upload_asset.sh \ + "alt-art/commit" ./commit_${GITHUB_REF##*/}_amd64.deb $GITHUB_TOKEN + + windows: + runs-on: windows-latest + + defaults: + run: + shell: bash + + steps: + - uses: actions/checkout@v2 + - name: Test + run: cargo test --release + - name: Build + run: cargo build --release + - name: Upload portable executable + run: | + cp ./target/release/commit.exe ./commit-${GITHUB_REF##*/}-portable.exe + ./.github/workflows/upload_asset.sh \ + "alt-art/commit" ./commit-${GITHUB_REF##*/}-portable.exe $GITHUB_TOKEN diff --git a/.github/workflows/upload_asset.sh b/.github/workflows/upload_asset.sh new file mode 100644 index 0000000..ad6fabf --- /dev/null +++ b/.github/workflows/upload_asset.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# +# Author: Christian Duerr +# License: Apache-2.0 +# https://github.com/alacritty/alacritty/blob/master/.github/workflows/upload_asset.sh +# +# +# This script accepts the following parameters: +# +# * "owner/repo" +# * file_path +# * token +# +# Script to upload a release asset +# +# Example: +# +# upload_asset.sh "alt-art/lyrs" ./build.zip $GITHUB_TOKEN + +repo=$1 +file_path=$2 +file_name=${file_path##*/} +token=$3 + +echo "Starting asset upload from $file_name to $repo." +# Define variables. +TAG="$(git describe --tags --abbrev=0)" +if [ -z "$TAG" ]; then + printf "\e[31mError: Unable to find git tag\e[0m\n" + exit 1 +fi +echo "Git tag: $TAG" +GH_API="https://api.github.com" +GH_REPO="$GH_API/repos/$repo" +AUTH="Authorization: Bearer $token" + +# Validate token. +curl -o /dev/null -sH "$AUTH" "$GH_REPO" || { echo "Error: Invalid repo, token or network issue!"; exit 1; } + +echo "Checking for existing release..." +upload_url=$(\ + curl \ + -H "$AUTH" \ + "$GH_REPO/releases" \ + 2> /dev/null \ + | grep -E "(upload_url|tag_name)" \ + | paste - - \ + | grep -e "tag_name\": \"$TAG\"" \ + | head -n 1 \ + | sed 's/.*\(https.*assets\).*/\1/' \ +) + +# Create a new release if we didn't find one for this tag. +if [ -z "$upload_url" ]; then + echo "No release found." + echo "Creating new release..." + + # Create new release. + response=$( + curl -f \ + -X POST \ + -H "$AUTH" \ + -d "{\"tag_name\":\"$TAG\",\"draft\":true}" \ + "$GH_REPO/releases" \ + 2> /dev/null\ + ) + + # Abort if the release could not be created. + if [ $? -ne 0 ]; then + printf "\e[31mError: Unable to create new release.\e[0m\n" + exit 1; + fi + + # Extract upload URL from new release. + upload_url=$(\ + echo "$response" \ + | grep "upload_url" \ + | sed 's/.*: "\(.*\){.*/\1/' \ + ) +fi + +if [ -z "$upload_url" ]; then + printf "\e[31mError: Unable to find release upload url.\e[0m\n" + exit 2 +fi + +# Upload asset + +echo "Uploading asset $file_name to $TAG..." +curl -f \ + -X POST \ + -H "$AUTH" \ + -H "Content-Type: application/octet-stream" \ + --data-binary @"$file_path" \ + "$upload_url?name=$file_name" \ + &> /dev/null \ +|| { \ + printf "\e[31mError: Unable to upload asset.\e[0m\n" \ + && exit 3; \ +} + +printf "\e[32mSuccess\e[0m\n" \ No newline at end of file From 9592ef2eb5d284996fd78dca76832ef82087b959 Mon Sep 17 00:00:00 2001 From: alt-art Date: Wed, 15 Dec 2021 19:52:27 -0300 Subject: [PATCH 2/2] chore(CI): Add new targets in release --- .github/workflows/release.yml | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3f43a9e..301e685 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,15 +16,33 @@ jobs: - uses: actions/checkout@v2 - name: Install cargo-deb run: cargo install cargo-deb + - name: Install cargo-generate-rpm + run: cargo install cargo-generate-rpm + - name: Install cargo-appimage + run: cargo install cargo-appimage - name: Test run: cargo test --release - - name: Build + - name: Build .deb run: cargo deb --verbose - name: Upload .deb package run: | cp ./target/debian/*.deb ./commit_${GITHUB_REF##*/}_amd64.deb ./.github/workflows/upload_asset.sh \ "alt-art/commit" ./commit_${GITHUB_REF##*/}_amd64.deb $GITHUB_TOKEN + - name: Build .rpm + run: cargo generate-rpm --verbose + - name: Upload .rpm package + run: | + cp ./target/generate-rpm/rpm/*.rpm ./commit_${GITHUB_REF##*/}_x86_64.rpm + ./.github/workflows/upload_asset.sh \ + "alt-art/commit" ./commit_${GITHUB_REF##*/}_x86_64.rpm $GITHUB_TOKEN + - name: Build .AppImage + run: cargo appimage --verbose + - name: Upload .AppImage + run: | + cp ./*.AppImage ./commit_${GITHUB_REF##*/}_x86_64.AppImage + ./.github/workflows/upload_asset.sh \ + "alt-art/commit" ./commit_${GITHUB_REF##*/}_x86_64.AppImage $GITHUB_TOKEN windows: runs-on: windows-latest