Skip to content

Commit

Permalink
feat: Add platform arg to force building a specific architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Sep 29, 2024
1 parent 20d1950 commit ba1f9ba
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 3 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,58 @@ jobs:
grep -q 'ARG IMAGE_REGISTRY=ghcr.io/blue-build' Containerfile || exit 1
bluebuild build --retry-push -B docker -I docker -S sigstore --push -vv recipes/recipe.yml recipes/recipe-39.yml
arm64-build:
timeout-minutes: 60
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
needs:
- build
if: needs.build.outputs.push == 'true'

steps:
- name: Maximize build space
uses: ublue-os/remove-unwanted-software@v6

- uses: sigstore/[email protected]

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true

- uses: actions-rust-lang/setup-rust-toolchain@v1

- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}

- name: Install bluebuild
run: |
cargo install --path . --debug --all-features
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v3

- name: Run Build
env:
GH_TOKEN: ${{ github.token }}
GH_PR_EVENT_NUMBER: ${{ github.event.number }}
COSIGN_PRIVATE_KEY: ${{ secrets.TEST_SIGNING_SECRET }}
BB_BUILDKIT_CACHE_GHA: true
run: |
cd integration-tests/test-repo
bluebuild build \
--retry-push \
--platform linux/arm64 \
--push \
-vv \
recipes/recipe-arm64.yml
docker-build-external-login:
timeout-minutes: 60
runs-on: ubuntu-latest
Expand Down
38 changes: 38 additions & 0 deletions integration-tests/test-repo/recipes/recipe-arm64.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: cli/test
description: This is my personal OS image.
base-image: quay.io/fedora/fedora-silverblue
image-version: 40
alt_tags:
- arm64
stages:
modules:
- from-file: akmods.yml
- from-file: flatpaks.yml

- type: files
files:
- usr: /usr

- type: script
scripts:
- example.sh

- type: rpm-ostree
repos:
- https://copr.fedorainfracloud.org/coprs/atim/starship/repo/fedora-%OS_VERSION%/atim-starship-fedora-%OS_VERSION%.repo
install:
- micro
- starship
remove:
- firefox
- firefox-langpacks

- type: signing

- type: test-module

- type: containerfile
containerfiles:
- labels
snippets:
- RUN echo "This is a snippet" && ostree container commit
1 change: 1 addition & 0 deletions process/drivers/buildah_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl BuildDriver for BuildahDriver {
let command = cmd!(
"buildah",
"build",
if let Some(platform) = opts.platform => format!("--platform={platform}"),
"--pull=true",
format!("--layers={}", !opts.squash),
"-f",
Expand Down
2 changes: 2 additions & 0 deletions process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl BuildDriver for DockerDriver {
let status = cmd!(
"docker",
"build",
if let Some(platform) = opts.platform => format!("--platform={platform}"),
"-t",
&*opts.image,
"-f",
Expand Down Expand Up @@ -235,6 +236,7 @@ impl BuildDriver for DockerDriver {
},
"build",
"--pull",
if let Some(platform) = opts.platform => format!("--platform={platform}"),
"-f",
&*opts.containerfile,
// https://github.com/moby/buildkit?tab=readme-ov-file#github-actions-cache-experimental
Expand Down
10 changes: 7 additions & 3 deletions process/drivers/opts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{borrow::Cow, path::Path};

use bon::Builder;

use crate::drivers::types::Platform;

use super::CompressionType;

/// Options for building
Expand All @@ -15,14 +17,13 @@ pub struct BuildOpts<'scope> {

#[builder(into)]
pub containerfile: Cow<'scope, Path>,
pub platform: Option<Platform>,
}

#[derive(Debug, Clone, Builder)]
#[builder(on(Cow<'_, str>, into))]
pub struct TagOpts<'scope> {
#[builder(into)]
pub src_image: Cow<'scope, str>,

#[builder(into)]
pub dest_image: Cow<'scope, str>,
}

Expand Down Expand Up @@ -80,4 +81,7 @@ pub struct BuildTagPushOpts<'scope> {
/// Run all steps in a single layer.
#[builder(default)]
pub squash: bool,

/// The platform to build the image on.
pub platform: Option<Platform>,
}
1 change: 1 addition & 0 deletions process/drivers/podman_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl BuildDriver for PodmanDriver {
let command = cmd!(
"podman",
"build",
if let Some(platform) = opts.platform => format!("--platform={platform}"),
"--pull=true",
format!("--layers={}", !opts.squash),
"-f",
Expand Down
1 change: 1 addition & 0 deletions process/drivers/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub trait BuildDriver {
let build_opts = BuildOpts::builder()
.image(&full_image)
.containerfile(opts.containerfile.as_ref())
.maybe_platform(opts.platform)
.squash(opts.squash)
.build();

Expand Down
22 changes: 22 additions & 0 deletions process/drivers/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,25 @@ impl DetermineDriver<CiDriverType> for Option<CiDriverType> {
)
}
}

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Platform {
#[value(name = "linux/amd64")]
LinuxAmd64,

#[value(name = "linux/arm64")]
LinuxArm64,
}

impl std::fmt::Display for Platform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match *self {
Self::LinuxAmd64 => "linux/amd64",
Self::LinuxArm64 => "linux/arm64",
}
)
}
}
1 change: 1 addition & 0 deletions scripts/exports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ color_string() {

# Parse OS version and export it
export OS_VERSION=$(grep -Po "(?<=VERSION_ID=)\d+" /usr/lib/os-release)
export OS_ARCH=$(uname -m)

# Export functions for use in sub-shells or sourced scripts
export -f get_yaml_array
Expand Down
11 changes: 11 additions & 0 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use blue_build_process_management::{
BuildTagPushOpts, CheckKeyPairOpts, CompressionType, GenerateImageNameOpts,
GenerateTagsOpts, SignVerifyOpts,
},
types::Platform,
BuildDriver, CiDriver, Driver, DriverArgs, SigningDriver,
},
logging::{color_str, gen_random_ansi_color},
Expand Down Expand Up @@ -58,6 +59,14 @@ pub struct BuildCommand {
#[builder(default)]
push: bool,

/// Build for a specific platform.
///
/// NOTE: Building for a different architecture
/// than your hardware will require installing
/// qemu.
#[arg(long)]
platform: Option<Platform>,

/// The compression format the images
/// will be pushed in.
#[arg(short, long, default_value_t = CompressionType::Gzip)]
Expand Down Expand Up @@ -268,6 +277,7 @@ impl BuildCommand {
let opts = if let Some(archive_dir) = self.archive.as_ref() {
BuildTagPushOpts::builder()
.containerfile(containerfile)
.maybe_platform(self.platform)
.archive_path(format!(
"{}/{}.{ARCHIVE_SUFFIX}",
archive_dir.to_string_lossy().trim_end_matches('/'),
Expand All @@ -279,6 +289,7 @@ impl BuildCommand {
BuildTagPushOpts::builder()
.image(&image_name)
.containerfile(containerfile)
.maybe_platform(self.platform)
.tags(tags.to_cow_vec())
.push(self.push)
.retry_push(self.retry_push)
Expand Down

0 comments on commit ba1f9ba

Please sign in to comment.