Skip to content

Commit

Permalink
Reapply "chore(ci): refactor release pipeline"
Browse files Browse the repository at this point in the history
This reverts commit 7c09f17.
  • Loading branch information
rholshausen committed Dec 20, 2024
1 parent 2d87431 commit 823b009
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 261 deletions.
158 changes: 108 additions & 50 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@ name: Release workflow

on:
release:
types: [published]
types:
- published

pull_request:
branches:
- master

workflow_dispatch:

schedule:
- cron: 0 0 * * 4 # Midnight Wednesday

concurrency:
group: release-${{ github.ref }}
cancel-in-progress: true

env:
RUST_BACKTRACE: "1"
Expand All @@ -16,75 +30,119 @@ jobs:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-20.04, windows-latest, macos-13]
env:
pact_do_not_track: true
include:
- operating-system: ubuntu-20.04
targets: x86_64-unknown-linux-gnu
- operating-system: ubuntu-20.04
targets: x86_64-unknown-linux-musl
- operating-system: ubuntu-20.04
targets: aarch64-unknown-linux-gnu
- operating-system: ubuntu-20.04
targets: aarch64-unknown-linux-musl
- operating-system: windows-2019
targets: x86_64-pc-windows-msvc
- operating-system: windows-2019
targets: aarch64-pc-windows-msvc
- operating-system: macos-13
targets: x86_64-apple-darwin
- operating-system: macos-14
targets: aarch64-apple-darwin
fail-fast: false

steps:
- uses: actions/checkout@v4

- name: Install stable Rust toolchain
if: runner.os == 'Linux'
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-unknown-linux-gnu,x86_64-unknown-linux-musl
- name: Install stable Rust toolchain
if: runner.os == 'Windows'
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-pc-windows-msvc,x86_64-pc-windows-msvc
- name: Install stable Rust toolchain
if: runner.os == 'MacOS'
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-apple-darwin,x86_64-apple-darwin
- uses: maxim-lobanov/setup-xcode@v1
targets: ${{ matrix.targets }}

- name: Rust caching
uses: Swatinem/rust-cache@v2
with:
xcode-version: 12.4
if: runner.os == 'MacOS'
workspaces: rust
shared-key: ${{ runner.os }}

- name: Set up QEMU
if: runner.os == 'Linux'
if: runner.os == 'Linux' && startsWith(matrix.targets, 'aarch64')
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
if: runner.os == 'Linux'
uses: docker/setup-buildx-action@v3
- if: startsWith(github.ref, 'refs/tags/pact_mock_server_cli') && runner.os == 'Linux'
run: ./release-linux.sh
working-directory: rust/pact_mock_server_cli
- if: startsWith(github.ref, 'refs/tags/pact_mock_server_cli') && runner.os == 'Windows'
run: ./release-win.sh
shell: bash
working-directory: rust/pact_mock_server_cli
- if: startsWith(github.ref, 'refs/tags/pact_mock_server_cli') && runner.os == 'macOS'
run: ./release-osx.sh

- name: Platform abbreviation
id: platform-abbreviation
shell: bash
working-directory: rust/pact_mock_server_cli
- if: startsWith(github.ref, 'refs/tags/pact_verifier_cli') && runner.os == 'Linux'
run: ./release-linux.sh
working-directory: rust/pact_verifier_cli
- if: startsWith(github.ref, 'refs/tags/pact_verifier_cli') && runner.os == 'Windows'
run: ./release-win.sh
run: |
if [[ "${{ runner.os }}" = "Linux" ]]; then
echo "platform=linux" >> "$GITHUB_OUTPUT"
elif [[ "${{ runner.os }}" = "Windows" ]]; then
echo "platform=win" >> "$GITHUB_OUTPUT"
elif [[ "${{ runner.os }}" = "macOS" ]]; then
echo "platform=macos" >> "$GITHUB_OUTPUT"
else
echo "Unknown platform"
exit 1
fi
- name: Cargo flags
id: cargo-flags
shell: bash
working-directory: rust/pact_verifier_cli
- if: startsWith(github.ref, 'refs/tags/pact_verifier_cli') && runner.os == 'macOS'
run: ./release-osx.sh
run: |
echo "flags=--release" >> "$GITHUB_OUTPUT"
- name: Build verifier CLI
if: |
github.event_name == 'push' ||
github.event_name == 'pull_request' ||
github.event_name == 'workflow_dispatch' ||
startsWith(github.ref, 'refs/tags/pact_verifier_cli')
shell: bash
run: |
./release-${{ steps.platform-abbreviation.outputs.platform }}.sh ${{ matrix.targets }} \
${{ steps.cargo-flags.outputs.flags }}
working-directory: rust/pact_verifier_cli
- if: startsWith(github.ref, 'refs/tags/libpact_ffi') && runner.os == 'Linux'
run: ./release-linux.sh
working-directory: rust/pact_ffi
- if: startsWith(github.ref, 'refs/tags/libpact_ffi') && runner.os == 'Windows'
run: ./release-win.sh
shell: bash
working-directory: rust/pact_ffi
- if: startsWith(github.ref, 'refs/tags/libpact_ffi') && runner.os == 'macOS'
run: ./release-osx.sh

- name: Build FFI library
if: |
github.event_name == 'push' ||
github.event_name == 'pull_request' ||
github.event_name == 'workflow_dispatch' ||
startsWith(github.ref, 'refs/tags/libpact_ffi')
shell: bash
run: |
./release-${{ steps.platform-abbreviation.outputs.platform }}.sh ${{ matrix.targets }} \
${{ steps.cargo-flags.outputs.flags }}
working-directory: rust/pact_ffi

- name: Upload the artifacts
if: |
startsWith(github.ref, 'refs/tags/libpact_ffi') ||
startsWith(github.ref, 'refs/tags/pact_verifier_cli')
uses: actions/upload-artifact@v4
with:
name: release-artifacts-${{ matrix.targets }}
path: rust/release_artifacts
if-no-files-found: warn

publish:
runs-on: ubuntu-latest
if: |
startsWith(github.ref, 'refs/tags/libpact_ffi') ||
startsWith(github.ref, 'refs/tags/pact_verifier_cli')
needs: build-release

steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
pattern: release-artifacts-*
path: rust/release_artifacts
merge-multiple: true

- name: Upload Release Assets
if: startsWith(github.ref, 'refs/tags/libpact_ffi') || startsWith(github.ref, 'refs/tags/pact_verifier_cli') || startsWith(github.ref, 'refs/tags/pact_mock_server_cli')
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
170 changes: 107 additions & 63 deletions rust/pact_ffi/release-linux.sh
Original file line number Diff line number Diff line change
@@ -1,75 +1,119 @@
#!/bin/bash -x
#!/bin/bash

set -e
set -x

echo -- Setup directories --
cargo clean
mkdir -p ../release_artifacts
RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
APP_NAME=libpact_ffi

echo -- Build the Docker build image --
docker build -f Dockerfile.linux-build -t pact-ffi-build .
source "$RUST_DIR/scripts/gzip-and-sum.sh"
ARTIFACTS_DIR=${ARTIFACTS_DIR:-"$RUST_DIR/release_artifacts"}
mkdir -p "$ARTIFACTS_DIR"
install_cross() {
cargo install [email protected] --force
}
install_cross_latest() {
cargo install cross --git https://github.com/cross-rs/cross --force
}
clean_cargo_release_build() {
rm -rf $CARGO_TARGET_DIR/release/build
}

echo -- Build the release artifacts --
docker run -t --rm --user "$(id -u)":"$(id -g)" -v $(pwd)/..:/workspace -w /workspace/pact_ffi pact-ffi-build -c 'cargo build --release'
gzip -c ../target/release/libpact_ffi.so > ../release_artifacts/libpact_ffi-linux-x86_64.so.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-x86_64.so.gz > ../release_artifacts/libpact_ffi-linux-x86_64.so.gz.sha256
gzip -c ../target/release/libpact_ffi.a > ../release_artifacts/libpact_ffi-linux-x86_64.a.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-x86_64.a.gz > ../release_artifacts/libpact_ffi-linux-x86_64.a.gz.sha256
export CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-"$RUST_DIR/target"}

echo -- Generate the header files --
rustup toolchain install nightly
rustup component add rustfmt --toolchain nightly
rustup run nightly cbindgen \
--config cbindgen.toml \
--crate pact_ffi \
--output include/pact.h
rustup run nightly cbindgen \
--config cbindgen-c++.toml \
--crate pact_ffi \
--output include/pact-cpp.h
cp include/*.h ../release_artifacts
# All flags passed to this script are passed to cargo.
case $1 in
x86_64-unknown-linux-musl)
TARGET=$1
shift
;;
aarch64-unknown-linux-musl)
TARGET=$1
shift
;;
x86_64-unknown-linux-gnu)
TARGET=$1
shift
;;
aarch64-unknown-linux-gnu)
TARGET=$1
shift
;;
*) ;;
esac
cargo_flags=("$@")

echo -- Build the musl release artifacts --
cargo install [email protected]
cross build --release --target=x86_64-unknown-linux-musl
gzip -c ../target/x86_64-unknown-linux-musl/release/libpact_ffi.a > ../release_artifacts/libpact_ffi-linux-x86_64-musl.a.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-x86_64-musl.a.gz > ../release_artifacts/libpact_ffi-linux-x86_64-musl.a.gz.sha256
build_target() {
TARGET=$1

mkdir tmp
cp ../target/x86_64-unknown-linux-musl/release/libpact_ffi.a tmp/
docker run --platform=linux/amd64 --rm -v $PWD/tmp:/scratch alpine /bin/sh -c 'apk add --no-cache musl-dev gcc && \
cd /scratch && \
ar -x libpact_ffi.a && \
gcc -shared *.o -o libpact_ffi.so && \
rm -f *.o'
case $TARGET in
x86_64-unknown-linux-musl)
FILE_SUFFIX=linux-x86_64-musl
RUSTFLAGS="-C target-feature=-crt-static"
;;
aarch64-unknown-linux-musl)
FILE_SUFFIX=linux-aarch64-musl
RUSTFLAGS="-C target-feature=-crt-static"
;;
x86_64-unknown-linux-gnu)
FILE_SUFFIX=linux-x86_64
;;
aarch64-unknown-linux-gnu)
FILE_SUFFIX=linux-aarch64
;;
*)
echo unknown target $TARGET
exit 1
;;
esac
RUSTFLAGS=${RUSTFLAGS:-""} cross build --target $TARGET "${cargo_flags[@]}"

gzip -c tmp/libpact_ffi.so > ../release_artifacts/libpact_ffi-linux-x86_64-musl.so.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-x86_64-musl.so.gz > ../release_artifacts/libpact_ffi-linux-x86_64-musl.so.gz.sha256
rm -rf tmp
if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
file "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.a"
file "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.so"
du -sh "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.a"
du -sh "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.so"
gzip_and_sum \
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.a" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.a.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.so" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.so.gz"
fi
}

build_header() {
rustup toolchain install nightly
rustup run nightly cbindgen \
--config cbindgen.toml \
--crate pact_ffi \
--output "$ARTIFACTS_DIR/pact.h"
rustup run nightly cbindgen \
--config cbindgen-c++.toml \
--crate pact_ffi \
--output "$ARTIFACTS_DIR/pact-cpp.h"
}

echo -- Build the musl aarch64 release artifacts --
cargo clean
cross build --release --target=aarch64-unknown-linux-musl
gzip -c ../target/aarch64-unknown-linux-musl/release/libpact_ffi.a > ../release_artifacts/libpact_ffi-linux-aarch64-musl.a.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-aarch64-musl.a.gz > ../release_artifacts/libpact_ffi-linux-aarch64-musl.a.gz.sha256
install_cross
if [ ! -z "$TARGET" ]; then
echo building for target $TARGET
build_target $TARGET

mkdir tmp
cp ../target/aarch64-unknown-linux-musl/release/libpact_ffi.a tmp/
docker run --platform=linux/arm64 --rm -v $PWD/tmp:/scratch alpine /bin/sh -c 'apk add --no-cache musl-dev gcc && \
cd /scratch && \
ar -x libpact_ffi.a && \
gcc -shared *.o -o libpact_ffi.so && \
rm -f *.o'

gzip -c tmp/libpact_ffi.so > ../release_artifacts/libpact_ffi-linux-aarch64-musl.so.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-aarch64-musl.so.gz > ../release_artifacts/libpact_ffi-linux-aarch64-musl.so.gz.sha256
rm -rf tmp

echo -- Build the aarch64 release artifacts --
cargo clean
cross build --target aarch64-unknown-linux-gnu --release
gzip -c ../target/aarch64-unknown-linux-gnu/release/libpact_ffi.so > ../release_artifacts/libpact_ffi-linux-aarch64.so.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-aarch64.so.gz > ../release_artifacts/libpact_ffi-linux-aarch64.so.gz.sha256
gzip -c ../target/aarch64-unknown-linux-gnu/release/libpact_ffi.a > ../release_artifacts/libpact_ffi-linux-aarch64.a.gz
openssl dgst -sha256 -r ../release_artifacts/libpact_ffi-linux-aarch64.a.gz > ../release_artifacts/libpact_ffi-linux-aarch64.a.gz.sha256
# If we are building indiv targets, ensure we build the headers
# for at least 1 nominated target
if [ "$TARGET" == "x86_64-unknown-linux-gnu" ]; then
build_header
fi
else
echo building for all targets
# clean release build to avoid conflicting symbols when building all targets
clean_cargo_release_build
build_target x86_64-unknown-linux-gnu
clean_cargo_release_build
build_target aarch64-unknown-linux-gnu
clean_cargo_release_build
build_target x86_64-unknown-linux-musl
clean_cargo_release_build
build_target aarch64-unknown-linux-musl
build_header
fi
19 changes: 0 additions & 19 deletions rust/pact_ffi/release-osx.sh

This file was deleted.

Loading

0 comments on commit 823b009

Please sign in to comment.