Compare commits

...

5 Commits

Author SHA1 Message Date
DataHearth 9e3b09d662
bump v0.5.0 2022-03-17 00:02:20 +01:00
DataHearth a64d2f56f2
add earthly container image stage 2022-03-16 23:59:33 +01:00
DataHearth ffe439d698
update readme 2022-03-16 23:59:33 +01:00
DataHearth 2d5714c9c6
use docker socket 2022-03-16 23:59:07 +01:00
DataHearth 042be476e0
invert condition for empty tags 2022-03-16 13:59:16 +01:00
7 changed files with 94 additions and 27 deletions

View File

@ -7,6 +7,7 @@ on:
jobs:
build-linux-musl:
name: Build Linux MUSL artefact
runs-on: ubuntu-latest
steps:
- name: Checkout
@ -32,6 +33,7 @@ jobs:
path: ./target/release/clear-docker-images
build-linux-gnu:
name: Build Linux GNU artefact
runs-on: ubuntu-latest
steps:
- name: Checkout
@ -57,6 +59,7 @@ jobs:
path: ./target/release/clear-docker-images
build-macos:
name: Build MacOS artefact
runs-on: macos-latest
steps:
- name: Checkout
@ -81,12 +84,47 @@ jobs:
name: x86_64-apple-darwin
path: ./target/release/clear-docker-images
container-images:
name: Create and deploy container images
runs-on: ubuntu-latest
steps:
- name: Setup Earthly
uses: earthly/actions-setup@v1
with:
version: "latest"
- name: Checkout
uses: actions/checkout@v3
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get current tag
id: tags
run: echo ::set-output name=CURRENT_TAG::${GITHUB_REF/refs\/tags\//}
- name: build and push
run: earthly --build-arg VERSION=${{ steps.tags.outputs.CURRENT_TAG }} --build-arg DOCKER_IMG=datahearth/clear-docker-images --build-arg GHCR_IMG=ghcr.io/datahearth/clear-docker-images --push +build-images
release:
name: Create release and upload artefacts
runs-on: ubuntu-latest
needs: [build-macos, build-linux-gnu, build-linux-musl]
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- run: mkdir -p binaries

2
Cargo.lock generated
View File

@ -130,7 +130,7 @@ dependencies = [
[[package]]
name = "clear-docker-images"
version = "0.4.2"
version = "0.5.0"
dependencies = [
"bollard",
"chrono",

View File

@ -1,6 +1,6 @@
[package]
name = "clear-docker-images"
version = "0.4.2"
version = "0.5.0"
edition = "2021"
authors = ["Antoine <DataHearth> Langlois"]
description = "small binary to clean up docker images (mostly in CI)"

View File

@ -1,4 +1,4 @@
VERSION := 0.4.2
VERSION := 0.5.0
.PHONY: bump-version
bump-version:

View File

@ -12,6 +12,8 @@ By default, `clear-docker-images` will select images that are older than 2 days
docker run --name clear-docker-image -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/datahearth/clear-docker-images <OPTIONS>
```
*N.B: Since v0.5.0, container images switched from the Docker alpine docker-in-docker image to Debian 11 distroless. I recommand switching to v0.5.0 for a significant size gain (around x20 in term of image size => 230MB).*
### Binary
```bash
@ -32,7 +34,7 @@ git clone https://github.com/DataHearth/clear-docker-images.git
cd clear-docker-images
cargo install --path .
clear-docker-images <OPTIONS>
clear-docker-images <REPOSITORY> <OPTIONS>
```
## Options
@ -55,18 +57,20 @@ OPTIONS:
--dry-run
image cleanup will not be triggered [default: false]
--force
force image removal [default: false]
-f, --force
should force image deletion [default: false]
-h, --help
Print help information
-s, --socket <SOCKET>
where is located the docker socket (can be a UNIX socket or TCP protocol)
[default: /var/run/docker.sock]
-t, --tags <TAGS>
add tags exclusion
-v, --verbose
add more logs [default: false]
-V, --version
Print version information
```

View File

@ -1,4 +1,5 @@
use bollard::image::ListImagesOptions;
use bollard::image::RemoveImageOptions;
use bollard::models::ImageSummary;
use bollard::Docker;
use bollard::API_DEFAULT_VERSION;
@ -7,9 +8,6 @@ use std::collections::HashMap;
use crate::DateArgs;
const GHCR_REPO: &str = "ghcr.io/datahearth/clear-docker-images";
const DOCKER_REPO: &str = "datahearth/clear-docker-images";
pub struct DockerActions {
docker: Docker,
repository: Option<String>,
@ -23,13 +21,15 @@ impl DockerActions {
repository: Option<String>,
tags: Vec<String>,
date: DateArgs,
) -> Self {
Self {
docker: Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION).unwrap(),
) -> Result<Self, bollard::errors::Error> {
let docker = Docker::connect_with_socket(&socket, 120, API_DEFAULT_VERSION)?;
Ok(Self {
docker,
repository,
tags,
date,
}
})
}
pub async fn get(&self) -> Result<Vec<ImageSummary>, bollard::errors::Error> {
@ -52,6 +52,7 @@ impl DockerActions {
pub async fn delete(
&self,
images: Vec<ImageSummary>,
force: bool,
dry_run: bool,
) -> Result<i64, bollard::errors::Error> {
let mut removed_size = 0;
@ -59,7 +60,19 @@ impl DockerActions {
info!("deleting: {}", image.id);
if !dry_run {
if let Err(e) = self.docker.delete_service(&image.id).await {
let res = self
.docker
.remove_image(
&image.id,
Some(RemoveImageOptions {
force,
..Default::default()
}),
None,
)
.await;
if let Err(e) = res {
return Err(e);
}
}
@ -81,15 +94,14 @@ impl DockerActions {
self.date.start > image.created && image.created > stop
})
&& image.repo_tags.iter().any(|tag| {
!tag.contains(GHCR_REPO)
&& !tag.contains(DOCKER_REPO)
&& self
!tag.contains("ghcr.io/datahearth/clear-docker-images")
&& !tag.contains("datahearth/clear-docker-images")
&& !self
.tags
.iter()
.any(|excluded_tag| !tag.contains(excluded_tag))
.any(|excluded_tag| tag.contains(excluded_tag))
})
{
println!("{:?}", self.tags);
to_be_deleted.push(image);
}
}

View File

@ -31,6 +31,10 @@ struct Args {
#[clap(long, takes_value = false)]
dry_run: bool,
/// should force image deletion [default: false]
#[clap(short, long, takes_value = false)]
force: bool,
/// where is located the docker socket (can be a UNIX socket or TCP protocol)
#[clap(short, long, default_value = "/var/run/docker.sock")]
socket: String,
@ -54,7 +58,7 @@ async fn main() {
exit(1);
}
let actions = DockerActions::new(
let actions = match DockerActions::new(
args.socket,
args.repository,
args.tags.map_or(vec![], |t| t),
@ -65,7 +69,13 @@ async fn main() {
},
|d| d,
),
);
) {
Ok(d) => d,
Err(e) => {
error!("failed to connect to docker socket: {}", e);
exit(1);
}
};
let images = match actions.get().await {
Ok(i) => i,
@ -75,10 +85,13 @@ async fn main() {
}
};
let saved = match actions.delete(actions.filter(images), args.dry_run).await {
let saved = match actions
.delete(actions.filter(images), args.force, args.dry_run)
.await
{
Ok(s) => s,
Err(e) => {
error!("failed to retrieve docker images: {}", e);
error!("failed to delete docker images: {}", e);
exit(1);
}
};