Skip to content

Commit

Permalink
init (#2)
Browse files Browse the repository at this point in the history
* init

* environment name fix

* convert yaml to json

* convert yaml to json

* convert yaml to json

* convert yaml to json

* convert yaml to json

* add quotes

* use backtick for multiline

* bugfix syntax

* update yaml to json

* jsontoyaml

* jsontoyaml output fix

* update test

* update test to compare correct outputs

* use branch-name override

* update to support Pipes

* change to be test

* operation = ''

* new var for new value

* update where let is defined

* cleanup

* conditionally set value

* switch instead of ifs

* fix bad test

* remove negative test
  • Loading branch information
Benbentwo authored Aug 1, 2023
1 parent 7122960 commit 08498b3
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 58 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/test-negative.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,3 @@ jobs:
- name: Setup
run: echo "Do setup"

test:
runs-on: ubuntu-latest
needs: [setup]
continue-on-error: true
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: ./
id: current
with:
param1: 'false'

outputs:
result: "${{ steps.current.outputs.result1 }}"

assert:
runs-on: ubuntu-latest
needs: [test]
steps:
- uses: nick-fields/assert-action@v1
with:
expected: 'false'
actual: "${{ needs.test.outputs.result }}"

teardown:
runs-on: ubuntu-latest
needs: [assert]
if: ${{ always() }}
steps:
- name: Tear down
run: echo "Do Tear down"
47 changes: 36 additions & 11 deletions .github/workflows/test-positive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,58 @@ jobs:
run: echo "Do setup"

test:
strategy:
matrix:
include:
- env: preview
expected-namespace: app-ns
- env: preview-prs
expected-namespace: app-pr-undefined # undefined here because this is called through dispatch
- env: production
expected-namespace: foo-bar-zyx
- env: dev
expected-namespace: dev
env: [preview, preview-prs, production, dev]
runs-on: ubuntu-latest
continue-on-error: true
needs: [setup]
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: ./
id: current
with:
param1: 'true'
environment: ${{ matrix.env }}
namespace: ns
application: "app"
config: |
preview:
cluster: cluster-preview
namespace: ${{inputs.namespace}}
reformat: branch-name
preview-prs:
cluster: cluster-preview-prs
namespace: bar-123
reformat: pr-number
production:
cluster: cluster-production
namespace: Foo_Bar/ZYX | kebabcase
outputs:
result: "${{ steps.current.outputs.result1 }}"
dev:
cluster: cluster-dev
namespace: dev
assert:
runs-on: ubuntu-latest
needs: [test]
steps:
- uses: nick-fields/assert-action@v1
with:
expected: 'true'
actual: "${{ needs.test.outputs.result }}"
actual: "${{ steps.current.outputs.namespace }}"
expected: '${{ matrix.expected-namespace }}'

teardown:
runs-on: ubuntu-latest
needs: [assert]
needs: [test]
if: ${{ always() }}
steps:
- name: Tear down
Expand Down
162 changes: 147 additions & 15 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,155 @@
name: 'Example composite GitHub action'
description: 'Example composite GitHub action'
author: [email protected]
branding:
icon: 'file'
color: 'white'
name: 'Environments - ArgoCD'
description: 'Get information about environment'
inputs:
param1:
environment:
description: "Environment"
required: true
description: "Input parameter placeholder"
default: "true"
namespace:
description: Kubernetes namespace
required: true
application:
description: "The application name"
required: false
config:
description: "configuration"
required: true
namespace-prefix:
description: Kubernetes namespace prefix
required: false
default: ''
namespace-suffix:
description: Kubernetes namespace suffix
required: false
default: ''
namespace-deny-list:
description: Kubernetes namespace deny list, generated names cannot contain this comma separated list.
required: false
default: kube-system,kube-public,default
outputs:
result1:
description: "Output result placeholder"
value: "${{ steps.context.outputs.action-result }}"
name:
description: "Environment name"
value: ${{ inputs.environment }}
role:
description: "Environments that need to be deployed"
value: ${{ steps.result.outputs.cluster-role }}
cluster:
description: "Environments that need to be destroyed"
value: ${{ steps.result.outputs.cluster }}
namespace:
description: "Namespace"
value: ${{ steps.result.outputs.namespace }}
ssm-path:
description: "Path to ssm secrets"
value: ${{ steps.result.outputs.ssm-path }}
environment-config:
description: "Environment configuration"
value: ${{ steps.json-to-yaml.outputs.output }}
runs:
using: "composite"
steps:
- id: context

- uses: actions/checkout@v3

- run: |
echo '''${{ inputs.config }}''' > config.yml
shell: bash
- uses: fabasoad/data-format-converter-action@main
id: yaml-to-json
with:
input: "config.yml"
from: "yaml"
to: "json"

- name: Evaluate Config
id: parse-config
uses: actions/github-script@v6
with:
script: |
let data = JSON.parse(`${{ steps.yaml-to-json.outputs.output }}`);
let environment = '${{ inputs.environment }}';
for(const [environment, config] of Object.entries(data)) {
if(environment === '${{ inputs.environment }}') {
for(const [key, value] of Object.entries(config)) {
// Support `|` Operations
let operation = '';
let new_value = '';
if(String(value).includes('|')) {
new_value = value.split('|')[0].trim()
operation = value.split('|')[1].trim()
}
switch(operation) {
case 'kebab':
case 'kebabcase':
case 'toKebab':
new_value = new_value.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_/]+/g, '-').toLowerCase()
break;
case 'upper':
case 'uppercase':
case 'toUpper':
new_value = new_value.toLowerCase()
break;
case 'lower':
case 'lowercase':
case 'toLower':
new_value = new_value.toLowerCase()
break;
default:
new_value = new_value
}
if(new_value !== '') {
data[environment][key] = new_value;
}
if(key === 'reformat') {
core.setOutput('reformat', value);
delete data[environment][key];
}
}
}
}
core.setOutput('stripped-config', JSON.stringify(data[environment]));
- name: Reformat
if: ${{ steps.parse-config.outputs.reformat }}
uses: cloudposse/kubernetes-namespace-builder-composite-action@main
id: reformat
with:
flavor: ${{ steps.parse-config.outputs.reformat }}
app-name: ${{ inputs.application }}
prefix: ${{ inputs.namespace-prefix }}
suffix: ${{ inputs.namespace-suffix }}
deny-list: ${{ inputs.namespace-deny-list }}
override-branch-name: ${{ inputs.namespace }}

- name: Merge Substitutions
id: merge-config
uses: actions/github-script@v6
with:
script: |
let data = JSON.parse(`${{ steps.parse-config.outputs.stripped-config }}`);
if('${{ steps.parse-config.outputs.reformat }}') {
data['namespace'] = '${{ steps.reformat.outputs.kubernetes-namespace }}';
}
core.setOutput('merged-config', JSON.stringify(data));
- run: |
echo '''${{ steps.merge-config.outputs.merged-config }}''' > merged-config.json
shell: bash
run: |
echo "action-result=${{ inputs.param1 }}" >> $GITHUB_OUTPUT
- uses: fabasoad/data-format-converter-action@main
id: json-to-yaml
with:
input: "merged-config.json"
from: "json"
to: "yaml"

- name: Environment info
uses: cloudposse/[email protected]
id: result
with:
query: .
config: ${{ steps.json-to-yaml.outputs.output }}

0 comments on commit 08498b3

Please sign in to comment.