Skip to content

Commit

Permalink
Add dockerfile for cross.
Browse files Browse the repository at this point in the history
This uses the installs Docker from the official sources using the latest Ubuntu image stable, and installs the latest stable Rust version internally. Cross is installed from the latest git (locked), which when tagged will work well with our releases. It exports the environment variable `CROSS_CONTAINER_IN_CONTAINER` so everything should work as expected.

Replaces cross-rs#667.
  • Loading branch information
Alexhuszagh committed Jun 29, 2022
1 parent e583132 commit 779c6ae
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 1 deletion.
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.default-test
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.default-test
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - ReleaseDate

### Added

- #878 - added a dockerfile containing cross.

### Changed

- #869 - ensure cargo configuration environment variable flags are passed to the docker container.
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
# shellcheck disable=SC2086

set -x
set -eo pipefail

if [[ -z "${TARGET}" ]]; then
export TARGET="aarch64-unknown-linux-gnu"
fi
if [[ -z "${CROSS_TARGET_CROSS_IMAGE}" ]]; then
CROSS_TARGET_CROSS_IMAGE="ghcr.io/cross-rs/cross:main"
fi

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 "${@}"
21 changes: 21 additions & 0 deletions docker/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM ubuntu:20.04 as rust
ARG DEBIAN_FRONTEND=noninteractive
COPY lib.sh cross.sh /
RUN /cross.sh

# 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 --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
23 changes: 23 additions & 0 deletions docker/cross.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC1091

set -x
set -euo pipefail

. lib.sh

main() {
install_packages ca-certificates curl gcc libc6-dev

# since we can't share outside the current docker context,
# we need to install from git, rather than from path
curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "$HOME"/.cargo/env
cargo install cross --git https://github.com/cross-rs/cross --locked

purge_packages

rm -rf "${0}"
}

main "${@}"
3 changes: 3 additions & 0 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ 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() {
gha_output("default-test", "true")
}
}
CiJob::Check { ref_type, ref_name } => {
let version = semver::Version::parse(&cross_meta.version)?;
Expand Down
5 changes: 5 additions & 0 deletions xtask/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ 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"
}
}

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

0 comments on commit 779c6ae

Please sign in to comment.