diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 68cfce2d..e3395e92 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,23 +4,53 @@ on: release: types: [published] + pull_request: + branches: + - main + push: + branches: + - feat/musl_built_binaries + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: true + jobs: build-release: runs-on: ${{ matrix.operating-system }} strategy: matrix: - operating-system: [ubuntu-latest, windows-latest, macos-latest] + include: + - operating-system: ubuntu-20.04 + targets: x86_64-unknown-linux-gnu,x86_64-unknown-linux-musl,aarch64-unknown-linux-gnu,aarch64-unknown-linux-musl + - operating-system: windows-2019 + targets: aarch64-pc-windows-msvc,x86_64-pc-windows-msvc + - operating-system: macos-12 + targets: aarch64-apple-darwin,x86_64-apple-darwin + fail-fast: false env: pact_do_not_track: true steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install stable Rust toolchain - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@stable with: - profile: minimal toolchain: stable - override: true - target: aarch64-apple-darwin + targets: ${{ matrix.targets }} + + - name: Rust caching + uses: Swatinem/rust-cache@v2 + with: + workspaces: rust + + - name: Set up QEMU + if: runner.os == 'Linux' + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + if: runner.os == 'Linux' + uses: docker/setup-buildx-action@v3 + - name: Install LLVM run: choco install -y llvm if: runner.os == 'Windows' @@ -28,27 +58,36 @@ jobs: uses: arduino/setup-protoc@v1 with: repo-token: ${{ secrets.GITHUB_TOKEN }} - - if: startsWith(github.ref, 'refs/tags/csv-plugin') - run: ./release.sh ${{ runner.os }} + - name: Cargo flags + id: cargo-flags + shell: bash + run: | + if [[ "${{ github.event_name }}" = "release" ]]; then + echo "flags=--release" >> "$GITHUB_OUTPUT" + else + echo "flags=" >> "$GITHUB_OUTPUT" + fi + - if: | + github.event_name == 'push' || + github.event_name == 'pull_request' || + startsWith(github.ref, 'refs/tags/csv-plugin') + run: ./release.sh ${{ runner.os }} ${{ steps.cargo-flags.outputs.flags }} shell: bash working-directory: plugins/csv - - name: Upload Release Assets - if: startsWith(github.ref, 'refs/tags/csv-plugin') - uses: svenstaro/upload-release-action@v2 - with: - repo_token: ${{ secrets.GITHUB_TOKEN }} - file: plugins/csv/target/artifacts/* - file_glob: true - tag: ${{ github.ref }} - - if: startsWith(github.ref, 'refs/tags/pact-plugin-cli') - run: ./release.sh ${{ runner.os }} + - if: | + github.event_name == 'push' || + github.event_name == 'pull_request' || + startsWith(github.ref, 'refs/tags/pact-plugin-cli') + run: ./release.sh ${{ runner.os }} ${{ steps.cargo-flags.outputs.flags }} shell: bash working-directory: cli - name: Upload Release Assets - if: startsWith(github.ref, 'refs/tags/pact-plugin-cli') + if: | + startsWith(github.ref, 'refs/tags/pact-plugin-cli') || + startsWith(github.ref, 'refs/tags/csv-plugin') uses: svenstaro/upload-release-action@v2 with: repo_token: ${{ secrets.GITHUB_TOKEN }} - file: cli/target/artifacts/* + file: release_artifacts/* file_glob: true tag: ${{ github.ref }} diff --git a/.gitignore b/.gitignore index 681ea9b5..200e300e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,8 @@ build target pacts *.out +# Generated Release Artifacts +release_artifacts # Misc diff --git a/cli/release.sh b/cli/release.sh index affd10af..9cef5eba 100755 --- a/cli/release.sh +++ b/cli/release.sh @@ -1,5 +1,15 @@ #!/bin/bash +set -e +set -x + +RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd )" + +source "$RUST_DIR/scripts/gzip-and-sum.sh" +ARTIFACTS_DIR=${ARTIFACTS_DIR:-"$RUST_DIR/release_artifacts"} +mkdir -p "$ARTIFACTS_DIR" +export CARGO_TARGET_DIR=${CARO_TARGET_DIR:-"$RUST_DIR/target"} + if [ $# -lt 1 ] then echo "Usage : $0 " @@ -7,41 +17,107 @@ then fi echo Building Release for "$1" +APP=pact-plugin-cli +OS=$1 +shift; +# All flags passed to this script are passed to cargo. +cargo_flags=( "$@" ) +install_cross() { + cargo install cross@0.2.5 +} + +build_linux_x86_64() { + install_cross + cargo clean + cross build --target=x86_64-unknown-linux-musl "${cargo_flags[@]}" + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-linux-x86_64.gz" + + fi +} + +build_linux_aarch64() { + install_cross + cargo clean + cross build --target=aarch64-unknown-linux-musl "${cargo_flags[@]}" + + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-linux-aarch64.gz" + fi +} +# Build the x86_64 darwin release +build_macos_x86_64() { + #cargo clean + cargo build --target x86_64-apple-darwin "${cargo_flags[@]}" + + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-osx-x86_64.gz" + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-macos-x86_64.gz" + fi +} + +# Build the aarch64 darwin release +build_macos_aarch64() { + #cargo clean + cargo build --target aarch64-apple-darwin "${cargo_flags[@]}" + + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-osx-aarch64.gz" + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-macos-aarch64.gz" + fi +} + +# Build the x86_64 windows release +build_windows_x86_64() { + #cargo clean + cargo build --target x86_64-pc-windows-msvc "${cargo_flags[@]}" + + # If --release in cargo flags, then gzip and sum the release artifacts + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/${APP}.exe" \ + "$ARTIFACTS_DIR/${APP}-windows-x86_64.exe.gz" + fi +} + +# Build the aarch64 windows release +build_windows_aarch64() { + #cargo clean + cargo build --target aarch64-pc-windows-msvc "${cargo_flags[@]}" -cargo clean -mkdir -p target/artifacts/ + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/${APP}.exe" \ + "$ARTIFACTS_DIR/${APP}-windows-aarch64.exe.gz" + fi +} -case "$1" in +case "$OS" in Linux) echo "Building for Linux" - docker run --rm --user "$(id -u)":"$(id -g)" -v "$(pwd):/workspace" -w /workspace -t pactfoundation/rust-musl-build -c 'cargo build --release' - gzip -c target/release/pact-plugin-cli > target/artifacts/pact-plugin-cli-linux-x86_64.gz - openssl dgst -sha256 -r target/artifacts/pact-plugin-cli-linux-x86_64.gz > target/artifacts/pact-plugin-cli-linux-x86_64.gz.sha256 - - # Build aarch64 - cargo install cross - cross build --target aarch64-unknown-linux-gnu --release - gzip -c target/aarch64-unknown-linux-gnu/release/pact-plugin-cli > target/artifacts/pact-plugin-cli-linux-aarch64.gz - openssl dgst -sha256 -r target/artifacts/pact-plugin-cli-linux-aarch64.gz > target/artifacts/pact-plugin-cli-linux-aarch64.gz.sha256 + build_linux_x86_64 + build_linux_aarch64 ;; - Windows) echo "Building for Windows" - cargo build --release - gzip -c target/release/pact-plugin-cli.exe > target/artifacts/pact-plugin-cli-windows-x86_64.exe.gz - openssl dgst -sha256 -r target/artifacts/pact-plugin-cli-windows-x86_64.exe.gz > target/artifacts/pact-plugin-cli-windows-x86_64.exe.gz.sha256 + Windows) echo "Building for windows" + build_windows_x86_64 + build_windows_aarch64 ;; - macOS) echo "Building for OSX" - cargo build --release - gzip -c target/release/pact-plugin-cli > target/artifacts/pact-plugin-cli-osx-x86_64.gz - openssl dgst -sha256 -r target/artifacts/pact-plugin-cli-osx-x86_64.gz > target/artifacts/pact-plugin-cli-osx-x86_64.gz.sha256 - - # M1 - export SDKROOT=$(xcrun -sdk macosx11.1 --show-sdk-path) - export MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx11.1 --show-sdk-platform-version) - cargo build --target aarch64-apple-darwin --release - - gzip -c target/aarch64-apple-darwin/release/pact-plugin-cli > target/artifacts/pact-plugin-cli-osx-aarch64.gz - openssl dgst -sha256 -r target/artifacts/pact-plugin-cli-osx-aarch64.gz > target/artifacts/pact-plugin-cli-osx-aarch64.gz.sha256 + macOS) echo "Building for macos" + build_macos_x86_64 + build_macos_aarch64 ;; - *) echo "$1 is not a recognised OS" + *) echo "$OS is not a recognised OS" exit 1 ;; -esac +esac \ No newline at end of file diff --git a/plugins/csv/Cross.toml b/plugins/csv/Cross.toml index 99e26e99..5874bc7b 100644 --- a/plugins/csv/Cross.toml +++ b/plugins/csv/Cross.toml @@ -1,4 +1,10 @@ -[target.aarch64-unknown-linux-gnu] +[target.x86_64-unknown-linux-musl] +pre-build=[ + "apt-get update && apt-get install --assume-yes wget unzip", + "wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip", + "unzip protoc-21.5-linux-x86_64.zip -d /usr/local/" +] +[target.aarch64-unknown-linux-musl] pre-build=[ "dpkg --add-architecture arm64 && apt-get update && apt-get install --assume-yes wget unzip", "wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip", diff --git a/plugins/csv/release.sh b/plugins/csv/release.sh index 7c81524c..0ee8232a 100755 --- a/plugins/csv/release.sh +++ b/plugins/csv/release.sh @@ -1,46 +1,123 @@ #!/bin/bash +set -e +set -x + +RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd )" + +source "$RUST_DIR/scripts/gzip-and-sum.sh" +ARTIFACTS_DIR=${ARTIFACTS_DIR:-"$RUST_DIR/release_artifacts"} +mkdir -p "$ARTIFACTS_DIR" +export CARGO_TARGET_DIR=${CARO_TARGET_DIR:-"$RUST_DIR/target"} + if [ $# -lt 1 ] then - echo "Usage : $0 " + echo "Usage : $0 " exit fi -echo Building Release for "$1" +APP=pact-csv-plugin +OS=$1 +shift; +echo Building Release for "$OS" +# All flags passed to this script are passed to cargo. +cargo_flags=( "$@" ) +install_cross() { + cargo install cross@0.2.5 +} + +build_linux_x86_64() { + install_cross + cargo clean + cross build --target=x86_64-unknown-linux-musl "${cargo_flags[@]}" + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-linux-x86_64.gz" + + fi +} + +build_linux_aarch64() { + install_cross + cargo clean + cross build --target=aarch64-unknown-linux-musl "${cargo_flags[@]}" + + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-linux-aarch64.gz" + fi +} +# Build the x86_64 darwin release +build_macos_x86_64() { + #cargo clean + cargo build --target x86_64-apple-darwin "${cargo_flags[@]}" + + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-osx-x86_64.gz" + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-macos-x86_64.gz" + fi +} + +# Build the aarch64 darwin release +build_macos_aarch64() { + #cargo clean + cargo build --target aarch64-apple-darwin "${cargo_flags[@]}" + + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-osx-aarch64.gz" + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-apple-darwin/release/${APP}" \ + "$ARTIFACTS_DIR/${APP}-macos-aarch64.gz" + fi +} + +# Build the x86_64 windows release +build_windows_x86_64() { + #cargo clean + cargo build --target x86_64-pc-windows-msvc "${cargo_flags[@]}" + + # If --release in cargo flags, then gzip and sum the release artifacts + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/${APP}.exe" \ + "$ARTIFACTS_DIR/${APP}-windows-x86_64.exe.gz" + fi +} + +# Build the aarch64 windows release +build_windows_aarch64() { + #cargo clean + cargo build --target aarch64-pc-windows-msvc "${cargo_flags[@]}" -cargo clean -mkdir -p target/artifacts/ + if [[ "${cargo_flags[*]}" =~ "--release" ]]; then + gzip_and_sum \ + "$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/${APP}.exe" \ + "$ARTIFACTS_DIR/${APP}-windows-aarch64.exe.gz" + fi +} -case "$1" in +case "$OS" in Linux) echo "Building for Linux" - docker run --rm --user "$(id -u)":"$(id -g)" -v "$(pwd):/workspace" -w /workspace -t pactfoundation/rust-musl-build -c 'cargo build --release' - gzip -c target/release/pact-csv-plugin > target/artifacts/pact-csv-plugin-linux-x86_64.gz - openssl dgst -sha256 -r target/artifacts/pact-csv-plugin-linux-x86_64.gz > target/artifacts/pact-csv-plugin-linux-x86_64.gz.sha256 - cp pact-plugin.json target/artifacts/ - cargo install cross - cross build --target aarch64-unknown-linux-gnu --release - gzip -c target/aarch64-unknown-linux-gnu/release/pact-csv-plugin > target/artifacts/pact-csv-plugin-linux-aarch64.gz - openssl dgst -sha256 -r target/artifacts/pact-csv-plugin-linux-aarch64.gz > target/artifacts/pact-csv-plugin-linux-aarch64.gz.sha256 + build_linux_x86_64 + build_linux_aarch64 ;; - Windows) echo "Building for Windows" - cargo build --release - gzip -c target/release/pact-csv-plugin.exe > target/artifacts/pact-csv-plugin-windows-x86_64.exe.gz - openssl dgst -sha256 -r target/artifacts/pact-csv-plugin-windows-x86_64.exe.gz > target/artifacts/pact-csv-plugin-windows-x86_64.exe.gz.sha256 + Windows) echo "Building for windows" + build_windows_x86_64 + build_windows_aarch64 ;; - macOS) echo "Building for OSX" - cargo build --release - gzip -c target/release/pact-csv-plugin > target/artifacts/pact-csv-plugin-osx-x86_64.gz - openssl dgst -sha256 -r target/artifacts/pact-csv-plugin-osx-x86_64.gz > target/artifacts/pact-csv-plugin-osx-x86_64.gz.sha256 - - # M1 - export SDKROOT=$(xcrun -sdk macosx11.1 --show-sdk-path) - export MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx11.1 --show-sdk-platform-version) - cargo build --target aarch64-apple-darwin --release - - gzip -c target/aarch64-apple-darwin/release/pact-csv-plugin > target/artifacts/pact-csv-plugin-osx-aarch64.gz - openssl dgst -sha256 -r target/artifacts/pact-csv-plugin-osx-aarch64.gz > target/artifacts/pact-csv-plugin-osx-aarch64.gz.sha256 + macOS) echo "Building for macos" + build_macos_x86_64 + build_macos_aarch64 ;; - *) echo "$1 is not a recognised OS" + *) echo "$OS is not a recognised OS" exit 1 ;; -esac +esac \ No newline at end of file diff --git a/scripts/gzip-and-sum.sh b/scripts/gzip-and-sum.sh new file mode 100644 index 00000000..42f1f1d8 --- /dev/null +++ b/scripts/gzip-and-sum.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Gzip and sum a file. +# +# Usage: gzip_and_sum [] +# +# - orig_file: the file to gzip and sum +# - target_file: the file to write the gzipped file to +# - digest_file: the file to write the digest to. If not provided, defaults to +# .sha256 +gzip_and_sum() { + orig_file=$1 + target_file=$2 + digest_file=${3:-$target_file.sha256} + + gzip --stdout --best "$orig_file" > "$target_file" + openssl dgst -sha256 -r "$target_file" > "$digest_file" +}