Skip to content

Commit

Permalink
feat(ci): adjust chart detection
Browse files Browse the repository at this point in the history
  • Loading branch information
cwrau committed Aug 6, 2024
1 parent 314cb65 commit 445a6e1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 47 deletions.
60 changes: 34 additions & 26 deletions .github/workflows/get-changed-chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@ name: Get single changed chart in last commit

on:
workflow_call:
inputs:
pr_number:
type: number
outputs:
chart:
description: The name of the changed cart in the last commit
value: ${{ jobs.getChangedChart.outputs.chart }}
found:
description: A chart was changed
value: ${{ jobs.getChangedChart.outputs.found == 'true' }}

jobs:
getChangedCharts:
uses: ./.github/workflows/get-changed-charts.yaml
with:
pr_number: ${{ inputs.pr_number }}
getChangedChart:
runs-on: ubuntu-latest
needs: getChangedCharts
outputs:
chart: ${{ steps.getChangedChart.outputs.chart }}
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}

- uses: helm/chart-testing-action@e6669bcd63d7cb57cb4380c33043eebe5d111992 # v2.6.1

- id: getChangedChart
name: Get changed chart in this commit
env:
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
name: Get changed chart in PR
run: |
set -x
set -e
set -o pipefail
changed="$(ct list-changed | cut -d / -f 2)"
if [[ -z "$changed" ]]; then
echo chart= | tee -a "$GITHUB_OUTPUT"
exit 0
fi
num_changed=$(wc -l <<<"$changed")
if ((num_changed > 1)); then
echo "This PR has changes to multiple charts. Please create individual PRs per chart." >&2
exit 1
fi
echo chart="$changed" | tee -a "$GITHUB_OUTPUT"
changed='${{ needs.getChangedCharts.outputs.charts }}'
num_changed='${{ needs.getChangedCharts.outputs.count }}'
case "$num_changed" in
0)
(
echo chart=
echo found=false
)| tee -a "$GITHUB_OUTPUT"
;;
1)
(
echo chart="$(<<<"$changed" jq -r first)"
echo found=true
) | tee -a "$GITHUB_OUTPUT"
;;
*)
echo "This PR has changes to multiple charts. Please create individual PRs per chart." >&2
exit 1
;;
esac
28 changes: 15 additions & 13 deletions .github/workflows/get-changed-charts.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
name: Get changed charts in last commit
name: Get changed charts in PR

on:
workflow_call:
inputs:
pr_number:
type: number
outputs:
charts:
description: The names of the changed charts in the last commit
description: The names of the changed charts in the PR
value: ${{ jobs.getChangedCharts.outputs.charts }}
count:
value: ${{ jobs.getChangedCharts.outputs.count }}

jobs:
getChangedCharts:
runs-on: ubuntu-latest
outputs:
charts: ${{ steps.getCharts.outputs.charts }}
permissions:
pull-requests: read
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}

- uses: helm/chart-testing-action@e6669bcd63d7cb57cb4380c33043eebe5d111992 # v2.6.1

- name: Get all charts
id: getCharts
env:
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
PULL_REQUEST_NUMBER: ${{ inputs.pr_number }}
GITHUB_TOKEN: ${{ github.token }}
run: |
set -ex
set -e
set -o pipefail
charts="$(gh api --paginate /repos/${GITHUB_REPOSITORY}/pulls/${PULL_REQUEST_NUMBER}/files | jq -cr 'map(.filename | select(test("charts/[^/]*")) | split("/") | .[1] | select(.)) | unique')"
(
echo -n charts=
ct list-changed | cut -d / -f 2 | jq -c -Rn '[inputs]'
echo charts="$charts"
echo count="$(<<<"$charts" jq -r length)"
) | tee -a "$GITHUB_OUTPUT"
15 changes: 8 additions & 7 deletions .github/workflows/label-pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ on:
- synchronize

jobs:
getChangedChart:
uses: ./.github/workflows/get-changed-chart.yaml
getChangedCharts:
uses: ./.github/workflows/get-changed-charts.yaml
with:
pr_number: ${{ github.event.pull_request.number }}
labelPullRequest:
name: Validate and label PR
name: Label PR
runs-on: ubuntu-latest
needs: getChangedChart
needs: getChangedCharts
steps:
- env:
PR_TITLE: ${{ github.event.pull_request.title }}
GITHUB_TOKEN: ${{ github.token }}
ISSUE_NUMBER: ${{ github.event.number }}
CHANGED_CHART: ${{ needs.getChangedChart.outputs.chart }}
CHANGED_CHARTS: ${{ needs.getChangedCharts.outputs.charts }}
run: |
curl --silent --fail-with-body \
-X POST \
-H 'Accept: application/vnd.github+json' \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${ISSUE_NUMBER}/labels" \
-d '{"labels":["'"$CHANGED_CHART"'"]}'
-d '{"labels":'"${CHANGED_CHARTS}"'}'
2 changes: 2 additions & 0 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ on:
jobs:
getChangedCharts:
uses: ./.github/workflows/get-changed-charts.yaml
with:
pr_number: ${{ github.event.pull_request.number }}
prepare-helm-chart:
name: prepare helm chart
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pr-comment-diff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
jobs:
getChangedChart:
uses: ./.github/workflows/get-changed-chart.yaml
with:
pr_number: ${{ github.event.pull_request.number }}
postDiffComment:
runs-on: ubuntu-latest
needs: getChangedChart
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/release-update-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ concurrency:
cancel-in-progress: true

on:
push:
pull_request:
types:
- opened
- synchronize
branches:
- release-please--branches--main--components-*

jobs:
getChangedChart:
uses: ./.github/workflows/get-changed-chart.yaml
with:
pr_number: ${{ github.event.pull_request.number }}
update-metadata-files:
runs-on: ubuntu-latest
needs: getChangedChart
Expand All @@ -19,6 +24,11 @@ jobs:
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
CHART: ${{ needs.getChangedChart.outputs.chart }}
steps:
- name: Validate changed chart
if: ${{ needs.getChangedChart.outputs.found == 'true' }}
run: |
echo 'No chart has been changed?' >&2
exit 1
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/validate-pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
getChangedChart:
if: ${{ !startsWith(github.head_ref, 'release-please--') }}
uses: ./.github/workflows/get-changed-chart.yaml
with:
pr_number: ${{ github.event.pull_request.number }}
validateCommits:
if: ${{ !startsWith(github.head_ref, 'release-please--') }}
name: Validate commits
Expand Down

0 comments on commit 445a6e1

Please sign in to comment.