Skip to content

Commit

Permalink
CI -> Publish docker image on release builds
Browse files Browse the repository at this point in the history
  • Loading branch information
reimarstier committed Apr 11, 2024
1 parent d65c11e commit 1028d71
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions .ci/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license.workspace = true
anyhow = { workspace = true }
assert_fs = { workspace = true }
cargo_metadata = { workspace = true }
chrono = { workspace = true, default-features = true }
clap = { workspace = true, features = ["derive", "string"] }
flate2 = { workspace = true }
fs_extra = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions .ci/xtask/src/core/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cargo_metadata::Package;
use lazy_static::lazy_static;

lazy_static! {
Expand All @@ -12,3 +13,10 @@ lazy_static! {
pub fn cargo() -> cargo_metadata::Metadata {
CARGO_METADATA.to_owned()
}

pub fn repository_url() -> String {
let carl_package: Package = cargo().workspace_packages().into_iter()
.find(|&package| package.name == "opendut-carl").cloned()
.expect("Could not extract repository url for package opendut-carl from opendut-carl/Cargo.toml.").to_owned();
carl_package.repository.unwrap()
}
5 changes: 5 additions & 0 deletions .ci/xtask/src/packages/carl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct CarlCli {
#[derive(clap::Subcommand)]
pub enum TaskCli {
Distribution(crate::tasks::distribution::DistributionCli),
Docker(crate::tasks::docker::DockerCli),
Licenses(crate::tasks::licenses::LicensesCli),
Run(crate::tasks::run::RunCli),

Expand Down Expand Up @@ -62,6 +63,10 @@ impl CarlCli {
distribution::validate::validate_contents(target)?;
}
}
TaskCli::Docker(crate::tasks::docker::DockerCli {}) => {
crate::tasks::docker::build_carl_docker_image()?;
crate::tasks::docker::publish_carl_docker_image()?;
}
};
Ok(())
}
Expand Down
58 changes: 58 additions & 0 deletions .ci/xtask/src/tasks/docker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::process::Command;

use crate::core::constants::workspace_dir;
use crate::core::types::Package;
use crate::core::util::RunRequiringSuccess;

/// Build and publish a Docker image
#[derive(Debug, clap::Parser)]
pub struct DockerCli {}

const OPENDUT_DOCKER_IMAGE_HOST: &str = "ghcr.io";
const OPENDUT_DOCKER_IMAGE_NAMESPACE: &str = "eclipse-opendut";
fn carl_container_uri() -> String {
let image_host = std::env::var("OPENDUT_DOCKER_IMAGE_HOST").unwrap_or(OPENDUT_DOCKER_IMAGE_HOST.to_string());
let image_namespace = std::env::var("OPENDUT_DOCKER_IMAGE_NAMESPACE").unwrap_or(OPENDUT_DOCKER_IMAGE_NAMESPACE.to_string());
let image_uri = format!("{}/{}/{}:{}", image_host, image_namespace, Package::Carl.ident(), crate::build::PKG_VERSION);
image_uri
}

pub fn build_carl_docker_image() -> crate::Result {
let image_version_build_arg = format!("VERSION={}", crate::build::PKG_VERSION);
let now = chrono::Utc::now().naive_utc();

// https://github.com/opencontainers/image-spec/blob/main/annotations.md
let source = format!("org.opencontainers.image.source={}", crate::core::metadata::repository_url());
let url = format!("org.opencontainers.image.url={}", carl_container_uri());
let version = format!("org.opencontainers.image.version={}", crate::build::PKG_VERSION);
let created = format!("org.opencontainers.image.created={}", now);
let revision = format!("org.opencontainers.image.revision={}", crate::build::COMMIT_HASH);

Command::new("docker")
.current_dir(workspace_dir())
.args([
"build",
"--file",
".ci/docker/carl/Dockerfile",
"--build-arg",
&image_version_build_arg,
"--label", &source,
"--label", &url,
"--label", &version,
"--label", &created,
"--label", &revision,
"--tag",
&carl_container_uri(),
".",
])
.run_requiring_success()?;
Ok(())
}

pub fn publish_carl_docker_image() -> crate::Result {
Command::new("docker")
.current_dir(workspace_dir())
.args(["push", &carl_container_uri()])
.run_requiring_success()?;
Ok(())
}
1 change: 1 addition & 0 deletions .ci/xtask/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub mod build;
pub mod check;
pub mod distribution;
pub mod doc;
pub mod docker;
pub mod licenses;
pub mod run;
31 changes: 31 additions & 0 deletions .github/workflows/job-publish-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: publish-docker
on:
workflow_call: # allow this workflow to be called from other workflows
inputs:
runs-on:
default: "['ubuntu-latest']"
required: false
type: string

jobs:
publish-docker:
name: "Publish docker"
runs-on: ${{ fromJson(inputs.runs-on) }}
permissions:
contents: write
packages: write

steps:
- name: Checkout Sources
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
- name: Rust setup
uses: ./.github/actions/rust-setup

- name: Download opendut-CARL bundle
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935
with:
name: "opendut-carl-x86_64-unknown-linux-gnu-${{ github.sha }}.tar.gz"
path: "./target/ci/distribution/x86_64-unknown-linux-gnu/"

- name: Publish Docker image
run: cargo ci carl docker
9 changes: 9 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,12 @@ jobs:
contents: write
with:
runs-on: "${{ vars.OPENDUT_GH_RUNNER_SMALL || '[\"ubuntu-latest\"]' }}"

publish-docker:
needs: [ bundle-carl ]
uses: ./.github/workflows/job-publish-docker.yml
permissions:
contents: write
packages: write
with:
runs-on: "${{ vars.OPENDUT_GH_RUNNER_SMALL || '[\"ubuntu-latest\"]' }}"
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ resolver = "2"
edition = "2021"
rust-version = "1.76"
license = "Apache-2.0"
repository = "https://github.com/eclipse-opendut/opendut"

[workspace.dependencies]
opendut-carl = { path = "opendut-carl" }
Expand Down
1 change: 1 addition & 0 deletions opendut-carl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
opendut-carl-api = { workspace = true, features = ["oidc_client"] }
Expand Down

0 comments on commit 1028d71

Please sign in to comment.