Skip to content

Commit

Permalink
Inject current and last successful git sha into action envs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bittrance committed Oct 2, 2023
1 parent b8348b5 commit 95c8502
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The plan forward, roughly in falling priority:
- [x] allow configuring notification actions
- [x] proper options validation (e.g. config-file xor url/action)
- [x] specialized notification action to update github status
- [ ] new git sha and branch name in action env vars
- [x] new git sha and branch name in action env vars
- [ ] changed task config should override state loaded from disk
- [ ] docker packaging
- [ ] readme with design and deployment options
Expand Down
4 changes: 4 additions & 0 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl Action {
pub fn id(&self) -> String {
self.name.clone()
}

pub fn set_env(&mut self, key: String, val: String) {
self.environment.insert(key, val);
}
}

impl TryFrom<&CliOptions> for Action {
Expand Down
10 changes: 8 additions & 2 deletions src/task/gixworkload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Workload for GitWorkload {
self.config.interval
}

fn perform(&self, workdir: PathBuf, current_sha: ObjectId) -> Result<ObjectId, GitOpsError> {
fn perform(mut self, workdir: PathBuf, current_sha: ObjectId) -> Result<ObjectId, GitOpsError> {
let deadline = Instant::now() + self.config.timeout;
let watchers = self.watchers.clone();
let sink = Arc::new(Mutex::new(move |event: WorkloadEvent| {
Expand All @@ -81,9 +81,15 @@ impl Workload for GitWorkload {
}
Ok::<_, GitOpsError>(())
}));

let new_sha = ensure_worktree(&self.config.git, deadline, &self.repo_dir, &workdir)?;
if current_sha != new_sha {
self.config.actions.iter_mut().for_each(|action| {
action.set_env(
"KITOPS_LAST_SUCCESSFUL_SHA".to_string(),
current_sha.to_string(),
);
action.set_env("KITOPS_SHA".to_string(), new_sha.to_string());
});
sink.lock().unwrap()(WorkloadEvent::Changes(
self.config.name.clone(),
current_sha,
Expand Down
2 changes: 1 addition & 1 deletion src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod scheduled;
pub trait Workload {
fn id(&self) -> String;
fn interval(&self) -> Duration;
fn perform(&self, workdir: PathBuf, current_sha: ObjectId) -> Result<ObjectId, GitOpsError>;
fn perform(self, workdir: PathBuf, current_sha: ObjectId) -> Result<ObjectId, GitOpsError>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Workload for TestWorkload {
Duration::from_secs(1)
}

fn perform(&self, _workdir: PathBuf, _current_sha: ObjectId) -> Result<ObjectId, GitOpsError> {
fn perform(self, _workdir: PathBuf, _current_sha: ObjectId) -> Result<ObjectId, GitOpsError> {
self.status
.store(true, std::sync::atomic::Ordering::Relaxed);
sleep(Duration::from_millis(10));
Expand Down
48 changes: 42 additions & 6 deletions tests/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use gix::{hash::Kind, ObjectId};
use kitops::{
errors::GitOpsError,
opts::CliOptions,
receiver::WorkloadEvent,
receiver::{SourceType, WorkloadEvent},
task::{gixworkload::GitWorkload, GitTaskConfig, Workload},
};
use utils::*;
Expand All @@ -16,7 +16,7 @@ fn cli_options(repodir: &tempfile::TempDir) -> CliOptions {
CliOptions::parse_from(&["kitops", "--repo-dir", &repodir.path().to_str().unwrap()])
}

fn config(upstream: &tempfile::TempDir, entrypoint: &str) -> GitTaskConfig {
fn config(upstream: &tempfile::TempDir, entrypoint: &str, args: &[&str]) -> GitTaskConfig {
serde_yaml::from_str(&format!(
r#"
name: ze-task
Expand All @@ -25,9 +25,11 @@ git:
actions:
- name: ze-action
entrypoint: {}
args: {}
"#,
upstream.path().to_str().unwrap(),
entrypoint
entrypoint,
serde_json::to_string(args).unwrap()
))
.unwrap()
}
Expand Down Expand Up @@ -57,7 +59,7 @@ fn watch_successful_workload() {
let next_sha = ObjectId::from_hex(next_sha.as_bytes()).unwrap();
let workdir = tempfile::tempdir().unwrap();
let opts = cli_options(&repodir);
let config = config(&upstream, "/bin/ls");
let config = config(&upstream, "/bin/ls", &[]);
let mut workload = GitWorkload::from_config(config, &opts);
let events = Arc::new(Mutex::new(Vec::new()));
let events2 = events.clone();
Expand Down Expand Up @@ -85,7 +87,7 @@ fn watch_failing_workload() {
let repodir = tempfile::tempdir().unwrap();
let workdir = tempfile::tempdir().unwrap();
let opts = cli_options(&repodir);
let config = config(&upstream, "/usr/bin/false");
let config = config(&upstream, "/usr/bin/false", &[]);
let mut workload = GitWorkload::from_config(config, &opts);
let events = Arc::new(Mutex::new(Vec::new()));
let events2 = events.clone();
Expand All @@ -110,7 +112,7 @@ fn watch_erroring_workload() {
let repodir = tempfile::tempdir().unwrap();
let workdir = tempfile::tempdir().unwrap();
let opts = cli_options(&repodir);
let config = config(&upstream, "/no/such/file");
let config = config(&upstream, "/no/such/file", &[]);
let mut workload = GitWorkload::from_config(config, &opts);
let events = Arc::new(Mutex::new(Vec::new()));
let events2 = events.clone();
Expand All @@ -126,3 +128,37 @@ fn watch_erroring_workload() {
assert!(matches!(events[0], WorkloadEvent::Changes(..)));
assert!(matches!(events[1], WorkloadEvent::Error(..)));
}

#[cfg(unix)]
#[test]
fn woarkload_gets_sha_env() {
let sh = shell();
let upstream = empty_repo(&sh);
let next_sha = commit_file(&upstream, "revision 1");
let repodir = tempfile::tempdir().unwrap();
let next_sha = ObjectId::from_hex(next_sha.as_bytes()).unwrap();
let workdir = tempfile::tempdir().unwrap();
let opts = cli_options(&repodir);
let config = config(&upstream, "/bin/sh", &["-c", "echo $KITOPS_SHA"]);
let mut workload = GitWorkload::from_config(config, &opts);
let events = Arc::new(Mutex::new(Vec::new()));
let events2 = events.clone();
workload.watch(move |event| {
events2.lock().unwrap().push(event);
Ok(())
});
let prev_sha = ObjectId::empty_tree(Kind::Sha1);
workload.perform(workdir.into_path(), prev_sha).unwrap();
assert_eq!(
events
.lock()
.unwrap()
.iter()
.find(|e| matches!(e, WorkloadEvent::ActionOutput(..))),
Some(&WorkloadEvent::ActionOutput(
"ze-task|ze-action".to_string(),
SourceType::StdOut,
format!("{}\n", next_sha).into_bytes(),
))
);
}

0 comments on commit 95c8502

Please sign in to comment.