Skip to content

PROD-3069: Cross-compile self contained binaries #8

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 5 additions & 23 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -14,14 +14,10 @@ on:
tags-ignore:
- '**'

env:
DOCKER_DRIVER: overlay
CARGO_NET_GIT_FETCH_WITH_CLI: true

jobs:
test:
runs-on: ubuntu-latest
container: rust:1.69
container: rust:1.72
steps:
- uses: actions/checkout@v3
- name: Lint
@@ -33,22 +29,8 @@ jobs:
cargo test

build:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v3
- name: Setup
run: |
./scripts/build-setup
- name: Build
run: |
./scripts/build ${{ inputs.arch }}
- name: Package
run: |
./scripts/package
- name: Archive
uses: actions/upload-artifact@v3
with:
name: package
retention-days: 1
path: dist/*.tar.gz
uses: ./.github/workflows/wc_build_binaries.yml
with:
arch: ${{ inputs.arch }}
artifact_retention: 1
44 changes: 19 additions & 25 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: Release
on:
push:
tags:
- "v*"
# Workflow dispatch should be used only as fallback method
# Remember to select a Git tag otherwise the workflow will fail
workflow_dispatch:
inputs:
arch:
@@ -20,37 +17,33 @@ env:
CARGO_NET_GIT_FETCH_WITH_CLI: true

jobs:
build:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check version
run: |
VERSION_IN_CARGO=$(find . -type f -name 'Cargo.toml' -exec grep -oP '^\W*version\W*\K[0-9\.]+' {} \; | sort -nru | head -1)
VERSION_IN_CARGO=$(find . -type f -name 'Cargo.toml' -exec grep -oP '^\W*version\W*\K[0-9\.\-rc]+' {} \; | sort -nru | head -1)
GIT_TAG='${{ github.ref_name }}'

if [ "${GIT_TAG/#v/}" != "${VERSION_IN_CARGO}" ]; then
echo 'git tag and Cargo.toml version mismatch'
echo "${GIT_TAG/#v/} != ${VERSION_IN_CARGO}"
if [[ "${GIT_TAG/#v/}" != "${VERSION_IN_CARGO}" ]]; then
echo 'Version mismatch between git tag and Cargo.toml'
echo "git: ${GIT_TAG/#v/}"
echo "cargo: ${VERSION_IN_CARGO}"
echo ''
echo 'REMINDER: Git tags must start with "v"'
echo 'REMINDERS:'
echo ' - This workflow must be triggered from a Git tag'
echo ' - Git tags must start with "v"'
exit 1
fi
- name: Setup
run: |
./scripts/build-setup
- name: Build
run: |
./scripts/build ${{ inputs.arch }}
- name: Package
run: |
./scripts/package
- name: Archive
uses: actions/upload-artifact@v3
with:
name: package
retention-days: 7
path: dist/*.tar.gz
shell: bash

build:
needs: check
uses: ./.github/workflows/wc_build_binaries.yml
with:
arch: ${{ inputs.arch }}
artifact_retention: 7

release:
runs-on: ubuntu-latest
@@ -64,5 +57,6 @@ jobs:
- name: Publish release
uses: softprops/action-gh-release@v1
with:
prerelease: true
files: |
dist/*.tar.gz
54 changes: 54 additions & 0 deletions .github/workflows/wc_build_binaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: build binaries
on:
workflow_call:
inputs:
arch:
required: false
type: string
default: amd64
artifact_retention:
required: false
type: number
default: 1

env:
DOCKER_DRIVER: overlay
CARGO_NET_GIT_FETCH_WITH_CLI: true
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER: aarch64-linux-gnu-gcc

jobs:
binaries:
runs-on: ubuntu-latest
container: rust:1.72
steps:
- uses: actions/checkout@v3
- name: Setup
run: |
rustup target add x86_64-unknown-linux-musl
rustup target add aarch64-unknown-linux-musl

apt-get update
apt-get install -y musl-tools musl-dev

if [ '${{ contains(inputs.arch, 'arm64') }}' = 'true' ]; then
apt-get install -y gcc-aarch64-linux-gnu
fi

- name: Build & package amd64
if: ${{ contains(inputs.arch, 'amd64') }}
run: |
cargo build --release --target x86_64-unknown-linux-musl
./scripts/package ./target/x86_64-unknown-linux-musl/release

- name: Build & package arm64
if: ${{ contains(inputs.arch, 'arm64') }}
run: |
cargo build --release --target aarch64-unknown-linux-musl
./scripts/package ./target/aarch64-unknown-linux-musl/release

- name: Archive
uses: actions/upload-artifact@v3
with:
name: package
retention-days: ${{ inputs.artifact_retention }}
path: dist/*.tar.gz
30 changes: 18 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Contributing

Thanks for taking the time to contribute to SDK Bash!
Thanks for taking the time to contribute to SDK Bash! 🎉🎉🎉

This Bash SDK is a group of binaries enabling users to interact with the CTO.ai
The Bash SDK is a group of binaries enabling users to interact with the CTO.ai
API within their workflows. These binaries are written in Rust, so using `cargo`
for the development cycle should suffice; however, we would like to share some
details to help you with your contribution.
@@ -35,13 +35,19 @@ Rust libraries. Required parameters are provided to the initial
constructor and optional parameters through method calls on the
builder struct.

## Build and release process

The Bash SDK binaries are automatically built and packaged as `tar.gz` by the
GitHub Workflows within this repository, on every pushed commit. The build logic is
distributed across the `scripts/build*` scripts, which make use of `docker buildx` to
cross compile the binaries. However, for local development, using `cargo build` should suffice.

When the master branch is tagged with a new version (which must match the
version in the root `Cargo.toml` configuration), a new release is built. The
resulting artifacts will be published through the **Releases** of this repo.
## Build and release processes

The Bash SDK binaries are cross-compiled for `x86_64` (AKA `amd64`) and `arm64`
(e.g.: Apple Silicon M1) chipset architectures, making use of the
[musl](https://musl.libc.org/) C standard library to get self-contained
artifacts. The build logic around said artifacts lies on the GitHub workflows
within this repository; however, for contributions running `cargo clippy`,
`cargo test`, and `cargo build --release` locally is more than okay to test out
new changes.

A new release binary is cut when the `release` workflow is triggered from a
pre-existing git tag, which must match the version in the root `Cargo.toml`
file. When said pipeline successfully finishes, the resulting artifacts will be
published as a pre-release in the
[Releases](https://github.com/cto-ai/sdk-bash/releases) section. The maintainers
will manually remove the pre-release check when needed.
Loading