Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions .github/workflows/build_interu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,14 @@ jobs:
with:
persist-credentials: false

- name: Setup Rust Toolchain
env:
RUST_TARGETS: ${{ inputs.target }}
shell: bash
# NOTE: target is a comma separated list of targets, which is fine here
# but further down it would break. Maybe we want to validate the target
# input is actually one target (and use the matrix for building multiple
# targets in parallel)
run: rustup toolchain install "${RUST_VERSION}" --target "${RUST_TARGETS}"

- name: Build Binary
env:
TARGET: ${{ inputs.target }}
run: cargo build --target "$TARGET" --release --package interu

- name: Rename Binary
env:
TARGET: ${{ inputs.target }}
run: mv "target/$TARGET/release/interu" "interu-$TARGET"
uses: $/build-rust-binary
with:
toolchain: ${{ env.RUST_VERSION }}
target: ${{ inputs.target }}
name: interu
cargo-cyclonedx-version: latest
cyclonedx-source-path: tools

- name: Upload Artifact
if: inputs.upload
Expand Down
16 changes: 16 additions & 0 deletions .scripts/actions/install_cargo-auditable.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

ARCH=$(uname -m)
cp "$GITHUB_ACTION_PATH/../configs/curlrc" "$XDG_CONFIG_HOME/curlrc"

if [ "$CARGO_AUDITABLE_VERSION" == "latest" ]; then
curl --config "$XDG_CONFIG_HOME/curlrc" -o /tmp/cargo-auditable "https://github.com/rust-secure-code/cargo-auditable/releases/latest/download/cargo-auditable-${ARCH}-unknown-linux-gnu"
else
curl --config "$XDG_CONFIG_HOME/curlrc" -o /tmp/cargo-auditable "https://github.com/rust-secure-code/cargo-auditable/releases/download/cargo-auditable-${CARGO_AUDITABLE_VERSION}/cargo-auditable-${ARCH}-unknown-linux-gnu"
fi

sudo install -m 755 -t /usr/local/bin /tmp/cargo-auditable
rm -rf /tmp/cargo-auditable
18 changes: 18 additions & 0 deletions .scripts/actions/install_cargo-cyclonedx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

ARCH=$(uname -m)
cp "$GITHUB_ACTION_PATH/../configs/curlrc" "$XDG_CONFIG_HOME/curlrc"
mkdir /tmp/cargo-cyclonedx

if [ "$CARGO_CYCLONEDX_VERSION" == "latest" ]; then
curl --config "$XDG_CONFIG_HOME/curlrc" -o /tmp/cargo-cyclonedx/cargo-cyclonedx.tar.xz "https://github.com/CycloneDX/cyclonedx-rust-cargo/releases/latest/download/cargo-cyclonedx-${ARCH}-unknown-linux-gnu.tar.xz"
else
curl --config "$XDG_CONFIG_HOME/curlrc" -o /tmp/cargo-cyclonedx/cargo-cyclonedx.tar.xz "https://github.com/CycloneDX/cyclonedx-rust-cargo/releases/download/cargo-cyclonedx-${CARGO_CYCLONEDX_VERSION}/cargo-cyclonedx-${ARCH}-unknown-linux-gnu.tar.xz"
fi

tar --directory="/tmp/cargo-cyclonedx" -Jxvf /tmp/cargo-cyclonedx/cargo-cyclonedx.tar.xz "cargo-cyclonedx-${ARCH}-unknown-linux-gnu/cargo-cyclonedx"
sudo install -m 755 -t /usr/local/bin "/tmp/cargo-cyclonedx/cargo-cyclonedx-${ARCH}-unknown-linux-gnu/cargo-cyclonedx"
rm -rf /tmp/cargo-cyclonedx
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ particular step in a workflow.
<!-- start:links: autogenerated by .scripts/local/update_readme_list.sh -->
- [build-container-image](./build-container-image/README.md)
- [build-product-image](./build-product-image/README.md)
- [build-rust-binary](./build-rust-binary/README.md)
- [check-pr-title](./check-pr-title/README.md)
- [detect-changes](./detect-changes/README.md)
- [free-disk-space](./free-disk-space/README.md)
Expand Down
161 changes: 161 additions & 0 deletions build-rust-binary/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
---
name: Build Rust Binary
description: This action builds a single Rust binary

inputs:
toolchain:
description: The toolchain version to install.
required: true
components:
description: The toolchain components to install.
required: false
default: ""
target:
description: The target, like x86_64-unknown-linux-gnu.
required: true
name:
description: The name of the binary to compile.
required: true
rustup-version:
description: The version of rustup to install. UNUSED
required: false
default: latest
cargo-auditable-version:
description: |
The version cargo-auditable. Also instruct this action to build the binary using
cargo-auditable.
required: false
cargo-cyclonedx-version:
description: |
The version of cargo-cyclonedx. Also instructs the action to generate an SBOM with
cargo-cyclonedx.
required: false
cyclonedx-spec-version:
description: The CycloneDX spec version.
required: false
default: "1.5"
cyclonedx-source-path:
description: The path to the source code of the binary where cargo-cyclonedx places the SBOM.
required: false

runs:
using: composite
steps:
- name: Format Toolchain Cache Key
id: toolchain-cache-key
shell: bash
env:
COMPONENTS: ${{ inputs.components }}
TOOLCHAIN: ${{ inputs.toolchain }}
TARGET: ${{ inputs.target }}
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

COMPONENTS=${COMPONENTS//,/_}
COMPONENTS=${COMPONENTS:-none}

echo "CACHE_KEY=rust-toolchain-${TOOLCHAIN}-target-${TARGET}-components-${COMPONENTS}" | tee -a "$GITHUB_OUTPUT"

- name: Setup Toolchain Cache
id: toolchain-cache
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: ~/.rustup/toolchains
key: ${{ steps.toolchain-cache-key.outputs.CACHE_KEY }}

- name: Setup Toolchain
if: ${{ steps.toolchain-cache.outputs.cache-hit != 'true' }}
env:
COMPONENTS: ${{ inputs.components }}
TOOLCHAIN: ${{ inputs.toolchain }}
TARGET: ${{ inputs.target }}
shell: bash
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

if [ -z "$COMPONENTS" ]; then
rustup toolchain install "${TOOLCHAIN}" --target "${TARGET}"
else
rustup toolchain install "${TOOLCHAIN}" --target "${TARGET}" --component "${COMPONENTS}"
fi

- name: Setup Cache
# Only cache build artifacts if this is run as part of a PR to avoid potential cache poisoning
# attacks.
if: github.event_name == 'pull_request'
uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1

- name: Setup cargo-auditable (${{ inputs.cargo-auditable-version }})
if: inputs.cargo-auditable-version
env:
CARGO_AUDITABLE_VERSION: ${{ inputs.cargo-auditable-version }}
shell: bash
run: "$GITHUB_ACTION_PATH/../.scripts/actions/install_cargo-auditable.sh"

- name: Setup cargo-cyclonedx (${{ inputs.cargo-cyclonedx-version }})
if: inputs.cargo-cyclonedx-version
env:
CARGO_CYCLONEDX_VERSION: ${{ inputs.cargo-cyclonedx-version }}
shell: bash
run: "$GITHUB_ACTION_PATH/../.scripts/actions/install_cargo-cyclonedx.sh"

- name: Build Binary
if: ${{ !inputs.cargo-auditable-version }}
env:
TARGET: ${{ inputs.target }}
NAME: ${{ inputs.name }}
shell: bash
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

cargo build --target "$TARGET" --release --package "$NAME"

- name: Build Binary with cargo-auditable
if: inputs.cargo-auditable-version
env:
TARGET: ${{ inputs.target }}
NAME: ${{ inputs.name }}
shell: bash
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

cargo auditable --quiet build --target "$TARGET" --release --package "$NAME"

- name: Generate SBOM
if: inputs.cargo-cyclonedx-version
env:
CYCLONEDX_SPEC_VERSION: ${{ inputs.cyclonedx-spec-version }}
shell: bash
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

cargo cyclonedx --all --spec-version "$CYCLONEDX_SPEC_VERSION" --describe binaries

- name: Rename Binary
env:
TARGET: ${{ inputs.target }}
NAME: ${{ inputs.name }}
shell: bash
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

mv "target/${TARGET}/release/${NAME}" "${NAME}-${TARGET}"

- name: Rename SBOM
if: inputs.cargo-cyclonedx-version
env:
CYCLONEDX_SOURCE_PATH: ${{ inputs.cyclonedx-source-path }}
TARGET: ${{ inputs.target }}
NAME: ${{ inputs.name }}
shell: bash
run: |
set -euo pipefail
[ -n "${RUNNER_DEBUG+set}" ] && set -x

mv "${CYCLONEDX_SOURCE_PATH}/${NAME}/${NAME}_bin.cdx.xml" "${NAME}-${TARGET}.cdx.xml"