Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zsv/setup-action for GHA workflows #220

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: ci
on:
push:
branches: [main]
paths-ignore: ['**.md']
paths-ignore: ['**.md', 'setup-action/**']
pull_request:
branches: [main]
paths-ignore: ['**.md']
paths-ignore: ['**.md', 'setup-action/**']
release:
types: [published]

Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/setup-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: zsv/setup-action

on:
push:
branches: [main]
paths: ['setup-action/**']
pull_request:
branches: [main]
paths: ['setup-action/**']
workflow_dispatch:

jobs:
ci:
strategy:
matrix:
os: [ubuntu-latest, macos-13, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

defaults:
run:
shell: bash

steps:
- name: Checkout
uses: actions/checkout@v4
with:
sparse-checkout: |
setup-action

- name: Set up
id: setup
uses: ./setup-action

- name: Check output parameter [install-path]
run: echo '${{ steps.setup.outputs.install-path }}'

- name: Check version
run: zsv version
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ $ cat worldcitiespop_mil.csv | docker run -i ghcr.io/liquidaty/zsv count
For image details, see [Dockerfile](./Dockerfile). You may use this as a
baseline for your own use cases as needed.

#### GitHub Actions

In a GitHub Actions workflow, you can use [`zsv/setup-action`](./setup-action)
to set up zsv+zsvlib:

```yml
- name: Set up zsv+zsvlib
uses: liquidaty/zsv/setup-action@main
```

See [zsv/setup-action/README](./setup-action/README.md) for more details.

### From source

See [BUILD.md](BUILD.md) for more details.
Expand Down
45 changes: 45 additions & 0 deletions setup-action/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# zsv/setup-action

[![CI](https://github.com/liquidaty/zsv/actions/workflows/setup-action.yml/badge.svg?branch=main)](https://github.com/liquidaty/zsv/actions/workflows/setup-action.yml)

[GitHub Action](https://docs.github.com/en/actions) to set up zsv+zsvlib.

Supports Linux, macOS, and Windows runners.

## Usage

### Inputs

| Input | Required | Default | Description |
| :-------: | :------: | :------: | :------------------------- |
| `version` | `false` | `latest` | Version/tag of the release |

### Outputs

| Output | Description |
| :------------: | :------------------------------------------ |
| `install-path` | Absolute path of the installation directory |

Under the installation directory, the subdirectories will include:

- `bin`: `zsv`/`zsv.exe` executable
- `include`: header files
- `lib`: `libzsv` library file

### Example

Set up the latest version:

```yml
- name: Set up zsv+zsvlib
uses: liquidaty/zsv/setup-action@main
```

Set up a specific version:

```yml
- name: Set up zsv+zsvlib
uses: liquidaty/zsv/setup-action@main
with:
version: '0.3.9-alpha'
```
28 changes: 28 additions & 0 deletions setup-action/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: zsv/setup-action
description: GitHub Action to set up zsv+zsvlib

branding:
icon: download
color: green

inputs:
version:
description: Version/tag of the release
required: false
default: latest

outputs:
install-path:
description: Absolute path of the installation directory
value: ${{ steps.setup.outputs.install-path }}

runs:
using: composite

steps:
- name: Setup
id: setup
env:
VERSION: '${{ inputs.version }}'
shell: bash
run: $GITHUB_ACTION_PATH/scripts/setup.bash
106 changes: 106 additions & 0 deletions setup-action/scripts/setup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/bash

set -eo pipefail

echo "[INF] Setting up zsv+zsvlib..."

echo "[INF] - RUNNER_ENVIRONMENT: $RUNNER_ENVIRONMENT"
echo "[INF] - RUNNER_OS: $RUNNER_OS"
echo "[INF] - RUNNER_ARCH: $RUNNER_ARCH"
echo "[INF] - VERSION: $VERSION"

# shellcheck disable=SC2207
AVAILABLE_VERSIONS=($(git ls-remote --tags --refs https://github.com/liquidaty/zsv | cut -d '/' -f3 | sort -r | xargs))

TARGET_VERSION=
if [[ $VERSION == "latest" ]]; then
TARGET_VERSION="${AVAILABLE_VERSIONS[0]}"
else
if [[ $VERSION != "v"* ]]; then
TARGET_VERSION="v$VERSION"
fi

echo "[INF] Validating version/tag..."
IS_VALID_VERSION=false
for AV in "${AVAILABLE_VERSIONS[@]}"; do
if [[ $TARGET_VERSION == "$AV" ]]; then
IS_VALID_VERSION=true
break
fi
done
if [[ $IS_VALID_VERSION == false ]]; then
echo "[ERR] Version/tag not found! [$VERSION]"
echo "[ERR] Available versions/tags are:"
for AV in "${AVAILABLE_VERSIONS[@]}"; do
echo "[ERR] - $AV"
done
exit 1
fi
echo "[INF] Validated version/tag successfully!"
fi

TARGET_VERSION="${TARGET_VERSION:1}"

TRIPLET=
if [[ $RUNNER_OS == "Linux" ]]; then
if [[ $RUNNER_ARCH == "X64" ]]; then
TRIPLET="amd64-linux-gcc"
fi
elif [[ $RUNNER_OS == "macOS" ]]; then
if [[ $RUNNER_ARCH == "X64" ]]; then
TRIPLET="amd64-macosx-gcc"
elif [[ $RUNNER_ARCH == "ARM64" ]]; then
TRIPLET="arm64-macosx-gcc"
fi
elif [[ $RUNNER_OS == "Windows" ]]; then
if [[ $RUNNER_ARCH == "X86" || $RUNNER_ARCH == "X64" ]]; then
TRIPLET="amd64-windows-mingw"
fi

if ! which wget >/dev/null; then
echo "[INF] Installing wget..."
if ! choco install wget --no-progress >/dev/null; then
echo "[ERR] Failed to install wget!"
exit 1
fi
fi
fi

if [[ -z $TRIPLET ]]; then
echo "[ERR] Architecture/OS not supported! [$RUNNER_ARCH $RUNNER_OS]"
exit 1
fi

INSTALL_DIR="$RUNNER_TEMP/zsv"
echo "[INF] INSTALL_DIR: $INSTALL_DIR"

rm -rf "${INSTALL_DIR:?}"/{bin,include,lib}
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

ZIP="zsv-$TARGET_VERSION-$TRIPLET.zip"
URL="https://github.com/liquidaty/zsv/releases/download/v$TARGET_VERSION/$ZIP"

echo "[INF] Downloading... [$URL]"
if [[ ! -f $ZIP ]]; then
wget --quiet "$URL"
echo "[INF] Downloaded successfully!"
else
echo "[INF] Archive already exists! Skipping download..."
fi

echo "[INF] Extracting... [$ZIP]"
unzip -q -o "$ZIP"
echo "[INF] Extracted successfully!"

INSTALL_PATH="$(realpath "$INSTALL_DIR")"
echo "[INF] INSTALL_PATH: $INSTALL_PATH"

echo "[INF] Setting PATH... [$INSTALL_PATH]"
echo "$INSTALL_PATH/bin" >>"$GITHUB_PATH"

echo "[INF] Setting output parameter... [install-path]"
echo "install-path=$INSTALL_PATH" >>"$GITHUB_OUTPUT"

echo "[INF] zsv+zsvlib set up successfully!"
echo "[INF] --- [DONE] ---"