Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(release_ci): get inspiration from bat
Browse files Browse the repository at this point in the history
efugier committed Nov 11, 2023
1 parent 1ada9df commit fd3cdfd
Showing 1 changed file with 263 additions and 115 deletions.
378 changes: 263 additions & 115 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ jobs:
outputs:
smartcat_version: ${{ env.SMARTCAT_VERSION }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Get the release version from the tag
shell: bash
if: env.SMARTCAT_VERSION == ''
@@ -43,124 +43,272 @@ jobs:
GH_TOKEN: ${{ github.token }}
run: gh release create ${{ env.SMARTCAT_VERSION }}

build-release:
name: build-release
needs: ['create-release']
runs-on: ${{ matrix.os }}
env:
# For some builds, we use cross to test on 32-bit and big-endian
# systems.
CARGO: cargo
# When CARGO is set to CROSS, this is set to `--target matrix.target`.
TARGET_FLAGS: ""
# When CARGO is set to CROSS, TARGET_DIR includes matrix.target.
TARGET_DIR: ./target
# Emit backtraces on panics.
RUST_BACKTRACE: 1
strategy:
matrix:
build: [linux, linux-arm, macos, win-msvc, win-gnu, win32-msvc]
include:
- build: linux
os: ubuntu-latest
rust: stable
target: x86_64-unknown-linux-musl
- build: linux-arm
os: ubuntu-latest
rust: stable
target: arm-unknown-linux-gnueabihf
- build: macos
os: macos-latest
rust: stable
target: x86_64-apple-darwin
- build: win-msvc
os: windows-latest
rust: stable
target: x86_64-pc-windows-msvc
- build: win-gnu
os: windows-latest
rust: stable-x86_64-gnu
target: x86_64-pc-windows-gnu
- build: win32-msvc
os: windows-latest
rust: stable
target: i686-pc-windows-msvc
# build-release-nope:
# name: build-release
# needs: ['create-release']
# runs-on: ${{ matrix.os }}
# env:
# # For some builds, we use cross to test on 32-bit and big-endian
# # systems.
# CARGO: cargo
# # When CARGO is set to CROSS, this is set to `--target matrix.target`.
# TARGET_FLAGS: ""
# # When CARGO is set to CROSS, TARGET_DIR includes matrix.target.
# TARGET_DIR: ./target
# # Emit backtraces on panics.
# RUST_BACKTRACE: 1
# strategy:
# fail-fast: false
# matrix:
# build: [linux, linux-arm, macos, win-msvc, win-gnu, win32-msvc]
# include:
# - build: linux
# os: ubuntu-latest
# rust: stable
# target: x86_64-unknown-linux-musl
# - build: linux-arm
# os: ubuntu-latest
# rust: stable
# target: arm-unknown-linux-gnueabihf
# - build: macos
# os: macos-latest
# rust: stable
# target: x86_64-apple-darwin
# - build: win-msvc
# os: windows-latest
# rust: stable
# target: x86_64-pc-windows-msvc
# - build: win-gnu
# os: windows-latest
# rust: stable-x86_64-gnu
# target: x86_64-pc-windows-gnu
# - build: win32-msvc
# os: windows-latest
# rust: stable
# target: i686-pc-windows-msvc

steps:
- name: Checkout repository
uses: actions/checkout@v3

# - name: Install packages (Ubuntu)
# if: matrix.os == 'ubuntu-latest'
# run: |
# ci/ubuntu-install-packages

# - name: Install packages (macOS)
# if: matrix.os == 'macos-latest'
# run: |
# ci/macos-install-packages

- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}

- name: Use Cross
shell: bash
run: |
cargo install cross
echo "CARGO=cross" >> $GITHUB_ENV
echo "TARGET_FLAGS=--target ${{ matrix.target }}" >> $GITHUB_ENV
echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV
# steps:
# - name: Checkout repository
# uses: actions/checkout@v4

- name: Show command used for Cargo
run: |
echo "cargo command is: ${{ env.CARGO }}"
echo "target flag is: ${{ env.TARGET_FLAGS }}"
echo "target dir is: ${{ env.TARGET_DIR }}"
# # - name: Install packages (Ubuntu)
# # if: matrix.os == 'ubuntu-latest'
# # run: |
# # ci/ubuntu-install-packages

- name: Build release binary
run: ${{ env.CARGO }} build --verbose --release ${{ env.TARGET_FLAGS }}
# # - name: Install packages (macOS)
# # if: matrix.os == 'macos-latest'
# # run: |
# # ci/macos-install-packages

- name: Strip release binary (linux, macos and macos-arm)
if: matrix.build == 'linux' || matrix.os == 'macos'
run: strip "target/${{ matrix.target }}/release/sc"
# - name: Install Rust
# uses: dtolnay/rust-toolchain@master
# with:
# toolchain: ${{ matrix.rust }}
# target: ${{ matrix.target }}

- name: Strip release binary (arm)
if: matrix.build == 'linux-arm'
run: |
docker run --rm -v \
"$PWD/target:/target:Z" \
rustembedded/cross:arm-unknown-linux-gnueabihf \
arm-linux-gnueabihf-strip \
/target/arm-unknown-linux-gnueabihf/release/sc
- name: Build archive
shell: bash
# - name: Use Cross
# shell: bash
# run: |
# cargo install cross
# echo "CARGO=cross" >> $GITHUB_ENV
# echo "TARGET_FLAGS=--target ${{ matrix.target }}" >> $GITHUB_ENV
# echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV

# - name: Show command used for Cargo
# run: |
# echo "cargo command is: ${{ env.CARGO }}"
# echo "target flag is: ${{ env.TARGET_FLAGS }}"
# echo "target dir is: ${{ env.TARGET_DIR }}"

# - name: Build release binary
# run: ${{ env.CARGO }} build --verbose --release ${{ env.TARGET_FLAGS }}

# - name: Strip release binary (linux, macos and macos-arm)
# if: matrix.build == 'linux' || matrix.os == 'macos'
# run: strip "target/${{ matrix.target }}/release/sc"

# - name: Strip release binary (arm)
# if: matrix.build == 'linux-arm'
# run: |
# docker run --rm -v \
# "$PWD/target:/target:Z" \
# rustembedded/cross:arm-unknown-linux-gnueabihf \
# arm-linux-gnueabihf-strip \
# /target/arm-unknown-linux-gnueabihf/release/sc

# - name: Build archive
# shell: bash
# run: |
# staging="smartcat-${{ needs.create-release.outputs.smartcat_version }}-${{ matrix.target }}"
# mkdir -p "$staging"/

# cp {README.md,LICENSE} "$staging/"
# # cp {CHANGELOG.md,FAQ.md,GUIDE.md} "$staging/doc/"

# if [ "${{ matrix.os }}" = "windows-latest" ]; then
# cp "target/${{ matrix.target }}/release/sc.exe" "$staging/"
# 7z a "$staging.zip" "$staging"
# certutil -hashfile "$staging.zip" SHA256 > "$staging.zip.sha256"
# echo "ASSET=$staging.zip" >> $GITHUB_ENV
# echo "ASSET_SUM=$staging.zip.sha256" >> $GITHUB_ENV
# else
# # The man page is only generated on Unix systems. ¯\_(ツ)_/¯
# cp "target/${{ matrix.target }}/release/sc" "$staging/"
# tar czf "$staging.tar.gz" "$staging"
# shasum -a 256 "$staging.tar.gz" > "$staging.tar.gz.sha256"
# echo "ASSET=$staging.tar.gz" >> $GITHUB_ENV
# echo "ASSET_SUM=$staging.tar.gz.sha256" >> $GITHUB_ENV
# fi

# - name: Upload release archive
# env:
# GH_TOKEN: ${{ github.token }}
# run: gh release upload ${{ needs.create-release.outputs.smartcat_version }} ${{ env.ASSET }} ${{ env.ASSET_SUM }}

crate-metadata:
name: Extract crate metadata
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Extract crate information
id: crate_metadata
run: |
staging="smartcat-${{ needs.create-release.outputs.smartcat_version }}-${{ matrix.target }}"
mkdir -p "$staging"/
cp {README.md,LICENSE} "$staging/"
# cp {CHANGELOG.md,FAQ.md,GUIDE.md} "$staging/doc/"
if [ "${{ matrix.os }}" = "windows-latest" ]; then
cp "target/${{ matrix.target }}/release/sc.exe" "$staging/"
7z a "$staging.zip" "$staging"
certutil -hashfile "$staging.zip" SHA256 > "$staging.zip.sha256"
echo "ASSET=$staging.zip" >> $GITHUB_ENV
echo "ASSET_SUM=$staging.zip.sha256" >> $GITHUB_ENV
else
# The man page is only generated on Unix systems. ¯\_(ツ)_/¯
cp "target/${{ matrix.target }}/release/sc" "$staging/"
tar czf "$staging.tar.gz" "$staging"
shasum -a 256 "$staging.tar.gz" > "$staging.tar.gz.sha256"
echo "ASSET=$staging.tar.gz" >> $GITHUB_ENV
echo "ASSET_SUM=$staging.tar.gz.sha256" >> $GITHUB_ENV
fi
- name: Upload release archive
cargo metadata --no-deps --format-version 1 | jq -r '"name=" + .packages[0].name' | tee -a $GITHUB_OUTPUT
cargo metadata --no-deps --format-version 1 | jq -r '"version=" + .packages[0].version' | tee -a $GITHUB_OUTPUT
cargo metadata --no-deps --format-version 1 | jq -r '"maintainer=" + .packages[0].authors[0]' | tee -a $GITHUB_OUTPUT
cargo metadata --no-deps --format-version 1 | jq -r '"homepage=" + .packages[0].homepage' | tee -a $GITHUB_OUTPUT
cargo metadata --no-deps --format-version 1 | jq -r '"msrv=" + .packages[0].rust_version' | tee -a $GITHUB_OUTPUT
outputs:
name: ${{ steps.crate_metadata.outputs.name }}
version: ${{ steps.crate_metadata.outputs.version }}
maintainer: ${{ steps.crate_metadata.outputs.maintainer }}
homepage: ${{ steps.crate_metadata.outputs.homepage }}
msrv: ${{ steps.crate_metadata.outputs.msrv }}

build-release:
name: ${{ matrix.job.target }} (${{ matrix.job.os }})
runs-on: ${{ matrix.job.os }}
needs: crate-metadata
needs:
- create-release
- crate-metadata
strategy:
fail-fast: false
matrix:
job:
- { target: aarch64-unknown-linux-gnu , os: ubuntu-20.04, use-cross: true }
- { target: arm-unknown-linux-gnueabihf , os: ubuntu-20.04, use-cross: true }
- { target: arm-unknown-linux-musleabihf, os: ubuntu-20.04, use-cross: true }
- { target: i686-pc-windows-msvc , os: windows-2019 }
- { target: i686-unknown-linux-gnu , os: ubuntu-20.04, use-cross: true }
- { target: i686-unknown-linux-musl , os: ubuntu-20.04, use-cross: true }
- { target: x86_64-apple-darwin , os: macos-12 }
- { target: x86_64-pc-windows-gnu , os: windows-2019 }
- { target: x86_64-pc-windows-msvc , os: windows-2019 }
- { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04, use-cross: true }
- { target: x86_64-unknown-linux-musl , os: ubuntu-20.04, use-cross: true }
env:
GH_TOKEN: ${{ github.token }}
run: gh release upload ${{ needs.create-release.outputs.smartcat_version }} ${{ env.ASSET }} ${{ env.ASSET_SUM }}
BUILD_CMD: cargo
steps:
- name: Checkout source code
uses: actions/checkout@v4

- name: Install prerequisites
shell: bash
run: |
case ${{ matrix.job.target }} in
arm-unknown-linux-*) sudo apt-get -y update ; sudo apt-get -y install gcc-arm-linux-gnueabihf ;;
aarch64-unknown-linux-gnu) sudo apt-get -y update ; sudo apt-get -y install gcc-aarch64-linux-gnu ;;
esac
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.job.target }}

- name: Install cross
if: matrix.job.use-cross
uses: taiki-e/install-action@v2
with:
tool: cross

- name: Overwrite build command env variable
if: matrix.job.use-cross
shell: bash
run: echo "BUILD_CMD=cross" >> $GITHUB_ENV

- name: Show version information (Rust, cargo, GCC)
shell: bash
run: |
gcc --version || true
rustup -V
rustup toolchain list
rustup default
cargo -V
rustc -V
- name: Build
shell: bash
run: $BUILD_CMD build --locked --release --target=${{ matrix.job.target }}

- name: Set binary name & path
id: bin
shell: bash
run: |
# Figure out suffix of binary
EXE_suffix=""
case ${{ matrix.job.target }} in
*-pc-windows-*) EXE_suffix=".exe" ;;
esac;
# Setup paths
BIN_NAME="${{ needs.crate_metadata.outputs.name }}${EXE_suffix}"
BIN_PATH="target/${{ matrix.job.target }}/release/${BIN_NAME}"
# Let subsequent steps know where to find the binary
echo "BIN_PATH=${BIN_PATH}" >> $GITHUB_OUTPUT
echo "BIN_NAME=${BIN_NAME}" >> $GITHUB_OUTPUT
- name: Build archive
shell: bash
run: |
PKG_suffix=".tar.gz" ; case ${{ matrix.job.target }} in *-pc-windows-*) PKG_suffix=".zip" ;; esac;
PKG_BASENAME=${{ needs.crate_metadata.outputs.name }}-${{ needs.crate_metadata.outputs.version }}-${{ matrix.job.target }}
PKG_NAME=${PKG_BASENAME}${PKG_suffix}
echo "PKG_NAME=${PKG_NAME}" >> $GITHUB_OUTPUT
PKG_STAGING="${{ env.CICD_INTERMEDIATES_DIR }}/package"
ARCHIVE_DIR="${PKG_STAGING}/${PKG_BASENAME}/"
mkdir -p "${ARCHIVE_DIR}"
# Binary
cp "${{ steps.bin.outputs.BIN_PATH }}" "$ARCHIVE_DIR"
# README, LICENSE and CHANGELOG files
cp "README.md" "LICENSE" "$ARCHIVE_DIR"
# base compressed package
pushd "${PKG_STAGING}/" >/dev/null
case ${{ matrix.job.target }} in
*-pc-windows-*) 7z -y a "${PKG_NAME}" "${PKG_BASENAME}"/* | tail -2 ;;
*) tar czf "${PKG_NAME}" "${PKG_BASENAME}"/* ;;
esac;
popd >/dev/null
# sha
pushd "${PKG_STAGING}/" >/dev/null
case ${{ matrix.job.target }} in
*-pc-windows-*) certutil -hashfile "${PKG_NAME}" SHA256 "${PKG_NAME}.zip.sha256" ;;
*) shasum -a 256 "${PKG_NAME}" "${PKG_NAME}.tar.gz.sha256" ;;
esac;
popd >/dev/null
# Let subsequent steps know where to find the compressed package
echo "ASSET_PATH=${PKG_STAGING}/${PKG_NAME}" >> $GITHUB_OUTPUT
echo "ASSET_SUM=${PKG_STAGING}/${PKG_NAME}.zip.sha256" >> $GITHUB_ENV
- name: Upload release archive
env:
GH_TOKEN: ${{ github.token }}
run: gh release upload ${{ needs.create-release.outputs.smartcat_version }} ${{ env.ASSET_PATH }} ${{ env.ASSET_SUM }}

0 comments on commit fd3cdfd

Please sign in to comment.