From 52a83b9336d4858534e875990c6e40e216071ce9 Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Mon, 24 Oct 2022 21:58:39 +0300 Subject: [PATCH] Scheduled job for Docker cleanup --- .gitlab-ci.yml | 63 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1f3308d..893ccd9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,10 +5,13 @@ stages: - test - publish - deploy + - cleanup install: image: node:14 stage: setup + only: + - pushes script: - npm ci artifacts: @@ -19,6 +22,8 @@ install: audit: image: python:3.9 stage: audit + only: + - pushes needs: [] before_script: - pip install poetry==1.1.13 @@ -30,6 +35,8 @@ audit: test: image: python:3.9 stage: test + only: + - pushes needs: [] services: - postgres:12 @@ -51,6 +58,8 @@ test: lint:py: image: python:3.9 stage: lint + only: + - pushes needs: [] script: - pip install black==22.3.0 @@ -59,6 +68,8 @@ lint:py: lint:js: image: node:14 stage: lint + only: + - pushes needs: ["install"] script: - npm run lint:js @@ -66,13 +77,15 @@ lint:js: lint:md: image: node:14 stage: lint + only: + - pushes needs: ["install"] script: - npm run lint:md publish: - stage: publish image: docker:stable + stage: publish needs: ["test", "lint:py", "lint:js", "lint:md"] services: - docker:stable-dind @@ -86,8 +99,8 @@ publish: - docker push "$IMAGE_NAME" deploy:dev: - stage: deploy image: docker:stable + stage: deploy only: - develop environment: @@ -128,4 +141,48 @@ deploy:production: script: - docker stack deploy --with-registry-auth -c stack-compose.yml "$SERVICE_NAME" after_script: - - docker logout "$CI_REGISTRY" \ No newline at end of file + - docker logout "$CI_REGISTRY" + +docker_prune:dev: + image: docker:stable + stage: cleanup + only: + - schedules + environment: + name: dev + url: http://api.dev.sahkoinsinoorikilta.fi + variables: + DOCKER_HOST: $DEV_CI_DOCKER_HOST + DOCKER_TLS_VERIFY: 1 + before_script: + - mkdir -p ~/.docker + - echo "$DEV_TLSCACERT" > ~/.docker/ca.pem + - echo "$DEV_TLSCERT" > ~/.docker/cert.pem + - echo "$DEV_TLSKEY" > ~/.docker/key.pem + - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY" + script: + - docker system prune + after_script: + - docker logout "$CI_REGISTRY" + +docker_prune:prod: + image: docker:stable + stage: cleanup + only: + - schedules + environment: + name: production + url: https://api.sahkoinsinoorikilta.fi + variables: + DOCKER_HOST: $CI_DOCKER_HOST + DOCKER_TLS_VERIFY: 1 + before_script: + - mkdir -p ~/.docker + - echo "$TLSCACERT" > ~/.docker/ca.pem + - echo "$TLSCERT" > ~/.docker/cert.pem + - echo "$TLSKEY" > ~/.docker/key.pem + - docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY" + script: + - docker system prune + after_script: + - docker logout "$CI_REGISTRY"