ubuntu - GitHub Action 生成的 Cargo 构建工件不在本地执行

标签 ubuntu rust continuous-integration github-actions rust-cargo

我正在尝试构建一个简单的“Hello World”Rust 程序并使用构建过程的工件创建一个 GitHub 版本。正在使用的工具链是 stable-x86_64-unknown-linux-gnu ,运行时没有问题cargo buildcargo run本地。可以在 here 中找到发布本身以及生成的二进制文件。 .可以找到 GitHub 操作日志 here .
该操作能够创建发布,但生成的二进制文件无法在我的系统上执行(Ubuntu 21.10 impish)。在以下命令中,下载的二进制文件的名称是 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 Action 工作流程,我进行了以下更改:

# 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"

关于ubuntu - GitHub Action 生成的 Cargo 构建工件不在本地执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70634532/

相关文章:

linux - Oracle 12c Ubuntu 17.04 安装报错

struct - Rust 特征状态

Github 操作 : Post comment to PR workflow that triggered the current workflow

continuous-integration - jenkins 开始丢失所有工作,尝试使用 'copy existing job' 功能

java - MATLAB 代理无法在 180000 毫秒内创建

node.js - #!/usr/bin/env : No such file or directory

java - 无法使用 java 进程执行器执行单元脚本

rust - 如何使用PyO3在Rust代码中嵌入的Python代码中执行地板分割?

postgresql - 我无法捕获数据库引用

windows - 使用 Windows 批处理命令配置 Jenkins CI 作业和 FreeFileSync 批处理