Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dockerfile for cross. #878

Merged
merged 1 commit into from
Jul 5, 2022
Merged
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
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ jobs:
- { target: thumbv7em-none-eabi, os: ubuntu-latest, std: 1 }
- { target: thumbv7em-none-eabihf, os: ubuntu-latest, std: 1 }
- { target: thumbv7m-none-eabi, os: ubuntu-latest, std: 1 }
- { target: cross, os: ubuntu-latest }

build:
name: target (${{ matrix.pretty }},${{ matrix.os }})
Expand Down Expand Up @@ -283,7 +284,7 @@ jobs:
IMAGE: ${{ steps.build-docker-image.outputs.image }}
shell: bash
- name: Test Image
if: steps.prepare-meta.outputs.has-image
if: steps.prepare-meta.outputs.has-image && steps.prepare-meta.outputs.test-variant == 'default'
run: ./ci/test.sh
env:
TARGET: ${{ matrix.target }}
Expand All @@ -300,6 +301,13 @@ jobs:
target: ${{ matrix.target }}
image: ${{ steps.build-docker-image.outputs.image }}

- name: Test Cross Image
if: steps.prepare-meta.outputs.has-image && steps.prepare-meta.outputs.test-variant == 'cross'
run: ./ci/test-cross-image.sh
env:
TARGET: 'aarch64-unknown-linux-gnu'
shell: bash

- name: Login to GitHub Container Registry
if: steps.prepare-meta.outputs.has-image
uses: docker/login-action@v1
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #900 - add the option to skip copying build artifacts back to host when using remote cross via `CROSS_REMOTE_SKIP_BUILD_ARTIFACTS`.
- #891 - support custom user namespace overrides by setting the `CROSS_CONTAINER_USER_NAMESPACE` environment variable.
- #890 - support rootless docker via the `CROSS_ROOTLESS_CONTAINER_ENGINE` environment variable.
- #878 - added an image `ghcr.io/cross-rs/cross` containing cross.

### Changed

Expand Down
26 changes: 26 additions & 0 deletions ci/test-cross-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this so we can test docker-in-docker scenarios where the CROSS_CONTAINER_IN_CONTAINER envvar has already been set.

# shellcheck disable=SC2086

set -x
set -eo pipefail

if [[ -z "${TARGET}" ]]; then
export TARGET="aarch64-unknown-linux-gnu"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always have this image, so it's a sensible target.

fi
if [[ -z "${CROSS_TARGET_CROSS_IMAGE}" ]]; then
CROSS_TARGET_CROSS_IMAGE="ghcr.io/cross-rs/cross:main"
fi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way we use the CI default if present, but have a reasonable target if testing our CI configurations locally.


main() {
docker run --rm -e TARGET \
-v /var/run/docker.sock:/var/run/docker.sock \
"${CROSS_TARGET_CROSS_IMAGE}" sh -c '
#!/usr/bin/env sh
td="$(mktemp -d)"
git clone --depth 1 https://github.com/cross-rs/rust-cpp-hello-word "${td}"
cd "${td}"
cross run --target "${TARGET}"
'
}

main "${@}"
22 changes: 22 additions & 0 deletions docker/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ubuntu:20.04 as rust
ARG DEBIAN_FRONTEND=noninteractive
COPY docker/lib.sh docker/cross.sh /
COPY ./ /project
RUN /cross.sh /project

# we build our images in 2 steps, to ensure we have a compact
# image, since we want to add our current subdirectory
FROM ubuntu:20.04
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to Ubuntu as a base so it's consistent with all our other images.

COPY --from=rust /root/.cargo /root/.cargo
COPY --from=rust /root/.rustup /root/.rustup

# need some basic devtools, and requirements for docker
RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
ca-certificates \
curl \
git

RUN curl -fsSL https://get.docker.com | sh

ENV CROSS_CONTAINER_IN_CONTAINER=1 \
PATH=/root/.cargo/bin:$PATH
24 changes: 24 additions & 0 deletions docker/cross.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC1091

set -x
set -euo pipefail

. lib.sh

main() {
local project_dir="${1}"

install_packages ca-certificates curl gcc libc6-dev

cd "${project_dir}"
curl --proto "=https" --tlsv1.2 --retry 3 -sSfL https://sh.rustup.rs | sh -s -- -y
source "${HOME}"/.cargo/env
cargo install --path . --locked

purge_packages

rm -rf "${0}"
}

main "${@}"
9 changes: 7 additions & 2 deletions xtask/src/build_docker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ pub fn build_docker_image(
}
}
let gha = std::env::var("GITHUB_ACTIONS").is_ok();
let docker_root = metadata.workspace_root.join("docker");
let root = metadata.workspace_root;
let docker_root = root.join("docker");
let cross_toolchains_root = docker_root.join("cross-toolchains").join("docker");
let targets = targets
.into_iter()
Expand Down Expand Up @@ -237,7 +238,11 @@ pub fn build_docker_image(
docker_build.args(&["--build-arg", arg]);
}

docker_build.arg(".");
if target.needs_workspace_root_context() {
docker_build.arg(&root);
} else {
docker_build.arg(".");
}

if !dry_run && (force || !push || gha) {
let result = docker_build.run(msg_info, false);
Expand Down
14 changes: 10 additions & 4 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
// Set labels
let mut labels = vec![];

labels.push(format!(
"org.opencontainers.image.title=cross (for {})",
target.triplet
));
let image_title = match target.triplet.as_ref() {
"cross" => target.triplet.to_string(),
_ => format!("cross (for {})", target.triplet),
};
labels.push(format!("org.opencontainers.image.title={image_title}"));
labels.push(format!(
"org.opencontainers.image.licenses={}",
cross_meta.license.as_deref().unwrap_or_default()
Expand All @@ -69,6 +70,11 @@ pub fn ci(args: CiJob, metadata: CargoMetadata) -> cross::Result<()> {
if target.has_ci_image() {
gha_output("has-image", "true")
}
if target.is_default_test_image() {
Alexhuszagh marked this conversation as resolved.
Show resolved Hide resolved
gha_output("test-variant", "default")
} else {
gha_output("test-variant", &target.triplet)
}
}
CiJob::Check { ref_type, ref_name } => {
let version = semver::Version::parse(&cross_meta.version)?;
Expand Down
10 changes: 10 additions & 0 deletions xtask/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ impl ImageTarget {
.iter()
.any(|m| m.builds_image() && m.target == self.triplet && m.sub == self.sub)
}

/// Determine if this target uses the default test script
pub fn is_default_test_image(&self) -> bool {
self.triplet != "cross"
}

/// Determine if this target needs to interact with the project root.
pub fn needs_workspace_root_context(&self) -> bool {
self.triplet == "cross"
}
}

impl std::str::FromStr for ImageTarget {
Expand Down