Skip to content

Commit

Permalink
Try #786:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Jun 13, 2022
2 parents 239e8cb + 3b2b7e3 commit 23af727
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 314 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
@@ -0,0 +1,3 @@
[alias]
cross-dev = ["run", "--bin", "cross-dev", "--features", "dev", "--"]
build-docker-image = ["cross-dev", "build-docker-image"]
21 changes: 9 additions & 12 deletions .github/workflows/ci.yml
Expand Up @@ -54,7 +54,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: --locked -- -D warnings
args: --locked -- -D warnings --all-features

test:
runs-on: ${{ matrix.os }}
Expand All @@ -74,7 +74,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --locked --all-targets --workspace
args: --locked --all-targets --workspace --all-features
timeout-minutes: 5

generate-matrix:
Expand Down Expand Up @@ -206,6 +206,11 @@ jobs:
- name: Set up Docker Buildx
if: runner.os == 'Linux'
uses: docker/setup-buildx-action@v1

- name: Install cross
if: matrix.deploy
run: cargo install --path . --force --features=dev

- name: Docker Meta
if: runner.os == 'Linux'
id: docker-meta
Expand All @@ -221,7 +226,7 @@ jobs:
id: build-docker-image
if: runner.os == 'Linux'
timeout-minutes: 60
run: ./build-docker-image.sh "${TARGET}"
run: cross-dev build-docker-image "${TARGET}"
env:
TARGET: ${{ matrix.target }}
LABELS: ${{ steps.docker-meta.outputs.labels }}
Expand All @@ -247,14 +252,6 @@ jobs:
RUN: ${{ matrix.run }}
RUNNERS: ${{ matrix.runners }}
shell: bash

- name: Install cross
if: matrix.deploy
run: cargo install --path . --force

- name: Build cross-dev
run: cargo build --features=dev --bin cross-dev

- uses: ./.github/actions/cargo-install-upload-artifacts
if: matrix.deploy
with:
Expand Down Expand Up @@ -287,7 +284,7 @@ jobs:
github.ref == format('refs/heads/{0}', github.event.repository.default_branch) ||
startsWith(github.ref, 'refs/tags/v')
)
run: ./build-docker-image.sh --push "${TARGET}"
run: cross-dev build-docker-image --push "${TARGET}"
env:
TARGET: ${{ matrix.target }}
LABELS: ${{ steps.docker-meta2.outputs.labels }}
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Expand Up @@ -11,11 +11,11 @@ edition = "2021"

[features]
default = []
dev = ["serde_yaml"]
dev = ["serde_yaml", "walkdir"]

[dependencies]
atty = "0.2"
clap = { version = "3.1.18", features = ["derive"] }
clap = { version = "3.1.18", features = ["derive", "env"] }
color-eyre = "0.6"
eyre = "0.6"
home = "0.5"
Expand All @@ -28,6 +28,7 @@ serde_json = "1"
serde_yaml = { version = "0.8", optional = true }
serde_ignored = "0.1.2"
shell-words = "1.1.0"
walkdir = { version = "2", optional = true }

[target.'cfg(not(windows))'.dependencies]
nix = { version = "0.24", default-features = false, features = ["user"] }
Expand Down
106 changes: 0 additions & 106 deletions build-docker-image.sh

This file was deleted.

158 changes: 158 additions & 0 deletions cross-dev/build_docker_image.rs
@@ -0,0 +1,158 @@
use std::{path::Path, process::Command};

use clap::Args;
use cross::CommandExt;

#[derive(Args, Debug)]
pub struct BuildDockerImage {
#[clap(long, hide = true, env = "GITHUB_REF_TYPE")]
ref_type: Option<String>,
#[clap(long, hide = true, env = "GITHUB_REF_NAME")]
ref_name: Option<String>,
/// Newline separated labels
#[clap(long, env = "LABELS")]
labels: Option<String>,
/// Provide verbose diagnostic output.
#[clap(short, long)]
verbose: bool,
#[clap(long)]
force: bool,
#[clap(short, long)]
push: bool,
/// Container engine (such as docker or podman).
#[clap(long)]
pub engine: Option<String>,

/// Targets to build for
#[clap()]
targets: Vec<String>,
}

pub fn build_docker_image(
BuildDockerImage {
mut targets,
verbose,
force,
push,
ref_type,
ref_name,
labels,
..
}: BuildDockerImage,
engine: &Path,
) -> cross::Result<()> {
let metadata = cross::cargo_metadata_with_args(
Some(Path::new(env!("CARGO_MANIFEST_DIR"))),
None,
verbose,
)?
.ok_or_else(|| eyre::eyre!("could not find cross workspace and its current version"))?;
let version = metadata
.packages
.get(0)
.expect("one package expected in workspace")
.version
.clone();
if targets.is_empty() {
targets = walkdir::WalkDir::new(metadata.workspace_root.join("docker"))
.max_depth(1)
.contents_first(true)
.into_iter()
.filter_map(|e| e.ok().filter(|f| f.file_type().is_file()))
.filter_map(|f| {
(&f.file_name())
.to_string_lossy()
.strip_prefix("Dockerfile.")
.map(ToOwned::to_owned)
})
.collect();
}
for target in targets {
let mut docker_build = Command::new(engine);
docker_build.args(&["buildx", "build"]);
docker_build.current_dir(metadata.workspace_root.join("docker"));

if push {
docker_build.arg("--push");
} else {
docker_build.arg("--load");
}

let dockerfile = format!("Dockerfile.{target}");
let image_name = format!("ghcr.io/cross-rs/{target}");
let mut tags = vec![];

match (ref_type.as_deref(), ref_name.as_deref()) {
(Some(ref_type), Some(ref_name)) if ref_type == "tag" && ref_name.starts_with('v') => {
let tag_version = ref_name
.strip_prefix('v')
.expect("tag name should start with v");
if version != tag_version {
eyre::bail!("git tag does not match package version.")
}
tags.push(format!("{image_name}:{version}"));
// Check for unstable releases, tag stable releases as `latest`
if version.contains('-') {
// TODO: Don't tag if version is older than currently released version.
tags.push(format!("{image_name}:latest"))
}
}
(Some(ref_type), Some(ref_name)) if ref_type == "branch" => {
tags.push(format!("{image_name}:{ref_name}"));

if ["staging", "trying"]
.iter()
.any(|branch| branch == &ref_name)
{
tags.push(format!("{image_name}:edge"));
}
}
_ => {
if push {
eyre::bail!("Refusing to push without tag or branch. {ref_type:?}:{ref_name:?}")
}
tags.push(format!("{image_name}:local"))
}
}

docker_build.arg("--pull");
docker_build.args(&[
"--cache-from",
&format!("type=registry,ref={image_name}:main"),
]);

if push {
docker_build.args(&["--cache-to", "type=inline"]);
}

for tag in &tags {
docker_build.args(&["--tag", tag]);
}

for label in labels
.as_deref()
.unwrap_or("")
.split('\n')
.filter(|s| !s.is_empty())
{
docker_build.args(&["--label", label]);
}

docker_build.args(&["-f", &dockerfile]);
docker_build.args(&["--progress", "plain"]);
docker_build.arg(".");

if (force || push && std::env::var("GITHUB_ACTIONS").is_ok()) || !push {
docker_build.run(verbose)?;
} else {
docker_build.print_verbose(true);
}
if std::env::var("GITHUB_ACTIONS").is_ok() {
println!("::set-output name=image::{}", &tags[0])
}
}
if !(force || push && std::env::var("GITHUB_ACTIONS").is_ok()) {
println!("refusing to push, use --force to override");
}
Ok(())
}

0 comments on commit 23af727

Please sign in to comment.