update build steps

This commit is contained in:
DataHearth 2022-03-05 16:25:59 +01:00
parent 9ce1619a59
commit 4c55e8cfff
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
7 changed files with 57 additions and 76 deletions

View File

@ -1,5 +1,8 @@
target
Makefile
LICENSE LICENSE
README.md README.md
.gitignore target
Makefile
Earthfile
.gitignore
.git
.env

5
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target target
.DS_STORE .DS_STORE
.env

View File

@ -1,19 +0,0 @@
FROM rust:1.58 as builder
WORKDIR /app
COPY . .
RUN rustup target add x86_64-unknown-linux-musl
RUN cargo build --release --target x86_64-unknown-linux-musl
FROM docker:20.10.12-dind-alpine3.15
LABEL maintainer="Antoine <DataHearth> Langlois"
LABEL repository="https://github.com/DataHearth/clear-docker-images"
LABEL org.opencontainers.image.source=&quot;https://github.com/DataHearth/clear-docker-images&quot;
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/clear-docker-images /usr/local/bin/clear-docker-images
VOLUME ["/var/run/docker.sock"]
ENTRYPOINT ["clear-docker-images"]

32
Earthfile Normal file
View File

@ -0,0 +1,32 @@
VERSION 0.6
FROM rust:1.59
ARG VERSION
ARG DOCKER_IMG
ARG GHCR_IMG
WORKDIR /clear-docker-images
build-linux:
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-gnu
SAVE ARTIFACT target/x86_64-unknown-linux-gnu /x86_64-unknown-linux-gnu AS LOCAL target/x86_64-unknown-linux-gnu
build-images:
FROM docker:20.10.12-dind-alpine3.15
LABEL maintainer="Antoine <DataHearth> Langlois"
LABEL repository="https://github.com/DataHearth/clear-docker-images"
LABEL org.opencontainers.image.source=&quot;https://github.com/DataHearth/clear-docker-images&quot;
COPY +build-linux/x86_64-unknown-linux-gnu /usr/local/bin/clear-docker-images
VOLUME ["/var/run/docker.sock"]
ENTRYPOINT ["clear-docker-images"]
SAVE IMAGE ${DOCKER_IMG}:${VERSION}
SAVE IMAGE ${DOCKER_IMG}:latest
SAVE IMAGE ${GHCR_IMG}:${VERSION}
SAVE IMAGE ${GHCR_IMG}:latest

View File

@ -1,38 +1,9 @@
DOCKER_IMG := datahearth/clear-docker-images
GHCR_IMG := ghcr.io/${DOCKER_IMG}
VERSION := 0.4.1
.PHONY: initialize-linux-build
initialize-linux-build:
@brew install FiloSottile/musl-cross/musl-cross
@rustup target add x86_64-unknown-linux-musl
@echo 'add these lines to your "~/.cargo":'
@printf '\n[target.x86_64-unknown-linux-musl]\nlinker = "x86_64-linux-musl-gcc"\n'
.PHONY: build-docker
build-docker:
@docker build --tag ${DOCKER_IMG}:${VERSION} .
@docker tag ${DOCKER_IMG}:${VERSION} ${GHCR_IMG}:${VERSION}
@docker tag ${DOCKER_IMG}:${VERSION} ${DOCKER_IMG}:latest
@docker tag ${DOCKER_IMG}:${VERSION} ${GHCR_IMG}:latest
.PHONY: build-binaries
build-binaries:
@echo "Building Linux MUSL binary..."
@cargo build --release --target x86_64-unknown-linux-musl
@echo "Building MacOS darwin"
@cargo build --release --target x86_64-apple-darwin
.PHONY: push-docker-images
push-docker-images: build-docker
@docker push --all-tags ${GHCR_IMG}
@docker push --all-tags ${DOCKER_IMG}
.PHONY: bump-version .PHONY: bump-version
bump-version: bump-version:
@echo "Bump version \"${VERSION}\" to \"${NEW_VERSION}\"" @type sd > /dev/null
@echo "Bump version to \"${NEW_VERSION}\""
@sd "version = \"${VERSION}\"" "version = \"${NEW_VERSION}\"" Cargo.toml @sd "version = \"${VERSION}\"" "version = \"${NEW_VERSION}\"" Cargo.toml
@sd "VERSION := ${VERSION}" "VERSION := ${NEW_VERSION}" Makefile @sd "VERSION=${VERSION}" "VERSION=${NEW_VERSION}" .env
@git add . @git add .
@git commit -m "bump v${NEW_VERSION}" @git commit -m "bump v${NEW_VERSION}"
@git tag -m "bump v${NEW_VERSION}" v${NEW_VERSION} @git tag -m "bump v${NEW_VERSION}" v${NEW_VERSION}

View File

@ -31,21 +31,13 @@ pub fn process_imgs(
for img in parse_imgs(repository) { for img in parse_imgs(repository) {
let image: Image = serde_json::from_str(&img).unwrap(); let image: Image = serde_json::from_str(&img).unwrap();
let del = if let Some(max) = date_to { let del = date_to.map_or(
if image.created_at.timestamp() >= date_from.timestamp() image.created_at.timestamp() >= date_from.timestamp(),
&& image.created_at.timestamp() <= max.timestamp() |max| {
{ date_from.timestamp() >= image.created_at.timestamp()
true && max.timestamp() <= image.created_at.timestamp()
} else { },
false );
}
} else {
if image.created_at.timestamp() >= date_from.timestamp() {
true
} else {
false
}
};
if del { if del {
if !tags.contains(&image.tag) { if !tags.contains(&image.tag) {
@ -86,16 +78,17 @@ fn get_images(repo: Option<String>) -> Vec<u8> {
let mut cmd = Command::new(DOCKER_BIN); let mut cmd = Command::new(DOCKER_BIN);
cmd.arg("images"); cmd.arg("images");
if let Some(repo) = repo { repo.map(|repo| cmd.arg(repo));
cmd.arg(repo);
}
cmd.args(["--format", "{{json .}}"]); cmd.args(["--format", "{{json .}}"]);
match cmd.output() { match cmd.output() {
Ok(o) => { Ok(o) => {
if !o.status.success() { if !o.status.success() {
eprintln!("{}", std::str::from_utf8(&o.stderr).unwrap()); eprintln!(
"{}",
std::str::from_utf8(&o.stderr).expect("failed to parse STDERR to UTF-8")
);
eprintln!("failed to retrieve docker images. Please checkout STDERR"); eprintln!("failed to retrieve docker images. Please checkout STDERR");
exit(1); exit(1);
} }

View File

@ -42,7 +42,7 @@ struct Args {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let tags = if let Some(t) = args.tags { t } else { vec![] }; let tags = args.tags.map_or(vec![], |tags| tags);
let (ids, saved_size) = process_imgs(args.repository, tags, args.date); let (ids, saved_size) = process_imgs(args.repository, tags, args.date);
if args.dry_run { if args.dry_run {