#gitlab #runner

gitlab-runner

Helper crate to build custom gitlab runners

8 releases

Uses new Rust 2021

0.0.7 Oct 18, 2022
0.0.6 Aug 11, 2022
0.0.5 May 16, 2022
0.0.4 Apr 27, 2022
0.0.1 Sep 26, 2021
Download history 11/week @ 2022-07-31 60/week @ 2022-08-07 17/week @ 2022-08-14 18/week @ 2022-08-21 5/week @ 2022-08-28 18/week @ 2022-09-04 19/week @ 2022-09-11 40/week @ 2022-09-18 12/week @ 2022-09-25 21/week @ 2022-10-02 11/week @ 2022-10-09 52/week @ 2022-10-16 12/week @ 2022-10-23 260/week @ 2022-10-30 19/week @ 2022-11-06 4/week @ 2022-11-13

296 downloads per month

MIT/Apache

61KB
1.5K SLoC

gitlab-runner-rs

A crate to help build custom gitlab runner implementations.

Implementing a custom runner

The overall idea is that this crate handles all interaction with the gitlab server and drives the executions while the the runner implementation focus on how to handle jobs from gitlab.

As the focus for an implementer of a custom runner is to implement the async JobHandler trait, which gets calle during job executation. An absolute minimal runner can be implement as such:

use gitlab_runner::{outputln, Runner, JobHandler, JobResult, Phase};
use std::path::PathBuf;

#[derive(Debug)]
struct Run {}

#[async_trait::async_trait]
impl JobHandler for Run {
      async fn step(&mut self, script: &[String], phase: Phase) -> JobResult {
          outputln!("Running script for phase {:?}", phase);
          for s in script {
            outputln!("Step: {}", s);
          }
          Ok(())
      }
}

#[tokio::main]
async fn main() {
    let mut runner = Runner::new(
        "https://gitlab.example.com".try_into().unwrap(),
        "runner token".to_owned(),
        PathBuf::from("/tmp"));
    runner.run(move | _job | async move { Ok(Run{})  }, 16).await.unwrap();
}

Gitlab runner registration

This crate does not support registering new runners with the gitlab server, so this has to be done by hand using the gitlab runner registration API.

The registration token can be retrieved from the runners section in the Gitlab administration area. With that token the runner can be register using a curl command like:

curl --request POST "https://GITLAB_URL/api/v4/runners"  \
  --form "description=My custom runner" \
  --form "run_untagged=false" \
  --form "tag_list=custom-gitlab-runner" \
  --form "token=REGISTRATION_TOKEN"

As a response to this command a new token for the registered runner will be provided, this token should be provided to the runner for it's gitlab connection.

One thing to key parameter provided here is run_untagged=false, which will make the runner only pickup jobs which matches its tag. This is important to prevent the runner from picking up "normal" jobs which it will not be able to process.

Dependencies

~12–21MB
~395K SLoC