deb.rs
1 #![allow(clippy::result_large_err)] 2 3 use std::path::{Path, PathBuf}; 4 5 use serde::{Deserialize, Serialize}; 6 7 use crate::{ 8 action::{ActionError, Context}, 9 action_impl::{spawn, ActionImpl}, 10 qemu, 11 }; 12 13 /// Build a `deb` package. 14 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] 15 pub struct Deb { 16 packages: Option<PathBuf>, 17 } 18 19 impl Deb { 20 /// Create a new `Deb` action. 21 pub fn new<P: AsRef<Path>>(packages: P) -> Self { 22 Self { 23 packages: Some(packages.as_ref().to_path_buf()), 24 } 25 } 26 } 27 28 impl ActionImpl for Deb { 29 fn execute(&self, context: &mut Context) -> Result<(), ActionError> { 30 let packages = Path::new(qemu::ARTIFACTS_DIR) 31 .join(self.packages.clone().unwrap_or(PathBuf::from("."))); 32 if !packages.exists() { 33 std::fs::create_dir(&packages).map_err(|err| DebError::mkdir(&packages, err))?; 34 } 35 let shell = format!( 36 r#"#!/usr/bin/env bash 37 set -xeuo pipefail 38 39 echo "PATH at start: $PATH" 40 export PATH="/root/.cargo/bin:$PATH" 41 export CARGO_HOME=/ci/deps 42 export DEBEMAIL=liw@liw.fi 43 export DEBFULLNAME="Lars Wirzenius" 44 /bin/env 45 46 command -v cargo 47 command -v rustc 48 49 cargo --version 50 rustc --version 51 52 # Get name and version of source package. 53 name="$(dpkg-parsechangelog -SSource)" 54 version="$(dpkg-parsechangelog -SVersion)" 55 56 # Get upstream version: everything before the last dash. 57 uv="$(echo "$version" | sed 's/-[^-]*$//')" 58 59 # Files that will be created. 60 arch="$(dpkg --print-architecture)" 61 orig="../${{name}}_${{uv}}.orig.tar.xz" 62 deb="../${{name}}_${{version}}_${{arch}}.deb" 63 changes="../${{name}}_${{version}}_${{arch}}.changes" 64 65 # Create "upstream tarball". 66 git archive HEAD | xz >"$orig" 67 68 # Build package. 69 dpkg-buildpackage -us -uc 70 71 # Dump some information to make it easier to visually verify 72 # everything looks OK. Also, test the package with the lintian tool. 73 74 ls -l .. 75 for x in ../*.deb; do dpkg -c "$x"; done 76 # FIXME: disabled while this prevents radicle-native-ci deb from being built. 77 # lintian -i --allow-root --fail-on warning ../*.changes 78 79 # Move files to artifacts directory. 80 mv ../*_* {} 81 "#, 82 packages.display() 83 ); 84 85 spawn(context, &["/bin/bash", "-c", &shell])?; 86 87 Ok(()) 88 } 89 } 90 91 /// Errors from the `deb` action. 92 #[derive(Debug, thiserror::Error)] 93 pub enum DebError { 94 /// Failed to create the artifacts directory for `deb` packages. 95 #[error("could not create artifacts directory {0}")] 96 Mkdir(PathBuf, #[source] std::io::Error), 97 } 98 99 impl DebError { 100 fn mkdir<P: Into<PathBuf>>(dirname: P, err: std::io::Error) -> Self { 101 Self::Mkdir(dirname.into(), err) 102 } 103 } 104 105 impl From<DebError> for ActionError { 106 fn from(value: DebError) -> Self { 107 Self::Deb(value) 108 } 109 }