diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ee810f5..2ab271f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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] diff --git a/.github/workflows/setup-action.yml b/.github/workflows/setup-action.yml new file mode 100644 index 00000000..2eef95f8 --- /dev/null +++ b/.github/workflows/setup-action.yml @@ -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 diff --git a/README.md b/README.md index 1cc24df1..a8c7a690 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/setup-action/README.md b/setup-action/README.md new file mode 100644 index 00000000..6d803889 --- /dev/null +++ b/setup-action/README.md @@ -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' +``` diff --git a/setup-action/action.yml b/setup-action/action.yml new file mode 100644 index 00000000..816ff698 --- /dev/null +++ b/setup-action/action.yml @@ -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 diff --git a/setup-action/scripts/setup.bash b/setup-action/scripts/setup.bash new file mode 100755 index 00000000..2187318c --- /dev/null +++ b/setup-action/scripts/setup.bash @@ -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] ---"