centreon-plugins/.github/workflows/get-environment.yml

186 lines
7.2 KiB
YAML
Raw Normal View History

2023-01-10 15:03:40 +01:00
on:
workflow_call:
inputs:
version_file:
required: false
type: string
2023-01-10 15:03:40 +01:00
outputs:
version:
description: "version"
value: ${{ jobs.get-environment.outputs.version }}
2023-01-10 15:03:40 +01:00
release:
description: "release"
value: ${{ jobs.get-environment.outputs.release }}
stability:
description: "branch stability (stable, testing, unstable, canary)"
value: ${{ jobs.get-environment.outputs.stability }}
target_stability:
description: "Final target branch stability (stable, testing, unstable, canary or not defined if not a pull request)"
value: ${{ jobs.get-environment.outputs.target_stability }}
release_type:
description: "type of release (hotfix, release or not defined if not a release)"
value: ${{ jobs.get-environment.outputs.release_type }}
is_targeting_feature_branch:
description: "if it is a PR, check if targeting a feature branch"
value: ${{ jobs.get-environment.outputs.is_targeting_feature_branch }}
2023-01-10 15:03:40 +01:00
jobs:
get-environment:
runs-on: ubuntu-24.04
2023-01-10 15:03:40 +01:00
outputs:
version: ${{ steps.get_version.outputs.version }}
release: ${{ steps.get_release.outputs.release }}
stability: ${{ steps.get_stability.outputs.stability }}
target_stability: ${{ steps.get_stability.outputs.target_stability }}
release_type: ${{ steps.get_release_type.outputs.release_type }}
is_targeting_feature_branch: ${{ steps.get_stability.outputs.is_targeting_feature_branch }}
2023-01-10 15:03:40 +01:00
steps:
- name: Checkout sources (current branch)
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- if: ${{ github.event_name == 'pull_request' }}
name: Get nested pull request path
id: pr_path
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const prPath = ['${{ github.head_ref }}', '${{ github.base_ref }}'];
const result = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
state: 'open'
});
let found = true;
while (found) {
found = false;
result.data.forEach(({ head: { ref: headRef }, base: { ref: baseRef} }) => {
if (headRef === prPath[prPath.length - 1] && ! prPath.includes(baseRef)) {
found = true;
prPath.push(baseRef);
}
});
}
return prPath;
- name: Get stability
id: get_stability
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const getStability = (branchName) => {
switch (true) {
case /(^develop$)|(^dev-\d{2}\.\d{2}\.x$)|(^prepare-release-cloud.*)/.test(branchName):
return 'unstable';
case /(^release.+)|(^hotfix.+)/.test(branchName):
return 'testing';
case /(^master$)|(^\d{2}\.\d{2}\.x$)/.test(branchName):
return 'stable';
default:
return 'canary';
}
};
core.setOutput('stability', getStability('${{ github.head_ref || github.ref_name }}'));
let isTargetingFeatureBranch = false;
if ("${{ github.event_name }}" === "pull_request") {
let targetStability = 'canary';
const prPath = ${{ steps.pr_path.outputs.result || '[]' }};
prPath.shift(); // remove current branch
if (prPath.length && getStability(prPath[0]) === 'canary') {
isTargetingFeatureBranch = true;
}
prPath.every((branchName) => {
console.log(`checking stability of ${branchName}`)
targetStability = getStability(branchName);
if (targetStability !== 'canary') {
return false;
}
2023-01-10 15:03:40 +01:00
return true;
});
core.setOutput('target_stability', targetStability);
}
core.setOutput('is_targeting_feature_branch', isTargetingFeatureBranch);
- name: Get version
id: get_version
run: |
if [[ "${{ inputs.version_file }}" == "" ]]; then
VERSION=$(date '+%Y%m%d')
elif [[ "${{ inputs.version_file }}" == */*.yaml ]]; then
VERSION=$(grep 'version: ' ${{ inputs.version_file }} | cut -d' ' -f2 | tr -d '"')
else
VERSION=$(grep VERSION ${{ inputs.version_file }} | cut -d "'" -f 2)
fi
2023-01-10 15:03:40 +01:00
echo "version=$(echo $VERSION)" >> $GITHUB_OUTPUT
shell: bash
2023-01-10 15:03:40 +01:00
- name: "Get release: 1 for testing / stable, <date>.<commit_sha> for others"
id: get_release
run: |
RELEASE=$(date '+%H%M%S')
echo "release=$RELEASE" >> $GITHUB_OUTPUT
2023-01-10 15:03:40 +01:00
shell: bash
- name: "Get release type: hotfix, release or not defined if not a release"
id: get_release_type
run: |
RELEASE_TYPE=$(echo "${{ github.head_ref || github.ref_name }}" | cut -d '-' -f 1)
if [[ "$RELEASE_TYPE" == "hotfix" || "$RELEASE_TYPE" == "release" ]]; then
echo "release_type=$RELEASE_TYPE" >> $GITHUB_OUTPUT
fi
shell: bash
- name: Display info in job summary
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const outputTable = [
[{data: 'Name', header: true}, {data: 'Value', header: true}],
['version', '${{ steps.get_version.outputs.version }}'],
['release', '${{ steps.get_release.outputs.release }}'],
['stability', '${{ steps.get_stability.outputs.stability }}'],
['release_type', '${{ steps.get_release_type.outputs.release_type || '<em>not defined because this is not a release</em>' }}'],
['is_targeting_feature_branch', '${{ steps.get_stability.outputs.is_targeting_feature_branch }}'],
['target_stability', '${{ steps.get_stability.outputs.target_stability || '<em>not defined because current run is not triggered by pull request event</em>' }}'],
];
core.summary
.addHeading(`${context.workflow} environment outputs`)
.addTable(outputTable);
if ("${{ github.event_name }}" === "pull_request") {
const prPath = ${{ steps.pr_path.outputs.result || '[]' }};
const mainBranchName = prPath.pop();
let codeBlock = `
%%{ init: { 'gitGraph': { 'mainBranchName': '${mainBranchName}', 'showCommitLabel': false } } }%%
gitGraph
commit`;
prPath.reverse().forEach((branchName) => {
codeBlock = `${codeBlock}
branch ${branchName}
checkout ${branchName}
commit`;
});
core.summary
.addHeading('Git workflow')
.addCodeBlock(
codeBlock,
"mermaid"
);
}
core.summary.write();