rust icon indicating copy to clipboard operation
rust copied to clipboard

Detect non-default build targets for cross compilation

Open Brendonovich opened this issue 2 years ago • 4 comments

When compiling in GitHub Actions it's necessary to target x86_64-unknown-linux-musl, which currently can only be set using the CARGO_BUILD_TARGET environment variable - and even then, the runtime doesn't know to look in target/x86_64-unknown-linux-musl/... for build artifacts. Detection of CARGO_BUILD_TARGET would probably be enough, idk if the runtime can be configured with CLI args or via vercel.json.

Brendonovich avatar Mar 18 '23 05:03 Brendonovich

The toolchain file should help in this scenario right? It could be created with https://github.com/marketplace/actions/create-file.

ecklf avatar Mar 28 '23 11:03 ecklf

Hmmm I'm not sure that does the job. I just tried adding a rust-toolchain.toml to a project and restricted it to linux-only, but it still built and ran for macOS. Maybe I'm using it wrong?

Brendonovich avatar Mar 28 '23 11:03 Brendonovich

From what I see we'd also have to use a cargo configuration file.

https://doc.rust-lang.org/cargo/reference/config.html#configuration-format

CleanShot 2023-03-29 at 10 39 01@2x

We can also specify the target directory here to resolve the issue of resolving the build artifacts.

ecklf avatar Mar 29 '23 09:03 ecklf

I ran into this issue as well.

A dirty workaround for me was to use cargo to build the project, then run rm -rf target/release && mv target/x86_64-unknown-linux-musl target/release and then run vercel build

That way I could also use the rust-cache action so my builds are pretty snappy now.

This method is obviously not ideal, but at least gets the job done for now.

Full github action workflow

name: vercel-dev

on:
  push:
    branches-ignore:
      - main

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
  # rust:
  #   uses: ./.github/workflows/rust.yml
  deploy:
    runs-on: ubuntu-latest
    # needs: rust
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Rust setup
        uses: actions-rs/toolchain@v1
        with:
          target: x86_64-unknown-linux-musl
          profile: minimal
          toolchain: stable
          override: true
      - run: rustup default stable-x86_64-unknown-linux-musl
      - name: Rust cache
        uses: Swatinem/rust-cache@v2
      - name: Rust build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --quiet --release
      - run: rm -rf target/release && mv target/x86_64-unknown-linux-musl/release target/release/
      - name: Vercel build
        run: npm install --global vercel@canary
      - name: Pull Vercel Environment Information
        run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
      - name: Build Project Artifacts
        run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
      - name: Deploy to Vercel
        uses: amondnet/vercel-action@v25
        with:
          vercel-args: "--prebuilt"
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          github-token: ${{ secrets.GITHUB_TOKEN }}
          vercel-org-id: ${{ vars.VERCEL_ORG_ID}}
          vercel-project-id: ${{ vars.VERCEL_PROJECT_ID}}

arnarthor avatar Apr 03 '23 01:04 arnarthor