Build, test, upload artifact and release crate #199
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Pipeline | |
run-name: Build, test, upload artifact and release crate | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
env: | |
CARGO_TERM_COLOR: always | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
outputs: | |
release-id: ${{ steps.create-release.outputs.release_id }} | |
release-flag: ${{ steps.set-release-flag.outputs.release_flag }} | |
pkg-name: ${{ steps.get-package-info.outputs.pkg_name }} | |
bin-name: ${{ steps.get-package-info.outputs.bin_name }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Get Package Name | |
id: get-package-info | |
run: | | |
pkg_name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name') | |
echo "Package Name: $pkg_name" | |
echo "pkg_name=$pkg_name" >> $GITHUB_ENV | |
echo "pkg_name=$pkg_name" >> "$GITHUB_OUTPUT" | |
bin_name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].targets[] | select(.kind[] == "bin" or .crate_types[] == "bin") | .name') | |
echo "Bin Name: $bin_name" | |
echo "bin_name=$bin_name" >> $GITHUB_ENV | |
echo "bin_name=$bin_name" >> "$GITHUB_OUTPUT" | |
shell: bash | |
- name: Set Release Flag # Release flag is set only for a push on main branch | |
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' | |
id: set-release-flag | |
run: | | |
current_version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version') | |
latest_version=$(curl -s https://crates.io/api/v1/crates/${{ env.pkg_name }} | jq -r '.versions[0].num') | |
echo "Current Package Version: ${current_version}" | |
echo "Latest Package Version: ${latest_version}" | |
if [ "$latest_version" != "$current_version" ]; then | |
echo "Version has changed. Setting release flag to true." | |
echo "release=true" >> $GITHUB_ENV | |
echo "release_flag=true" >> "$GITHUB_OUTPUT" | |
else | |
echo "Version has not changed. Setting release flag to false." | |
echo "release=false" >> $GITHUB_ENV | |
echo "release_flag=false" >> "$GITHUB_OUTPUT" | |
fi | |
echo "pkg_version=$current_version" >> $GITHUB_ENV | |
shell: bash | |
- name: Create New Release | |
id: create-release | |
run: | | |
if [ "$env.release" == "true" ]; then | |
release_tag="v${{ env.pkg_version }}" | |
cargo_prerelease=("alpha" "beta" "rc") | |
prerelease=false | |
for cargo_pre in "${cargo_prerelease[@]}"; do | |
if [[ $pkg_version == *"$cargo_pre"* ]]; then | |
prerelease=true | |
break | |
fi | |
done | |
else | |
epoch="$(date +%s)" | |
current_version=${{ env.pkg_version }} | |
part=2 | |
IFS='.' read -r -a version_parts <<< "$current_version" | |
((version_parts[$part]++)) | |
new_version="${version_parts[0]}.${version_parts[1]}.${version_parts[2]}" | |
echo "Bumped version to: $new_version" | |
release_tag="v${new_version}-prerelease-${epoch}" | |
prerelease=true | |
fi | |
commit_msg="Release compiled executable for $release_tag" | |
release_data="{\"tag_name\":\"$release_tag\",\"name\":\"$release_tag\",\"body\":\"$commit_msg\",\"draft\":false,\"prerelease\":$prerelease}" | |
response=$(curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \ | |
-d "$release_data" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases") | |
release_id=$(echo $response | jq -r .id) | |
if [ -z "$release_id" ]; then | |
echo "Error: release_id is null. Exiting with code 1." | |
exit 1 | |
fi | |
echo "Release ID: $release_id" | |
echo "release_id=$release_id" >> "$GITHUB_OUTPUT" | |
shell: bash | |
upload_assets: | |
needs: release | |
strategy: | |
matrix: | |
platform: | |
- release_for: Linux-x86_64 | |
os: ubuntu-20.04 | |
target: x86_64-unknown-linux-gnu | |
bin: ${{ needs.release.outputs.bin-name }} | |
name: ${{ needs.release.outputs.pkg-name }}-Linux-x86_64.tar.gz | |
command: build | |
- release_for: Windows-x86_64 | |
os: windows-latest | |
target: x86_64-pc-windows-msvc | |
bin: ${{ needs.release.outputs.bin-name }}.exe | |
name: ${{ needs.release.outputs.pkg-name }}-Windows-x86_64.zip | |
command: build | |
- release_for: macOS-x86_64 | |
os: macOS-latest | |
target: x86_64-apple-darwin | |
bin: ${{ needs.release.outputs.bin-name }} | |
name: ${{ needs.release.outputs.pkg-name }}-Darwin-x86_64.tar.gz | |
command: build | |
- release_for: RaspberryPi | |
os: ubuntu-20.04 | |
target: arm-unknown-linux-gnueabihf | |
bin: ${{ needs.release.outputs.bin-name }} | |
name: ${{ needs.release.outputs.pkg-name }}-RaspberryPi.tar.gz | |
command: build | |
name: Upload asset for ${{ matrix.platform.release_for }} | |
runs-on: ${{ matrix.platform.os }} | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout Repo | |
uses: actions/checkout@v4 | |
- name: Setup Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: 3.9 | |
- name: Update Rust | |
# print it with style | |
run: | | |
printf '*%.0s' {1..60} && printf "\n" | |
echo "Existing rust version: $(rustc --version)" | |
printf '*%.0s' {1..60} && printf "\n\n" | |
rustup update && printf "\n" | |
printf '*%.0s' {1..60} && printf "\n" | |
echo "Updated rust version: $(rustc --version)" | |
printf '*%.0s' {1..60} && printf "\n" | |
- name: Install OpenSSL static for Windows | |
# https://github.com/sfackler/rust-openssl/issues/1086 | |
if: ${{ matrix.platform.os == 'windows-latest' }} | |
run: | | |
mkdir \Tools | |
cd \Tools | |
git clone https://github.com/Microsoft/vcpkg.git | |
cd vcpkg | |
echo ("vcpkg_dir=" + $pwd) >> $env:GITHUB_ENV | |
.\bootstrap-vcpkg.bat | |
.\vcpkg.exe install openssl:x64-windows-static | |
- name: Build | |
run: | | |
if [ "${{ matrix.platform.os }}" == "windows-latest" ]; then | |
echo "Setting vcpkg env vars for OpenSSL in Windows" | |
export OPENSSL_DIR="${{ env.vcpkg_dir }}\installed\x64-windows-static" | |
export OPENSSL_STATIC="Yes" | |
export VCPKG_ROOT="${{ env.vcpkg_dir }}\installed\x64-windows-static" | |
fi | |
cargo build --release | |
shell: bash | |
- name: Run tests | |
run: | | |
if [ "${{ matrix.platform.os }}" == "windows-latest" ]; then | |
echo "Setting vcpkg env vars for OpenSSL in Windows" | |
export OPENSSL_DIR="${{ env.vcpkg_dir }}\installed\x64-windows-static" | |
export OPENSSL_STATIC="Yes" | |
export VCPKG_ROOT="${{ env.vcpkg_dir }}\installed\x64-windows-static" | |
fi | |
cargo test --no-run | |
shell: bash | |
- name: Copy Artifacts (Windows) | |
if: ${{ matrix.platform.os == 'windows-latest' }} | |
run: | | |
mkdir -p ${{ needs.release.outputs.pkg-name }} | |
cp target/release/${{ matrix.platform.bin }} ${{ needs.release.outputs.pkg-name }}/${{ matrix.platform.bin }} | |
Compress-Archive -DestinationPath ${{ matrix.platform.name }} -Path ${{ needs.release.outputs.pkg-name }}/ | |
- name: Copy Artifacts (macOS/Ubuntu) | |
if: ${{ matrix.platform.os != 'windows-latest' }} | |
run: | | |
mkdir -p ${{ needs.release.outputs.pkg-name }} | |
cp target/release/${{ matrix.platform.bin }} ${{ needs.release.outputs.pkg-name }}/${{ matrix.platform.bin }} | |
tar -zcvf ${{ matrix.platform.name }} ${{ needs.release.outputs.pkg-name }}/ | |
- name: Upload Asset to Release | |
run: | | |
curl -X POST -H "Authorization: token ${{ secrets.GIT_TOKEN }}" \ | |
-H "Content-Type: application/octet-stream" \ | |
--data-binary @"${{ matrix.platform.name }}" \ | |
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ needs.release.outputs.release-id }}/assets?name=${{ matrix.platform.name }}" | |
shell: bash | |
publish-crate: | |
needs: upload_assets | |
if: needs.release.outputs.release-flag == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Update Rust | |
run: | | |
printf '*%.0s' {1..60} && printf "\n" | |
echo "Existing rust version: $(rustc --version)" | |
printf '*%.0s' {1..60} && printf "\n\n" | |
rustup update && printf "\n" | |
printf '*%.0s' {1..60} && printf "\n" | |
echo "Updated rust version: $(rustc --version)" | |
printf '*%.0s' {1..60} && printf "\n" | |
- name: Release Crate | |
run: | | |
cargo login ${{ secrets.CRATES_TOKEN }} | |
cargo publish --allow-dirty # Set allow-dirty since building will create a /target folder that will be uncommitted in git | |
shell: bash |