Skip to content

Commit

Permalink
feat: replace apm-pipeline-library version-framework (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Nov 4, 2024
1 parent 5bbf2b9 commit 85cc5ac
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 0 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/test-version-framework.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: test-version-framework

on:
workflow_dispatch:
inputs:
dry-run:
description: 'Dry run'
required: true
default: false
type: boolean
push:
paths:
- '.github/workflows/test-version-framework.yml'
- 'version-framework/**'

permissions:
contents: read

jobs:
test:
if: always()
needs:
- valid-all
- valid-no-excluded
- invalid
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 }}

valid-all:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./version-framework
id: validation
with:
versions-file: version-framework/test-data/versions.yml
frameworks-file: version-framework/test-data/frameworks.yml
excluded-file: version-framework/test-data/excluded.yml
- id: want
run: echo "matrix={\"include\":[{\"version\":\"ruby:3.1\",\"framework\":\"rails-7.0\"},{\"version\":\"ruby:3.1\",\"framework\":\"rails-6.1\"},{\"version\":\"ruby:2.4\",\"framework\":\"rails-6.1\"}]}" >> $GITHUB_OUTPUT
- name: Assert is valid
run: |
echo '${{ steps.validation.outputs.matrix }}' | jq . > validation.json
echo '${{ steps.want.outputs.matrix }}' | jq . > want.json
diff validation.json want.json
valid-no-excluded:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./version-framework
id: validation
with:
versions-file: version-framework/test-data/versions.yml
frameworks-file: version-framework/test-data/frameworks.yml
- id: want
run: echo "matrix={\"include\":[{\"version\":\"ruby:3.1\",\"framework\":\"rails-7.0\"},{\"version\":\"ruby:3.1\",\"framework\":\"rails-6.1\"},{\"version\":\"ruby:2.4\",\"framework\":\"rails-7.0\"},{\"version\":\"ruby:2.4\",\"framework\":\"rails-6.1\"}]}" >> $GITHUB_OUTPUT
- name: Assert is valid
run: |
echo '${{ steps.validation.outputs.matrix }}' | jq . > validation.json
echo '${{ steps.want.outputs.matrix }}' | jq . > want.json
diff validation.json want.json
invalid:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./version-framework
id: validation
continue-on-error: true
with:
versions-file: invalid.yml
frameworks-file: invalid.yml

- name: Verify step failed
if: steps.validation.outcome != 'failure'
run: exit 1
62 changes: 62 additions & 0 deletions version-framework/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# <!--name-->version-framework<!--/name-->

[![usages](https://img.shields.io/badge/usages-white?logo=githubactions&logoColor=blue)](https://github.com/search?q=elastic%2Foblt-actions%2Fversion-framework+%28path%3A.github%2Fworkflows+OR+path%3A**%2Faction.yml+OR+path%3A**%2Faction.yaml%29&type=code)

<!--description-->
Create matrix for the supported versions and frameworks
<!--/description-->

## Inputs

<!--inputs-->
| Name | Description | Required | Default |
|-------------------|-----------------------------------------------------------------------------------------------------------|----------|---------|
| `versions-file` | The YAML file with the versions. Being VERSION the key for the list | `true` | ` ` |
| `frameworks-file` | The YAML file with the frameworks. Being FRAMEWORK the key for the list | `true` | ` ` |
| `excluded-file` | The YAML file with the excluded tuples. Being exclude the key for the list of tuples (VERSION, FRAMEWORK) | `false` | ` ` |
<!--/inputs-->

## Outputs
<!--outputs-->
| Name | Description |
|----------|--------------------------------------------------------------------|
| `matrix` | Processed matrix with the required tuples of version and framework |
<!--/outputs-->

## Usage

<!--usage action="elastic/oblt-actions/**" version="env:VERSION"-->
```yaml
on:
push:

permissions:
contents: read

jobs:
create-test-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.generate.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- id: generate
uses: elastic/oblt-actions/version-framework@v1
with:
versions-file: .ci/.ruby.yml
frameworks-file: .ci/.main_framework.yml
excluded-file: .ci/.exclude.yml

test:
needs:
- create-test-matrix
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 20
matrix: ${{ fromJSON(needs.create-test-matrix.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
[..]
```
<!--/usage-->
35 changes: 35 additions & 0 deletions version-framework/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: 'version-framework'
description: Create matrix for the supported versions and frameworks

inputs:
versions-file:
description: 'The YAML file with the versions. Being VERSION the key for the list'
required: true
frameworks-file:
description: 'The YAML file with the frameworks. Being FRAMEWORK the key for the list'
required: true
excluded-file:
description: 'The YAML file with the excluded tuples. Being exclude the key for the list of tuples (VERSION, FRAMEWORK)'
required: false

outputs:
matrix:
description: "Processed matrix with the required tuples of version and framework"
value: ${{ steps.generator.outputs.matrix }}

runs:
using: "composite"
steps:
- id: generator
run: python ${{ github.action_path }}/matrix.py
env:
VERSIONS_FILE: ${{ inputs.versions-file }}
FRAMEWORKS_FILE: ${{ inputs.frameworks-file }}
EXCLUDED_FILE: ${{ inputs.excluded-file }}
shell: bash

- name: debug
run: |
echo 'Matrix to test: ${{ steps.generator.outputs.matrix }}'
shell: bash
30 changes: 30 additions & 0 deletions version-framework/matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json
import os
import yaml
from pathlib import Path


def run() -> None:
excluded_file = os.getenv("EXCLUDED_FILE", "")
framework_file = os.environ["FRAMEWORKS_FILE"]
versions_file = os.environ["VERSIONS_FILE"]

excludes = {'exclude': []}

if len(excluded_file) > 0:
excludes = yaml.safe_load(Path(excluded_file).read_text())
frameworks = yaml.safe_load(Path(framework_file).read_text())
versions = yaml.safe_load(Path(versions_file).read_text())

matrix = {'include': []}
for version in versions['VERSION']:
for framework in frameworks['FRAMEWORK']:
if len(list(filter(lambda item: item['VERSION'] == version and item["FRAMEWORK"] == framework, excludes['exclude']))) > 0:
print('excluded ' + version + ' with ' + framework)
else:
matrix['include'].append({"version": version, "framework": framework})

with open(os.environ.get('GITHUB_OUTPUT'), "a") as f:
f.write("matrix={}\n".format(json.dumps(matrix)))

run()
3 changes: 3 additions & 0 deletions version-framework/test-data/excluded.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exclude:
- VERSION: ruby:2.4
FRAMEWORK: rails-7.0
3 changes: 3 additions & 0 deletions version-framework/test-data/frameworks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FRAMEWORK:
- rails-7.0
- rails-6.1
3 changes: 3 additions & 0 deletions version-framework/test-data/versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VERSION:
- ruby:3.1
- ruby:2.4

0 comments on commit 85cc5ac

Please sign in to comment.