GitHub 操作生成的货物构建工件不会在本地执行

Cargo build artifact produced by GitHub Action does not execute locally

我正在尝试构建一个简单的“Hello World”Rust 程序,并使用构建过程的工件创建一个 GitHub 版本。正在使用的工具链是 stable-x86_64-unknown-linux-gnu,在本地 运行 cargo buildcargo run 时没有问题。可以在 here. The GitHub Action logs can be found here.

中找到发行版本身以及生成的二进制文件

该操作能够创建发行版,但生成的二进制文件无法在我的系统上执行(Ubuntu 21.10 imish)。在以下命令中,下载的二进制文件的名称为 x86_64-unknown-linux-gnu.

$ bash x86_64-unknown-linux-gnu
x86_64-unknown-linux-gnu: x86_64-unknown-linux-gnu: cannot execute binary file
$ ./x86_64-unknown-linux-gnu
bash: ./x86_64-unknown-linux-gnu: Permission denied

尝试使用 chmod u+x x86_64-unknown-linux-gnu 添加权限后,上述命令没有输出。

$ file x86_64-unknown-linux-gnu
x86_64-unknown-linux-gnu: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=cb612cdcb3dfb4866238c50e96b9799037e427a2, for GNU/Linux 3.2.0, with debug_info, not stripped
$ file /lib/systemd/systemd
/lib/systemd/systemd: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=055a1b9666c7d1677a2942d46ca47e59fe75dae5, for GNU/Linux 3.2.0, stripped
$ uname -m
x86_64

src/main.rs:

fn main() {
    println!("Hello, world!");
}

.github/workflows/release.yml:

name: Release

on:
  push:
    branches:
      - main

env:
  ACTIONS_STEP_DEBUG: true
  PROJECT_NAME: color_difference

jobs:
  linux:
    strategy:
      matrix:
        os: [ubuntu-latest]
        rust:
          - stable

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          override: true
          profile: minimal

      - name: Set up cache
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build sources
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release

      - name: Run UPX
        uses: crazy-max/ghaction-upx@v1
        with:
          version: latest
          files: target/release/${{ env.PROJECT_NAME }}
          args: --brute

      - name: Rename file
        run: cp target/release/${{ env.PROJECT_NAME }} x86_64-unknown-linux-gnu

      - name: Release with artifacts
        uses: ncipollo/release-action@v1
        with:
          name: Release
          tag: latest
          token: ${{ secrets.GITHUB_TOKEN }}
          commit: ${{ github.sha }}
          artifacts: x86_64-unknown-linux-gnu

我找到了解决办法。显然,UPX 以某种方式破坏了 Linux 可执行文件。但是,当我尝试构建 Windows 可执行文件时,UPX 仍然运行良好。为了修复我的 GitHub 操作工作流程,我进行了以下更改:

# old
- name: Run UPX
  uses: crazy-max/ghaction-upx@v1
  with:
    version: latest
    files: target/release/${{ env.PROJECT_NAME }}
    args: --brute
# new
- name: Strip artifact
  run: strip target/release/${{ env.PROJECT_NAME }}

- name: Run UPX
  run: echo "Not supported on linux platform"