github action: release.yaml #8
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: Build and Release Rust Project | |
on: | |
push: | |
branches: | |
- 'release/**' # Trigger on branches matching release/{version} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
target: [ | |
#x86_64-unknown-linux-gnu, | |
#x86_64-unknown-linux-musl, | |
#x86_64-pc-windows-gnu, | |
#x86_64-pc-windows-msvc, | |
x86_64-apple-darwin | |
] | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Validate branch name | |
run: | | |
BRANCH_NAME="${GITHUB_REF#refs/heads/release/}" | |
if [[ ! "$BRANCH_NAME" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo "Error: Branch name must follow semantic versioning (e.g., release/1.0.0)." | |
exit 1 | |
fi | |
- name: Extract version from branch name and update Cargo.toml | |
run: | | |
VERSION="${GITHUB_REF#refs/heads/release/}" | |
cd cli | |
PROJECT_NAME=$(awk -F'=' '/^name/ {gsub(/"/, "", $2); print $2}' Cargo.toml | tr -d ' ') | |
sed -i.bak "s/^version = \".*\"/version = \"$VERSION\"/" Cargo.toml | |
echo "PROJECT_NAME=$PROJECT_NAME" >> $GITHUB_ENV | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Install Rust | |
uses: actions-rs/toolchain@v1 | |
with: | |
toolchain: stable | |
override: true | |
- name: Install cross | |
run: cargo install cross | |
- name: Install macOS target | |
if: matrix.target == 'x86_64-apple-darwin' | |
run: rustup target add x86_64-apple-darwin | |
- name: Install MSVC target | |
if: matrix.target == 'x86_64-pc-windows-msvc' | |
run: rustup target add x86_64-pc-windows-msvc | |
- name: Build with cross for non-macOS targets | |
if: matrix.target != 'x86_64-apple-darwin' | |
working-directory: cli | |
env: | |
RUSTFLAGS: '-C target-cpu=native' | |
run: cross build --release --target ${{ matrix.target }} --features jemalloc | |
- name: Build with cargo for macOS target | |
if: matrix.target == 'x86_64-apple-darwin' | |
working-directory: cli | |
env: | |
RUSTFLAGS: '-C target-cpu=native' | |
run: cargo build --release --target ${{ matrix.target }} --features jemalloc | |
- name: Create release directory | |
run: mkdir -p ${{ github.workspace }}/release/${{ matrix.target }} | |
- name: Copy binary to release directory | |
run: cp cli/target/${{ matrix.target }}/release/* ${{ github.workspace }}/release/${{ matrix.target }}/ | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v2 | |
with: | |
name: ${{ matrix.target }} | |
path: ${{ github.workspace }}/release/${{ matrix.target }}/ | |
create_release: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Download artifacts | |
uses: actions/download-artifact@v2 | |
with: | |
name: x86_64-unknown-linux-gnu | |
path: ./release/x86_64-unknown-linux-gnu | |
- name: Download artifacts | |
uses: actions/download-artifact@v2 | |
with: | |
name: x86_64-unknown-linux-musl | |
path: ./release/x86_64-unknown-linux-musl | |
- name: Download artifacts | |
uses: actions/download-artifact@v2 | |
with: | |
name: x86_64-pc-windows-gnu | |
path: ./release/x86_64-pc-windows-gnu | |
- name: Download artifacts | |
uses: actions/download-artifact@v2 | |
with: | |
name: x86_64-pc-windows-msvc | |
path: ./release/x86_64-pc-windows-msvc | |
- name: Download artifacts | |
uses: actions/download-artifact@v2 | |
with: | |
name: x86_64-apple-darwin | |
path: ./release/x86_64-apple-darwin | |
- name: Prepare release | |
run: | | |
ROOT_DIR=$(pwd) | |
TARGET_DIR="${ROOT_DIR}/documentation/docs/public/releases" | |
VERSION=${{ env.VERSION }} | |
VERSION=${VERSION//./-} | |
PROJECT_NAME=${{ env.PROJECT_NAME }} | |
# Remove existing version directory if it exists | |
rm -rf $TARGET_DIR/$PROJECT_NAME/$VERSION | |
mkdir -p $TARGET_DIR/$PROJECT_NAME | |
mkdir -p $TARGET_DIR/$PROJECT_NAME/$VERSION | |
cp ./release/x86_64-unknown-linux-gnu/* $TARGET_DIR/${PROJECT_NAME}_linux_gnu_latest | |
cp ./release/x86_64-unknown-linux-musl/* $TARGET_DIR/${PROJECT_NAME}_linux_musl_latest | |
cp ./release/x86_64-pc-windows-gnu/* $TARGET_DIR/${PROJECT_NAME}_windows_gnu_latest.exe | |
cp ./release/x86_64-pc-windows-msvc/* $TARGET_DIR/${PROJECT_NAME}_windows_msvc_latest.exe | |
cp ./release/x86_64-apple-darwin/* $TARGET_DIR/${PROJECT_NAME}_macos_latest | |
cp ./release/x86_64-unknown-linux-gnu/* $TARGET_DIR/$PROJECT_NAME/$VERSION/${PROJECT_NAME}_linux_gnu | |
cp ./release/x86_64-unknown-linux-musl/* $TARGET_DIR/$PROJECT_NAME/$VERSION/${PROJECT_NAME}_linux_musl | |
cp ./release/x86_64-pc-windows-gnu/* $TARGET_DIR/$PROJECT_NAME/$VERSION/${PROJECT_NAME}_windows_gnu.exe | |
cp ./release/x86_64-pc-windows-msvc/* $TARGET_DIR/$PROJECT_NAME/$VERSION/${PROJECT_NAME}_windows_msvc.exe | |
cp ./release/x86_64-apple-darwin/* $TARGET_DIR/$PROJECT_NAME/$VERSION/${PROJECT_NAME}_macos | |
- name: Commit and push changes | |
run: | | |
git config --global user.name "github-actions" | |
git config --global user.email "[email protected]" | |
git add cli/Cargo.toml | |
git add documentation/docs/public/releases | |
git commit -m "Release ${{ env.VERSION }}" | |
git push origin release/${{ env.VERSION }} | |
- name: Create pull request | |
uses: peter-evans/create-pull-request@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: "Release ${{ env.VERSION }}" | |
branch: release/${{ env.VERSION }} | |
base: master | |
title: "Release ${{ env.VERSION }}" | |
body: "This PR merges release ${{ env.VERSION }} into master and triggers a deploy." |