From b751afebcc542933cc017c0cecbf794e0b5286c2 Mon Sep 17 00:00:00 2001 From: Ramez Noshy Date: Wed, 2 Oct 2024 14:41:55 +0300 Subject: [PATCH] [FEATURE]: Nightly clean review apps and images (#603) * feat: set review apps as stale after 5 days * refactor: move common env setup to a different file * feat: nightly github action to remove stale apps and images * fix: rename folder * feat: allow manual triggers of removing stale review apps * feat: temporarily allow running workflow on pull requests events to debug * feat: add CPLN_TOKEN, CPLN_ORG * feat: add image_retention_days: 5 * feat: remove triggering stale-apps removal on PR events --- .controlplane/controlplane.yml | 2 ++ .../deploy-to-control-plane/action.yml | 19 ++---------- .github/actions/setup-environment/action.yml | 25 ++++++++++++++++ .../nightly-remove-stale-review-apps.yml | 30 +++++++++++++++++++ 4 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 .github/actions/setup-environment/action.yml create mode 100644 .github/workflows/nightly-remove-stale-review-apps.yml diff --git a/.controlplane/controlplane.yml b/.controlplane/controlplane.yml index af198fba..53f68730 100644 --- a/.controlplane/controlplane.yml +++ b/.controlplane/controlplane.yml @@ -71,3 +71,5 @@ apps: - rails # match_if_app_name_starts_with is used to identify these "qa" apps. match_if_app_name_starts_with: true + image_retention_days: 5 + stale_app_image_deployed_days: 5 # If the app is older than 5 days, the nightly automations will clean stale apps. diff --git a/.github/actions/deploy-to-control-plane/action.yml b/.github/actions/deploy-to-control-plane/action.yml index 7105204c..43a9eb97 100644 --- a/.github/actions/deploy-to-control-plane/action.yml +++ b/.github/actions/deploy-to-control-plane/action.yml @@ -16,29 +16,14 @@ inputs: runs: using: 'composite' steps: - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: '3.3.3' # Specify your Ruby version here - - - name: Install Control Plane CLI - shell: bash - run: | - sudo npm install -g @controlplane/cli@3.1.0 - cpln --version - gem install cpflow -v 4.0.0 - cpflow --version + - name: Setup Environment + uses: ./.github/actions/setup-environment - name: Set Short SHA id: vars shell: bash run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" - - name: cpln profile - shell: bash - run: | - cpln profile update default - # Caching step - uses: actions/cache@v2 with: diff --git a/.github/actions/setup-environment/action.yml b/.github/actions/setup-environment/action.yml new file mode 100644 index 00000000..48a5d269 --- /dev/null +++ b/.github/actions/setup-environment/action.yml @@ -0,0 +1,25 @@ +# Control Plane GitHub Action + +name: 'Setup Environment' +description: 'Sets up Ruby, installs Control Plane CLI, cpflow gem, and sets up the default profile' + +runs: + using: 'composite' + steps: + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3.3' + + - name: Install Control Plane CLI and cpflow gem + shell: bash + run: | + sudo npm install -g @controlplane/cli@3.1.0 + cpln --version + gem install cpflow -v 4.0.0 + cpflow --version + + - name: cpln profile + shell: bash + run: | + cpln profile update default diff --git a/.github/workflows/nightly-remove-stale-review-apps.yml b/.github/workflows/nightly-remove-stale-review-apps.yml new file mode 100644 index 00000000..9f3985ba --- /dev/null +++ b/.github/workflows/nightly-remove-stale-review-apps.yml @@ -0,0 +1,30 @@ +name: Nightly Remove Stale Review Apps and Images + +on: + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + schedule: + - cron: '0 0 * * *' + +env: + CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}} + CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}} + +jobs: + remove-stale-review-apps: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Environment + uses: ./.github/actions/setup-environment + + - name: Run cleanup-stale-apps script + run: | + cpflow cleanup-stale-apps -a qa-react-webpack-rails-tutorial -y + + - name: Run cleanup-images script + run: | + cpflow cleanup-images -a qa-react-webpack-rails-tutorial -y