From e1e27e4d0511ac613f66fb71b84cd135cf55e4c5 Mon Sep 17 00:00:00 2001 From: Edgar Gomes Date: Mon, 8 Jul 2024 20:39:56 -0300 Subject: [PATCH] feat: publish cli binaries and add install script (#22) --- .github/workflows/ci.yml | 30 +++++++++++++++-- .github/workflows/release-please.yml | 32 ++++++++++++++++++ flake.nix | 25 ++++++++++++++ scripts/install_cli.sh | 50 ++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 scripts/install_cli.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8357f3e..d5670efd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,8 +42,8 @@ jobs: run: | nix develop --command test -z $(gofmt -l .) - build_and_test_nix: - name: Test and build + build_publish_images: + name: Test, build and publish images needs: check_nix runs-on: ubuntu-22.04 steps: @@ -83,3 +83,29 @@ jobs: nix run ./#publish-kardinal-manager-container nix run ./#publish-kardinal-cli-container nix run ./#publish-redis-proxy-overlay-container + + build_clis: + name: Test and build cross-compiled clis + needs: check_nix + runs-on: ubuntu-22.04 + strategy: + matrix: + os: [linux, darwin, windows] + arch: [amd64, arm64] + + steps: + - name: git checkout + uses: actions/checkout@v3 + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@main + + - name: Magic cache + uses: DeterminateSystems/magic-nix-cache-action@main + + - name: Build cli + run: | + path=$(nix build ./#cross-compiled-cli.x86_64-linux.${{ matrix.os }}.${{ matrix.arch }} --no-link --print-out-paths) + binout="/tmp/kardinal-${{ matrix.os }}-${{ matrix.arch }}" + ln -s $path/bin/${{ matrix.os }}_${{ matrix.arch }}/kardinal.cli $binout + ls -lah $binout diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index e7e936fd..b8a11d1a 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -13,9 +13,13 @@ jobs: release-please: runs-on: ubuntu-latest # skip releases on forks + outputs: + tag_name: ${{ steps.release.outputs.tag_name }} + release_created: ${{ steps.release.outputs.release_created }} if: github.repository == 'kurtosis-tech/kardinal' steps: - name: Run Release Please + id: release uses: googleapis/release-please-action@v3 with: token: ${{ secrets.RELEASE_PLEASE_TOKEN }} @@ -24,3 +28,31 @@ jobs: bump-minor-pre-major: true bump-patch-for-minor-pre-major: true include-v-in-tag: false + + build-and-publish-clis: + needs: release-please + runs-on: ubuntu-22.04 + if: ${{ needs.release-please.outputs.release_created }} + strategy: + matrix: + os: [linux, darwin, windows] + arch: [amd64, arm64] + + steps: + - name: git checkout + uses: actions/checkout@v3 + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@main + + - name: Magic cache + uses: DeterminateSystems/magic-nix-cache-action@main + + - name: Upload Release CLI Artifact + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + path=$(nix build ./#cross-compiled-cli.x86_64-linux.${{ matrix.os }}.${{ matrix.arch }} --no-link --print-out-paths) + binout="/tmp/kardinal-${{ matrix.os }}-${{ matrix.arch }}" + ln -s $path/bin/${{ matrix.os }}_${{ matrix.arch }}/kardinal.cli $binout + gh release upload ${{ needs.release-please.outputs.tag_name }} $binout diff --git a/flake.nix b/flake.nix index 43c2dfb1..30b3c482 100644 --- a/flake.nix +++ b/flake.nix @@ -181,6 +181,31 @@ in pkgs.lib.foldl' (set: acc: pkgs.lib.recursiveUpdate acc set) {} all; + + cross-compiled-cli = let + all = + pkgs.lib.mapCartesianProduct ({ + arch, + os, + }: { + "${os}" = { + "${toString arch}" = packages.kardinal-cli.overrideAttrs (old: + old + // { + GOOS = os; + GOARCH = arch; + # CGO_ENABLED = disabled breaks the CLI compilation + # CGO_ENABLED = 0; + doCheck = false; + }); + }; + }) { + arch = architectures; + os = ["linux" "darwin" "windows"]; + }; + in + pkgs.lib.foldl' (set: acc: pkgs.lib.recursiveUpdate acc set) {} + all; }; # Add containers matching architecture with local system as toplevel packages # this means calling `nix build .#-container` will build the container matching the local system. diff --git a/scripts/install_cli.sh b/scripts/install_cli.sh new file mode 100644 index 00000000..9a13721e --- /dev/null +++ b/scripts/install_cli.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +REPO="kurtosis-tech/kardinal" +BINARY_NAME="kardinal" + +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) + +if [[ "$ARCH" == "x86_64" ]]; then + ARCH="amd64" +elif [[ "$ARCH" == "arm64" ]]; then + ARCH="arm64" +elif [[ "$ARCH" == "aarch64" ]]; then + ARCH="arm64" +fi + +LATEST_RELEASE=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + +DOWNLOAD_URL="https://github.com/$REPO/releases/download/$LATEST_RELEASE/${BINARY_NAME}-${OS}-${ARCH}" +curl -L $DOWNLOAD_URL -o /tmp/${BINARY_NAME} + +BIN_FOLDER="$HOME/.local/bin" +mkdir -p $BIN_FOLDER + +mv /tmp/$BINARY_NAME $BIN_FOLDER/$BINARY_NAME + +if [ -f $BIN_FOLDER/$BINARY_NAME ]; then + if [ -d /usr/share/bash-completion/completions ]; then + source $($BIN_FOLDER/$BINARY_NAME completion bash) + fi + + if [ -d ~/.zsh/completions ]; then + source $($BIN_FOLDER/$BINARY_NAME completion zsh) + fi + + if [ -d ~/.config/fish/completions ]; then + source $($BIN_FOLDER/$BINARY_NAME completion fish) + fi +fi + +echo "$BINARY_NAME has been installed successfully!" + +if ! command -v $BINARY_NAME &>/dev/null; then + echo "export PATH=\$PATH:$BIN_FOLDER" >>~/.bashrc + source ~/.bashrc + echo "export PATH=\$PATH:$BIN_FOLDER" >>~/.zshrc + source ~/.zshrc + echo "set -gx PATH \$PATH $BIN_FOLDER" >>~/.config/fish/config.fish + source ~/.config/fish/config.fish +fi