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 Oct 3, 2024
1 parent 20d1950 commit 0d9d105
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 68 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
20 changes: 15 additions & 5 deletions process/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
time::Duration,
};

use bon::Builder;
use bon::{bon, Builder};
use cached::proc_macro::cached;
use clap::Args;
use colored::Colorize;
Expand All @@ -24,6 +24,7 @@ use once_cell::sync::Lazy;
use opts::{GenerateImageNameOpts, GenerateTagsOpts};
#[cfg(feature = "sigstore")]
use sigstore_driver::SigstoreDriver;
use types::Platform;
use uuid::Uuid;

use crate::logging::Logger;
Expand Down Expand Up @@ -152,6 +153,7 @@ macro_rules! impl_driver_init {

pub struct Driver;

#[bon]
impl Driver {
/// Initializes the Strategy with user provided credentials.
///
Expand Down Expand Up @@ -192,7 +194,14 @@ impl Driver {
///
/// # Panics
/// Panics if the mutex fails to lock.
pub fn get_os_version(oci_ref: &Reference) -> Result<u64> {
#[builder]
pub fn get_os_version(
/// The OCI image reference.
oci_ref: &Reference,
/// The platform of the image to pull the version info from.
#[builder(default)]
platform: Platform,
) -> Result<u64> {
#[cfg(test)]
{
let _ = oci_ref; // silence lint
Expand All @@ -203,7 +212,7 @@ impl Driver {
}

trace!("Driver::get_os_version({oci_ref:#?})");
get_version(oci_ref)
get_version(oci_ref, platform)
}

fn get_build_driver() -> BuildDriverType {
Expand All @@ -230,10 +239,10 @@ impl Driver {
#[cached(
result = true,
key = "String",
convert = "{ oci_ref.to_string() }",
convert = r#"{ format!("{oci_ref}-{platform}") }"#,
sync_writes = true
)]
fn get_version(oci_ref: &Reference) -> Result<u64> {
fn get_version(oci_ref: &Reference, platform: Platform) -> Result<u64> {
info!("Retrieving OS version from {oci_ref}. This might take a bit");
let inspect_opts = GetMetadataOpts::builder()
.image(format!(
Expand All @@ -242,6 +251,7 @@ fn get_version(oci_ref: &Reference) -> Result<u64> {
oci_ref.repository()
))
.tag(oci_ref.tag().unwrap_or("latest"))
.platform(platform)
.build();
let os_version = Driver::get_metadata(&inspect_opts)
.and_then(|inspection| {
Expand Down
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",
format!("--platform={}", opts.platform),
"--pull=true",
format!("--layers={}", !opts.squash),
"-f",
Expand Down
12 changes: 11 additions & 1 deletion process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use blue_build_utils::{
constants::{BB_BUILDKIT_CACHE_GHA, CONTAINER_FILE, DOCKER_HOST, SKOPEO_IMAGE},
credentials::Credentials,
string_vec,
traits::IntoCollector,
};
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
Expand All @@ -26,6 +27,7 @@ use crate::{
drivers::{
image_metadata::ImageMetadata,
opts::{RunOptsEnv, RunOptsVolume},
types::Platform,
},
logging::{CommandLogging, Logger},
signal_handler::{add_cid, remove_cid, ContainerId, ContainerRuntime},
Expand Down Expand Up @@ -130,6 +132,7 @@ impl BuildDriver for DockerDriver {
let status = cmd!(
"docker",
"build",
format!("--platform={}", opts.platform),
"-t",
&*opts.image,
"-f",
Expand Down Expand Up @@ -235,6 +238,7 @@ impl BuildDriver for DockerDriver {
},
"build",
"--pull",
format!("--platform={}", opts.platform),
"-f",
&*opts.containerfile,
// https://github.com/moby/buildkit?tab=readme-ov-file#github-actions-cache-experimental
Expand Down Expand Up @@ -322,10 +326,16 @@ impl InspectDriver for DockerDriver {
);
progress.enable_steady_tick(Duration::from_millis(100));

let mut args = Vec::new();
if matches!(opts.platform, Platform::LinuxAmd64 | Platform::LinuxArm64) {
args.extend(["--override-arch", opts.platform.arch()]);
}
args.extend(["inspect", &url]);

let output = Self::run_output(
&RunOpts::builder()
.image(SKOPEO_IMAGE)
.args(bon::vec!["inspect", &url])
.args(args.collect_into_vec())
.remove(true)
.build(),
)
Expand Down
10 changes: 7 additions & 3 deletions process/drivers/github_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ impl CiDriver for GithubDriver {
fn generate_tags(opts: &GenerateTagsOpts) -> miette::Result<Vec<String>> {
const PR_EVENT: &str = "pull_request";
let timestamp = blue_build_utils::get_tag_timestamp();
let os_version =
Driver::get_os_version(opts.oci_ref).inspect(|v| trace!("os_version={v}"))?;
let os_version = Driver::get_os_version()
.oci_ref(opts.oci_ref)
.platform(opts.platform)
.call()
.inspect(|v| trace!("os_version={v}"))?;
let ref_name = get_env_var(GITHUB_REF_NAME).inspect(|v| trace!("{GITHUB_REF_NAME}={v}"))?;
let short_sha = {
let mut short_sha = get_env_var(GITHUB_SHA).inspect(|v| trace!("{GITHUB_SHA}={v}"))?;
Expand Down Expand Up @@ -144,7 +147,7 @@ mod test {
use rstest::rstest;

use crate::{
drivers::{opts::GenerateTagsOpts, CiDriver},
drivers::{opts::GenerateTagsOpts, types::Platform, CiDriver},
test::{TEST_TAG_1, TEST_TAG_2, TIMESTAMP},
};

Expand Down Expand Up @@ -285,6 +288,7 @@ mod test {
&GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.maybe_alt_tags(alt_tags)
.platform(Platform::LinuxAmd64)
.build(),
)
.unwrap();
Expand Down
6 changes: 5 additions & 1 deletion process/drivers/gitlab_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl CiDriver for GitlabDriver {

fn generate_tags(opts: &GenerateTagsOpts) -> miette::Result<Vec<String>> {
const MR_EVENT: &str = "merge_request_event";
let os_version = Driver::get_os_version(opts.oci_ref)?;
let os_version = Driver::get_os_version()
.oci_ref(opts.oci_ref)
.platform(opts.platform)
.call()?;
let timestamp = blue_build_utils::get_tag_timestamp();
let short_sha =
get_env_var(CI_COMMIT_SHORT_SHA).inspect(|v| trace!("{CI_COMMIT_SHORT_SHA}={v}"))?;
Expand Down Expand Up @@ -293,6 +296,7 @@ mod test {
&GenerateTagsOpts::builder()
.oci_ref(&oci_ref)
.maybe_alt_tags(alt_tags)
.platform(crate::drivers::types::Platform::LinuxAmd64)
.build(),
)
.unwrap();
Expand Down
5 changes: 4 additions & 1 deletion process/drivers/local_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ impl CiDriver for LocalDriver {

fn generate_tags(opts: &GenerateTagsOpts) -> miette::Result<Vec<String>> {
trace!("LocalDriver::generate_tags({opts:?})");
let os_version = Driver::get_os_version(opts.oci_ref)?;
let os_version = Driver::get_os_version()
.oci_ref(opts.oci_ref)
.platform(opts.platform)
.call()?;
let timestamp = blue_build_utils::get_tag_timestamp();
let short_sha = commit_sha();

Expand Down
13 changes: 10 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,15 @@ pub struct BuildOpts<'scope> {

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

#[builder(default)]
pub platform: 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 +83,8 @@ pub struct BuildTagPushOpts<'scope> {
/// Run all steps in a single layer.
#[builder(default)]
pub squash: bool,

/// The platform to build the image on.
#[builder(default)]
pub platform: Platform,
}
5 changes: 5 additions & 0 deletions process/drivers/opts/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ use std::borrow::Cow;
use bon::Builder;
use oci_distribution::Reference;

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

#[derive(Debug, Clone, Builder)]
pub struct GenerateTagsOpts<'scope> {
pub oci_ref: &'scope Reference,

#[builder(into)]
pub alt_tags: Option<Vec<Cow<'scope, str>>>,

#[builder(default)]
pub platform: Platform,
}

#[derive(Debug, Clone, Builder)]
Expand Down
3 changes: 3 additions & 0 deletions process/drivers/opts/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::borrow::Cow;

use bon::Builder;

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

#[derive(Debug, Clone, Builder)]
pub struct GetMetadataOpts<'scope> {
#[builder(into)]
pub image: Cow<'scope, str>,

#[builder(into)]
pub tag: Option<Cow<'scope, str>>,
pub platform: Platform,
}
5 changes: 5 additions & 0 deletions process/drivers/opts/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use bon::Builder;
use miette::{IntoDiagnostic, Result};
use zeroize::{Zeroize, Zeroizing};

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

pub enum PrivateKey {
Env(String),
Path(PathBuf),
Expand Down Expand Up @@ -112,4 +114,7 @@ pub struct SignVerifyOpts<'scope> {
/// Defaults to 1.
#[builder(default = 1)]
pub retry_count: u8,

#[builder(default)]
pub platform: Platform,
}
Loading

0 comments on commit 0d9d105

Please sign in to comment.