Coder Social home page Coder Social logo

github-action-container-cleanup's Introduction

github-action-container-cleanup

Github Action to cleanup containers created by the lifetime of a workflow Job

Motivation

For self-hosted runners, there might be job workflows that can potentially launch containers that will park in certain ports or container names and prevent subsequent runs of the same job or other jobs that use thsoe ports or names to work.

Moreover, self-hosted runners might well be monitored by solutions such as Prometheus with relies on exporters such as node-exporter or cadvisor that run on docker containers. Therefore, this action can't just kill all containers with docker kill $(docker ps -q) recklessly.

This action will kill only the containers launched by that specific workflow instantiation.

How it works

It uses javascript actions in order to support post scripts without relying on containers.

It has two stages, one that lists containers before the workflow run, and another that kills containers that are not in that first container list.

The lists are saved by the list-containers.sh script, which is invoked by index.js. This script saves a list of the currently running containers to /tmp/pre-containers-list.txt.

The kill-containers.sh script produces another list with running containers and saves it to /tmp/post-containers-list.txt. It then takes a differente of the two lists with comm and uses docker kill only on the resulting list. If it succeedes in killing containers it will then execute docker system prune --force --volumes to make sure all traces of the killed containers are purged from the system.

Example usage

name: test containers cleanup

on:
  workflow_dispatch:

jobs:
  test-container-cleanup:
    runs-on: self-hosted
    steps:
    - uses: waycarbon/github-action-container-cleanup@v1
    - name: launch nginx container
      run: docker run -d nginx
    - name: launch mysql container
      run: docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
    - name: introduce workflow error
      run: exit 1

github-action-container-cleanup's People

Contributors

gchamon avatar

Stargazers

 avatar

Watchers

 avatar Pedro Polez avatar Ighor avatar

Forkers

kacus infrashield

github-action-container-cleanup's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.