Skip to content

Commit

Permalink
Merge pull request #3 from alt-art/github-actions
Browse files Browse the repository at this point in the history
GitHub actions to release packages and check for errors
  • Loading branch information
newtoallofthis123 committed Oct 11, 2023
2 parents 98f1c55 + ce212c6 commit c14af34
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --verbose
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: Swatinem/rust-cache@v2
- name: Run cargo fmt
run: cargo fmt --all -- --check
- name: Run tests
run: cargo test --verbose
18 changes: 18 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Clippy

on: pull_request

env:
CARGO_TERM_COLOR: always

jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: rustup component add clippy
- uses: Swatinem/rust-cache@v2
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features
58 changes: 58 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Release

on:
push:
tags: ["[0-9]+.[0-9]+.[0-9]+*"]
workflow_dispatch:

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_TERM_COLOR: always

jobs:
linux:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: Swatinem/rust-cache@v1
- name: Install cargo-deb
run: cargo install cargo-deb
- name: Install cargo-generate-rpm
run: cargo install cargo-generate-rpm
- name: Test
run: cargo test --release
- name: Publish to crates.io
run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
- name: Build .deb
run: cargo deb -v -o ./lyrics_${GITHUB_REF##*/}_amd64.deb
- name: Upload .deb package
run: |
chmod +x ./.github/workflows/upload_asset.sh
./.github/workflows/upload_asset.sh \
"newtoallofthis123/lyrics_cli" ./lyrics_${GITHUB_REF##*/}_amd64.deb $GITHUB_TOKEN
- name: Build .rpm
run: cargo generate-rpm -o ./lyrics_${GITHUB_REF##*/}_x86_64.rpm
- name: Upload .rpm package
run: |
chmod +x ./.github/workflows/upload_asset.sh
./.github/workflows/upload_asset.sh \
"newtoallofthis123/lyrics_cli" ./lyrics_${GITHUB_REF##*/}_x86_64.rpm $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/lyrics.exe ./lyrics-${GITHUB_REF##*/}-portable.exe
./.github/workflows/upload_asset.sh \
"newtoallofthis123/lyrics_cli" ./lyrics-${GITHUB_REF##*/}-portable.exe $GITHUB_TOKEN
102 changes: 102 additions & 0 deletions .github/workflows/upload_asset.sh
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 25 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ serde_json = {version = "1"}
tokio = {version = "1", features = ["full"]}
human-panic = {version = "1.0"}
crossterm = "0.27.0"
futures = "0.3.28"
futures = "0.3.28"

[package.metadata.deb]
name = "lyrics"
maintainer = "NoobScience <[email protected]>"
copyright = "2023, NoobScience <[email protected]>"
license-file = ["LICENSE", "0"]
extended-description = "A simple CLI to get lyrics of a song from Genius using a public availed API"
depends = "openssl, ca-certificates"
section = "utils"
priority = "optional"
assets = [
# Binary
["target/release/lyrics", "/usr/bin/", "111"],
]

[package.metadata.generate-rpm]
assets = [
# Binary
{ source = "target/release/lyrics", dest = "/usr/bin/lyrics", mode = "111" },
]

[package.metadata.generate-rpm.requires]
openssl = "*"
ca-certificates = "*"
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use human_panic::setup_panic;

mod cli;
mod web;
mod spinner;
mod web;

use spinner::wrap_spinner;

Expand Down

0 comments on commit c14af34

Please sign in to comment.