Skip to content

Javascript github action to clean up rogue instances in the cloud.

License

Notifications You must be signed in to change notification settings

drudilorenzo/clean-up-cloud-rogue-instances

Repository files navigation

Clean-up-cloud-rogue-instances

GitHub Actions JavaScript License: MIT Stable Version Latest Release

  1. Prerequisites
  2. Explanation
  3. How-to-use

JavaScript Github Action to clean up AWS running instances (in a specified AWS region) without a Github Runner linked to them.
To identify the instances are used two tags:

  1. Key: Name, Value: <REPO_NAME> Github Runner' (insert the name of your repo).
  2. Key: Runner, Value: <Name of the runner linked to the instance (it's the value used to understand if its linked runner is online, it must be unique!).

It works perfectly with the runners deployed using ephemeral-github-runner (see also the related github action).

Prerequisites

  1. You have a repository where you use self-hosted runners.
  2. You have an AWS account.
  3. You have added secrets to your repository that are later used to set environment variables. More information on secrets: How to set up secrets.

Inputs

Everything below is required.

  • repo-name: The name of the repository for which you want to clean up offline runners.
  • repo-owner: The owner of the repository for which you want to clean up offline runners.
  • aws-region: AWS region where the instances are located, eg. eu-west-2.

Environment Variables

  • APP_ID: GitHub App ID.
  • APP_PRIVATE_KEY: GitHub App Private Key.
  • AWS_ACCESS_KEY_ID: Your access key id received when account was created.
  • AWS_SECRET_ACCESS_KEY: Your secret access key received when account was created.

Example Usage

name: clean-up-instances
on: <event on which the action has to start>
jobs:
    manage-runners:
        runs-on: ubuntu-latest
        steps:
          - uses: LorenzoDrudi/clean-up-cloud-rogue-instances@<version to use>
            env:
              APP_ID: ${{ secrets.APP_ID }}
              APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
            with:
              repo-name: <name of the repository for which you want to clean up offline runners>
              repo-owner: <owner of the repository for which you want to clean up offline runners>
              aws-region: <AWS region where the instances are located>

All the personal inputs are passed by github secret. See the docs.

Tags and Releases

A github action workflow automatically creates a Tag and a Release every push on the main branch.
That's only a good DevOps practice, furthermore the main branch is protected and changes can come only over PR.
The idea is to work on develop/features branches and when it's done merge to the main branch, so the workflow starts.

The default behaviour is to create a minor tag/release (e.g. 1.*.0), the schema is <major_version>.<minor_version>.<patch_version>.
It's possible also to create major or patch tags/releases adding a tag at the end of the commit message:

  • #major -> e.g. *.0.0
  • #patch -> e.g. 1.1.*

For more info see the references.

References