Browse Source

chore: Add release action

Signed-off-by: zu1k <[email protected]>
zu1k 3 năm trước cách đây
mục cha
commit
14c4ad58ef

+ 112 - 0
.github/workflows/build-release.yml

@@ -0,0 +1,112 @@
+name: Build Releases
+on:
+  release:
+    types: [published]
+
+env:
+  CARGO_TERM_COLOR: always
+
+jobs:
+  build-cross:
+    runs-on: ubuntu-latest
+    env:
+      RUST_BACKTRACE: full
+    strategy:
+      matrix:
+        target:
+          - i686-unknown-linux-musl
+          - x86_64-pc-windows-gnu
+          - x86_64-unknown-linux-gnu
+          - x86_64-unknown-linux-musl
+          - armv7-unknown-linux-musleabihf
+          - armv7-unknown-linux-gnueabihf
+          - aarch64-unknown-linux-gnu
+          - aarch64-unknown-linux-musl
+
+    steps:
+      - uses: actions/checkout@v2
+
+      - name: Install dependences
+        run: sudo apt-get update -y && sudo apt-get install -y upx;
+
+      - name: Install Rust
+        uses: actions-rs/toolchain@v1
+        with:
+          profile: minimal
+          target: ${{ matrix.target }}
+          toolchain: nightly
+          default: true
+          override: true
+
+      - name: Install cross
+        run: cargo install cross
+
+      - name: Build ${{ matrix.target }}
+        timeout-minutes: 120
+        run: |
+          compile_target=${{ matrix.target }}
+
+          if [[ "$compile_target" == "mips-"* || "$compile_target" == "mipsel-"* || "$compile_target" == "mips64-"* || "$compile_target" == "mips64el-"* ]]; then
+            if [[ "$?" == "0" ]]; then
+              compile_compress="-u"
+            fi
+          fi
+
+          cd build
+          ./build-release.sh -t ${{ matrix.target }} $compile_features $compile_compress
+
+      - name: Upload Github Assets
+        uses: softprops/action-gh-release@v1
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        with:
+          files: build/release/*
+          prerelease: ${{ contains(github.ref, '-') }}
+
+  build-unix:
+    runs-on: ${{ matrix.os }}
+    env:
+      RUST_BACKTRACE: full
+    strategy:
+      matrix:
+        os: [macos-latest]
+        target:
+          - x86_64-apple-darwin
+          - aarch64-apple-darwin
+    steps:
+      - uses: actions/checkout@v2
+
+      - name: Install GNU tar
+        if: runner.os == 'macOS'
+        run: |
+          brew install gnu-tar
+          # echo "::add-path::/usr/local/opt/gnu-tar/libexec/gnubin"
+          echo "/usr/local/opt/gnu-tar/libexec/gnubin" >> $GITHUB_PATH
+
+      - name: Install Rust
+        uses: actions-rs/toolchain@v1
+        with:
+          profile: minimal
+          target: ${{ matrix.target }}
+          toolchain: nightly
+          default: true
+          override: true
+
+      # https://github.com/actions/virtual-environments/issues/2557#issuecomment-769611326
+      - if: ${{ matrix.target }} == 'aarch64-apple-darwin'
+        run: |
+          sudo xcode-select -s /Applications/Xcode_12.4.app &&
+          sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*
+
+      - name: Build release
+        shell: bash
+        run: |
+          ./build/build-host-release.sh -t ${{ matrix.target }}
+
+      - name: Upload Github Assets
+        uses: softprops/action-gh-release@v1
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        with:
+          files: build/release/*
+          prerelease: ${{ contains(github.ref, '-') }}

+ 1 - 1
.github/workflows/build.yml → .github/workflows/build-test.yml

@@ -1,6 +1,6 @@
 on: [push, pull_request_target]
 
-name: Build
+name: Build Test
 
 jobs:
   build:

+ 103 - 0
build/build-host-release.sh

@@ -0,0 +1,103 @@
+#!/bin/bash
+
+BUILD_TARGET=""
+BUILD_FEATURES=()
+while getopts "t:f:" opt; do
+    case $opt in
+        t)
+            BUILD_TARGET=$OPTARG
+            ;;
+        f)
+            BUILD_FEATURES+=($OPTARG)
+            ;;
+        ?)
+            echo "Usage: $(basename $0) [-t <target-triple>] [-f <feature>]"
+            ;;
+    esac
+done
+
+BUILD_FEATURES+=${BUILD_EXTRA_FEATURES}
+
+ROOT_DIR=$( cd $( dirname $0 ) && pwd )
+VERSION=$(grep -E '^version' "${ROOT_DIR}/../Cargo.toml" | awk '{print $3}' | sed 's/"//g')
+HOST_TRIPLE=$(rustc -Vv | grep 'host:' | awk '{print $2}')
+
+echo "Started build release ${VERSION} for ${HOST_TRIPLE} (target: ${BUILD_TARGET}) with features \"${BUILD_FEATURES}\"..."
+
+if [[ "${BUILD_TARGET}" != "" ]]; then
+    if [[ "${BUILD_FEATURES}" != "" ]]; then
+        cargo build --release --features "${BUILD_FEATURES}" --target "${BUILD_TARGET}"
+    else
+        cargo build --release --target "${BUILD_TARGET}"
+    fi
+else
+    if [[ "${BUILD_FEATURES}" != "" ]]; then
+        cargo build --release --features "${BUILD_FEATURES}"
+    else
+        cargo build --release
+    fi
+fi
+
+if [[ "$?" != "0" ]]; then
+    exit 1;
+fi
+
+if [[ "${BUILD_TARGET}" == "" ]]; then
+    BUILD_TARGET=$HOST_TRIPLE
+fi
+
+TARGET_SUFFIX=""
+if [[ "${BUILD_TARGET}" == *"-windows-"* ]]; then
+    TARGET_SUFFIX=".exe"
+fi
+
+TARGET="good-mitm${TARGET_SUFFIX}"
+
+RELEASE_FOLDER="${ROOT_DIR}/release"
+RELEASE_PACKAGE_NAME="good-mitm-${VERSION}-${BUILD_TARGET}"
+
+mkdir -p "${RELEASE_FOLDER}"
+
+# Into release folder
+if [[ "${BUILD_TARGET}" != "" ]]; then
+    cd "${ROOT_DIR}/../target/${BUILD_TARGET}/release"
+else
+    cd "${ROOT_DIR}/../target/release"
+fi
+
+if [[ "${BUILD_TARGET}" == *"-windows-"* ]]; then
+    # For Windows, use zip
+
+    RELEASE_PACKAGE_FILE_NAME="${RELEASE_PACKAGE_NAME}.zip"
+    RELEASE_PACKAGE_FILE_PATH="${RELEASE_FOLDER}/${RELEASE_PACKAGE_FILE_NAME}"
+    zip "${RELEASE_PACKAGE_FILE_PATH}" "${TARGET}"
+
+    if [[ $? != "0" ]]; then
+        exit 1
+    fi
+
+    # Checksum
+    cd "${RELEASE_FOLDER}"
+    shasum -a 256 "${RELEASE_PACKAGE_FILE_NAME}" > "${RELEASE_PACKAGE_FILE_NAME}.sha256"
+else
+    # For others, Linux, OS X, uses tar.xz
+
+    # For Darwin, .DS_Store and other related files should be ignored
+    if [[ "$(uname -s)" == "Darwin" ]]; then
+        export COPYFILE_DISABLE=1
+    fi
+
+    RELEASE_PACKAGE_FILE_NAME="${RELEASE_PACKAGE_NAME}.tar.xz"
+    RELEASE_PACKAGE_FILE_PATH="${RELEASE_FOLDER}/${RELEASE_PACKAGE_FILE_NAME}"
+    tar -cJf "${RELEASE_PACKAGE_FILE_PATH}" "${TARGET}"
+
+    if [[ $? != "0" ]]; then
+        exit 1
+    fi
+
+    # Checksum
+    cd "${RELEASE_FOLDER}"
+    shasum -a 256 "${RELEASE_PACKAGE_FILE_NAME}" > "${RELEASE_PACKAGE_FILE_NAME}.sha256"
+fi
+
+echo "Finished build release ${RELEASE_PACKAGE_FILE_PATH}"

+ 120 - 0
build/build-release.sh

@@ -0,0 +1,120 @@
+#!/bin/bash
+
+CUR_DIR=$( cd $( dirname $0 ) && pwd )
+VERSION=$(grep -E '^version' ${CUR_DIR}/../Cargo.toml | awk '{print $3}' | sed 's/"//g')
+
+## Disable macos ACL file
+if [[ "$(uname -s)" == "Darwin" ]]; then
+    export COPYFILE_DISABLE=1
+fi
+
+targets=()
+features=()
+use_upx=false
+
+while getopts "t:f:u" opt; do
+    case $opt in
+        t)
+            targets+=($OPTARG)
+            ;;
+        f)
+            features+=($OPTARG)
+            ;;
+        u)
+            use_upx=true
+            ;;
+        ?)
+            echo "Usage: $(basename $0) [-t <target-triple>] [-f features] [-u]"
+            ;;
+    esac
+done
+
+features+=${EXTRA_FEATURES}
+
+if [[ "${#targets[@]}" == "0" ]]; then
+    echo "Specifying compile target with -t <target-triple>"
+    exit 1
+fi
+
+if [[ "${use_upx}" = true ]]; then
+    if [[ -z "$upx" ]] && command -v upx &> /dev/null; then
+        upx="upx -9"
+    fi
+
+    if [[ "x$upx" == "x" ]]; then
+        echo "Couldn't find upx in PATH, consider specifying it with variable \$upx"
+        exit 1
+    fi
+fi
+
+
+function build() {
+    cd "$CUR_DIR/.."
+
+    TARGET=$1
+
+    RELEASE_DIR="target/${TARGET}/release"
+    TARGET_FEATURES="${features[@]}"
+
+    if [[ "${TARGET_FEATURES}" != "" ]]; then
+        echo "* Building ${TARGET} package ${VERSION} with features \"${TARGET_FEATURES}\" ..."
+
+        cross build --target "${TARGET}" \
+                    --features "${TARGET_FEATURES}" \
+                    --release
+    else
+        echo "* Building ${TARGET} package ${VERSION} ..."
+
+        cross build --target "${TARGET}" \
+                    --release
+    fi
+
+    if [[ $? != "0" ]]; then
+        exit 1
+    fi
+
+    PKG_DIR="${CUR_DIR}/release"
+    mkdir -p "${PKG_DIR}"
+
+    if [[ "$TARGET" == *"-linux-"* ]]; then
+        PKG_NAME="good-mitm-${VERSION}-${TARGET}.tar.xz"
+        PKG_PATH="${PKG_DIR}/${PKG_NAME}"
+
+        cd ${RELEASE_DIR}
+
+        if [[ "${use_upx}" = true ]]; then
+            # Enable upx for MIPS.
+            $upx good-mitm #>/dev/null
+        fi
+
+        echo "* Packaging XZ in ${PKG_PATH} ..."
+        tar -cJf ${PKG_PATH} "good-mitm"
+
+        if [[ $? != "0" ]]; then
+            exit 1
+        fi
+
+        cd "${PKG_DIR}"
+        shasum -a 256 "${PKG_NAME}" > "${PKG_NAME}.sha256"
+    elif [[ "$TARGET" == *"-windows-"* ]]; then
+        PKG_NAME="good-mitm-${VERSION}-${TARGET}.zip"
+        PKG_PATH="${PKG_DIR}/${PKG_NAME}"
+
+        echo "* Packaging ZIP in ${PKG_PATH} ..."
+        cd ${RELEASE_DIR}
+        zip ${PKG_PATH} "good-mitm.exe"
+
+        if [[ $? != "0" ]]; then
+            exit 1
+        fi
+
+        cd "${PKG_DIR}"
+        shasum -a 256 "${PKG_NAME}" > "${PKG_NAME}.sha256"
+    fi
+
+    echo "* Done build package ${PKG_NAME}"
+}
+
+for target in "${targets[@]}"; do
+    build "$target";
+done