From 19f6e79fdfd4334874b6d31e4c1ea79b2684f386 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Tue, 3 Sep 2024 08:34:49 +0200 Subject: [PATCH] feat: support active branches (#109) --- .../test-elastic-active-branches.yml | 54 ++++++++++++++++++ elastic/active-branches/README.md | 55 +++++++++++++++++++ elastic/active-branches/action.yml | 28 ++++++++++ elastic/active-branches/script.py | 37 +++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 .github/workflows/test-elastic-active-branches.yml create mode 100644 elastic/active-branches/README.md create mode 100644 elastic/active-branches/action.yml create mode 100644 elastic/active-branches/script.py diff --git a/.github/workflows/test-elastic-active-branches.yml b/.github/workflows/test-elastic-active-branches.yml new file mode 100644 index 0000000..b4961d7 --- /dev/null +++ b/.github/workflows/test-elastic-active-branches.yml @@ -0,0 +1,54 @@ +name: test-elastic-active-branches + +on: + pull_request: + branches: + - main + paths: + - '.github/workflows/test-elastic-active-branches.yml' + - 'elastic/active-branches/**' + push: + branches: + - main + paths: + - '.github/workflows/test-elastic-active-branches.yml' + - 'elastic/active-branches/**' + +permissions: + contents: read + +jobs: + + test: + needs: + - main + - exclude-main + runs-on: ubuntu-latest + steps: + - id: check + uses: elastic/oblt-actions/check-dependent-jobs@v1 + with: + jobs: ${{ toJSON(needs) }} + - run: ${{ steps.check.outputs.is-success }} + + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ./elastic/active-branches + id: active-branches + - name: Verify main is in the active branches + if: ${{ !contains(fromJSON(steps.active-branches.outputs.branches), 'main') }} + run: echo "Main branch could not be found" + + exclude-main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ./elastic/active-branches + id: active-branches + with: + exclude-branches: main + - name: Verify main is not in the active branches + if: contains(fromJSON(steps.active-branches.outputs.branches), 'main') + run: echo "Main branch could be found but should be excluded" diff --git a/elastic/active-branches/README.md b/elastic/active-branches/README.md new file mode 100644 index 0000000..649019d --- /dev/null +++ b/elastic/active-branches/README.md @@ -0,0 +1,55 @@ +# elastic/active-branches + +[![usages](https://img.shields.io/badge/usages-white?logo=githubactions&logoColor=blue)](https://github.com/search?q=elastic%2Foblt-actions%2Felastic%2Factive-branches+%28path%3A.github%2Fworkflows+OR+path%3A**%2Faction.yml+OR+path%3A**%2Faction.yaml%29&type=code) +[![test-elastic-active-branches](https://github.com/elastic/oblt-actions/actions/workflows/test-elastic-active-branches.yml/badge.svg?branch=main)](https://github.com/elastic/oblt-actions/actions/workflows/test-elastic-active-branches.yml) + + +Fetch the current list of active branches in Elastic (the ones based on the Unified Release process) + + +## Inputs + +| Name | Description | Required | Default | +|--------------------|----------------------------------|----------|---------| +| `exclude-branches` | Exclude branches comma separated | `false` | ` ` | + + +## Outputs + + +| Name | Description | +|------------|---------------------------------------------------------------| +| `matrix` | Processed matrix with the branches (using the include format) | +| `branches` | Processed list of branches | + + +## Usage + + +```yaml +jobs: + filter: + runs-on: ubuntu-latest + timeout-minutes: 1 + outputs: + matrix: ${{ steps.generator.outputs.matrix }} + steps: + - id: generator + uses: elastic/oblt-actions/elastic/active-branches@v1 + with: + exclude-branches: '7.17' + + bump-elastic-stack: + runs-on: ubuntu-latest + needs: [filter] + strategy: + matrix: ${{ fromJson(needs.filter.outputs.matrix) }} + steps: + - uses: actions/checkout@v4 + with: + ref: "${{ matrix.branch }}" + + + # ... +``` + diff --git a/elastic/active-branches/action.yml b/elastic/active-branches/action.yml new file mode 100644 index 0000000..9c66e6e --- /dev/null +++ b/elastic/active-branches/action.yml @@ -0,0 +1,28 @@ +name: elastic/active-branches +description: | + Fetch the current list of active branches in Elastic (the ones based on the Unified Release process) +inputs: + exclude-branches: + description: "Exclude branches comma separated" + required: false + type: string + default: '' +outputs: + matrix: + description: "Processed matrix with the branches (using the include format)" + value: ${{ steps.generator.outputs.matrix }} + branches: + description: "Processed list of branches" + value: ${{ steps.generator.outputs.branches }} +runs: + using: composite + steps: + - id: generator + shell: bash + run: python ${{ github.action_path }}/script.py + env: + EXCLUDE_BRANCHES: ${{ inputs.exclude-branches }} + - id: debug + shell: bash + run: | + echo 'Matrix: ${{ steps.generator.outputs.matrix }}' diff --git a/elastic/active-branches/script.py b/elastic/active-branches/script.py new file mode 100644 index 0000000..4ab6bdd --- /dev/null +++ b/elastic/active-branches/script.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import json +import os +import requests + +def fails(msg): + print(msg) + exit(1) + +req = requests.get(url='https://storage.googleapis.com/artifacts-api/snapshots/branches.json') +if req.status_code != requests.codes.ok: + fails("Failed to fetch active branches") + +try: + payload = req.json() +except requests.exceptions.JSONDecodeError: + fails("Failed to decode json payload") + +branches = payload.get('branches') +if not branches: + fails("Failed to retrieve active branches") + +exclude_branches = os.environ.get('EXCLUDE_BRANCHES', '') +exclude_branches = set(filter(lambda branch: len(branch) > 0, exclude_branches.split(','))) +if exclude_branches: + branches = list(filter(lambda branch: branch not in exclude_branches, branches)) + +include_branches = list(map(lambda branch: {"branch": branch}, branches)) +matrix = {'include': include_branches} + +with open(os.environ.get('GITHUB_OUTPUT'), "a") as file_descriptor: + file_descriptor.write(f"matrix={json.dumps(matrix)}\n") + file_descriptor.write(f"branches={json.dumps(branches)}\n") + +print(f"INFO: matrix={json.dumps(matrix)}") +print(f"INFO: branches={json.dumps(branches)}")