Skip to content

Commit c37b3c4

Browse files
authored
VAULT-34822: Add pipeline github list changed-files (#30100)
* VAULT-34822: Add `pipeline github list changed-files` Add a new `github list changed-files` sub-command to `pipeline` command and integrate it into the pipeline. This replaces our previous `changed-files.sh` script. This command works quite a bit differently than the full checkout and diff based solution we used before. Instead of checking out the base ref and head ref and comparing a diff, we now provide either a pull request number or git commit SHA and use the Github REST API to determine the changed files. This approach has several benefits: - Not requiring a local checkout of the repo to get the list of changed files. This yields a significant perfomance improvement in `setup` jobs where we typically determine the changed files list. - The CLI supports both PRs and commit SHAs. - The implementation is portable and doesn't require any system tools like `git` or `bash` to be installed. - A much more advanced system for adding group metadata to the changed files. These groupings are going to be used heavily in future pipeline automation work and will be used to make required jobs smarter. The theoretical drawbacks: - It requires a GITHUB_TOKEN and only works for remote branches or commits in Github. We could eventually add a local diff sub-command or option to work locally, but that was not required for what we're trying to achieve here. While the groupings that I added in this change are quite rudimentary, the system will allow us to add additional groups with very little overhead. I tried to make this change more or less a port of the old system to enable future work. I did include one small change of behavior, which is that we now build all extended targets if the `go.mod` or `go.sum` files change. We do this to ensure that dependency changes don't subtly result in some extended platform breakage. Signed-off-by: Ryan Cragun <[email protected]>
1 parent c881782 commit c37b3c4

File tree

16 files changed

+788
-231
lines changed

16 files changed

+788
-231
lines changed

.github/actions/changed-files/action.yml

+30-60
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,45 @@
22
# SPDX-License-Identifier: BUSL-1.1
33

44
---
5-
name: Determine what files changed between two git referecnes.
5+
name: Determine what files have changed on either a pull request or commit.
66
description: |
7-
Determine what files have changed between two git references. If the github.event_type is
8-
pull_request we'll compare the github.base_ref (merge target) and pull request head SHA.
9-
For other event types we'll gather the changed files from the most recent commit. This allows
10-
us to support PR and merge workflows.
7+
Determine what files have changed on either a pull request or commit.
8+
Writes the list of files to
9+
10+
inputs:
11+
github-token:
12+
description: A preferred Github token to access private modules if necessary.
1113

1214
outputs:
13-
app-changed:
14-
description: Whether or not the vault Go app was modified.
15-
value: ${{ steps.changed-files.outputs.app-changed }}
16-
docs-changed:
17-
description: Whether or not the documentation was modified.
18-
value: ${{ steps.changed-files.outputs.docs-changed }}
19-
ui-changed:
20-
description: Whether or not the web UI was modified.
21-
value: ${{ steps.changed-files.outputs.ui-changed }}
22-
autopilot-changed:
23-
description: Whether or not files pertaining to Autopilot were modified.
24-
value: ${{ steps.changed-files.outputs.autopilot-changed }}
25-
files:
26-
description: All of the file names that changed.
27-
value: ${{ steps.changed-files.outputs.files }}
15+
changed-files:
16+
description: All of the files that changed.
17+
value: ${{ steps.changed-files.outputs.changed-files }}
2818

2919
runs:
3020
using: composite
3121
steps:
32-
- id: ref
22+
- id: changed-files-set-up-pipeline
23+
name: Set up the pipeline tool
24+
uses: ./.github/actions/set-up-pipeline
25+
with:
26+
github-token: ${{ inputs.github-token || github.token }}
27+
- id: changed-files
28+
name: Determine the changed files
3329
shell: bash
34-
name: ref
30+
env:
31+
GITHUB_TOKEN: ${{ inputs.github-token || github.token }}
3532
run: |
36-
# Determine our desired checkout ref.
37-
#
38-
# * If the trigger event is pull_request we will default to a magical merge SHA that Github
39-
# creates. This SHA is the product of what merging our PR into the merge target branch at
40-
# at the point in time when we created the PR. When you push a change to a PR branch
41-
# Github updates this branch if it can. When you rebase a PR it updates this branch.
42-
#
43-
# * If the trigger event is pull_request and a `checkout-head` tag is present or the
44-
# checkout-head input is set, we'll use HEAD of the PR branch instead of the magical
45-
# merge SHA.
46-
#
47-
# * If the trigger event is a push (merge) then we'll get the latest commit that was pushed.
48-
#
49-
# * For anything any other event type we'll default to whatever is default in Github.
33+
# Get a list of changed files and write the "changed-files" output to $GITHUB_OUTPUT
5034
if [ '${{ github.event_name }}' = 'pull_request' ]; then
51-
checkout_ref='${{ github.event.pull_request.head.sha }}'
52-
elif [ '${{ github.event_name }}' = 'push' ]; then
53-
# Our checkout ref for any other event type should default to the github ref.
54-
checkout_ref='${{ github.event.after && github.event.after || github.event.push.after }}'
35+
pipeline github list changed-files \
36+
--owner hashicorp \
37+
--repo '${{ github.event.pull_request.head.repo.name }}' \
38+
--pr '${{ github.event.pull_request.number }}' \
39+
--github-output
5540
else
56-
checkout_ref='${{ github.ref }}'
41+
pipeline github list changed-files \
42+
--owner hashicorp \
43+
--repo '${{ github.event.repository.name }}' \
44+
--commit '${{ github.event.after && github.event.after || github.event.push.after && github.event.push.after || github.sha }}' \
45+
--github-output
5746
fi
58-
echo "ref=${checkout_ref}" | tee -a "$GITHUB_OUTPUT"
59-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
60-
with:
61-
repository: ${{ github.repository }}
62-
path: "changed-files"
63-
# The fetch-depth could probably be optimized at some point. It's currently set to zero to
64-
# ensure that we have a successfull diff, regardless of how many commits might be present
65-
# present between the two references we're comparing. It would be nice to change this
66-
# depending on the number of commits by using the push.commits and/or pull_request.commits
67-
# payload fields, however, they have different behavior and limitations. For now we'll do
68-
# the slow but sure thing of getting the whole repository.
69-
fetch-depth: 0
70-
ref: ${{ steps.ref.outputs.ref }}
71-
- id: changed-files
72-
name: changed-files
73-
# This script writes output values to $GITHUB_OUTPUT and STDOUT
74-
shell: bash
75-
run: ./.github/scripts/changed-files.sh ${{ github.event_name }} ${{ github.ref_name }} ${{ github.base_ref }}
76-
working-directory: changed-files

.github/actions/set-up-pipeline/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ runs:
1414
steps:
1515
- uses: ./.github/actions/set-up-go
1616
with:
17-
github-token: ${{ inputs.github-token }}
17+
github-token: ${{ inputs.github-token || github.token }}
1818
no-restore: true # Don't download vault's modules for pipeline
1919
- name: pipeline-metadata
2020
id: pipeline-metadata

.github/scripts/changed-files.sh

-83
This file was deleted.

.github/workflows/build.yml

+32-13
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,32 @@ jobs:
8383
github.event_name == 'schedule' ||
8484
(github.event_name == 'pull_request' && github.event.pull_request.draft == false)
8585
runs-on: ${{ github.repository == 'hashicorp/vault' && 'ubuntu-latest' || fromJSON('["self-hosted","linux","small"]') }}
86+
permissions:
87+
id-token: write # vault-auth
88+
contents: read
8689
outputs:
87-
app-changed: ${{ steps.changed-files.outputs.app-changed }}
8890
build-date: ${{ steps.metadata.outputs.vault-build-date }}
91+
changed-files: ${{ steps.changed-files.outputs.changed-files }}
8992
checkout-ref: ${{ steps.checkout.outputs.ref }}
9093
compute-build: ${{ steps.metadata.outputs.compute-build }}
9194
compute-build-compat: ${{ steps.metadata.outputs.compute-build-compat }}
9295
compute-build-ui: ${{ steps.metadata.outputs.compute-build-ui }}
9396
compute-small: ${{ steps.metadata.outputs.compute-small }}
94-
docs-changed: ${{ steps.changed-files.outputs.docs-changed }}
9597
is-draft: ${{ steps.metadata.outputs.is-draft }}
9698
is-enterprise: ${{ steps.metadata.outputs.is-enterprise }}
9799
is-fork: ${{ steps.metadata.outputs.is-fork }}
98100
labels: ${{ steps.metadata.outputs.labels }}
99-
ui-changed: ${{ steps.changed-files.outputs.ui-changed }}
100101
vault-binary-name: ${{ steps.metadata.outputs.vault-binary-name }}
101102
vault-revision: ${{ steps.metadata.outputs.vault-revision }}
102103
vault-version: ${{ steps.metadata.outputs.vault-version }}
103104
vault-version-metadata: ${{ steps.metadata.outputs.vault-version-metadata }}
104105
vault-version-package: ${{ steps.metadata.outputs.vault-version-package }}
105106
workflow-trigger: ${{ steps.metadata.outputs.workflow-trigger }}
106107
steps:
107-
# Run the changed-files action to determine what Git reference we should check out
108108
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
109-
- uses: ./.github/actions/changed-files
110-
id: changed-files
109+
# Make sure we check out correct ref based on PR labels and such
111110
- uses: ./.github/actions/checkout
112-
id: checkout # Make sure we check out correct ref after checking changed files
111+
id: checkout
113112
# Get the vault version metadata
114113
- uses: hashicorp/actions-set-product-version@v2
115114
id: set-product-version
@@ -120,9 +119,29 @@ jobs:
120119
id: metadata
121120
with:
122121
vault-version: ${{ steps.set-product-version.outputs.product-version }}
122+
# Get the elevated github token
123+
- if: steps.metadata.outputs.is-enterprise == 'true'
124+
id: vault-auth
125+
name: Vault Authenticate
126+
run: vault-auth
127+
- if: steps.metadata.outputs.is-enterprise == 'true'
128+
id: vault-secrets
129+
name: Fetch Vault Secrets
130+
uses: hashicorp/vault-action@d1720f055e0635fd932a1d2a48f87a666a57906c # v3.0.0
131+
with:
132+
url: ${{ steps.vault-auth.outputs.addr }}
133+
caCertificate: ${{ steps.vault-auth.outputs.ca_certificate }}
134+
token: ${{ steps.vault-auth.outputs.token }}
135+
secrets: |
136+
kv/data/github/${{ github.repository }}/github-token token | ELEVATED_GITHUB_TOKEN;
137+
# Determine the changed files
138+
- uses: ./.github/actions/changed-files
139+
id: changed-files
140+
with:
141+
github-token: ${{ steps.metadata.outputs.is-enterprise != 'true' && secrets.ELEVATED_GITHUB_TOKEN || steps.vault-secrets.outputs.ELEVATED_GITHUB_TOKEN }}
142+
# Make sure all required Go modules are cached at this point. We don't want all of the Go
143+
# tests and build jobs to download modules and race to upload them to the cache.
123144
- uses: ./.github/actions/set-up-go
124-
# Make sure all required Go modules are cached at this point. We don't want all of the Go
125-
# tests and build jobs to download modules and race to upload them to the cache.
126145
name: Ensure Go modules are cached
127146
with:
128147
github-token: ${{ secrets.ELEVATED_GITHUB_TOKEN }}
@@ -150,8 +169,8 @@ jobs:
150169
needs.setup.outputs.workflow-trigger == 'pull_request' &&
151170
needs.setup.outputs.is-draft == 'false' &&
152171
(
153-
needs.setup.outputs.ui-changed == 'true' ||
154-
needs.setup.outputs.app-changed == 'true'
172+
contains(fromJSON(needs.setup.outputs.changed-files).groups, 'ui') ||
173+
contains(fromJSON(needs.setup.outputs.changed-files).groups, 'app')
155174
)
156175
)
157176
needs: setup
@@ -200,7 +219,7 @@ jobs:
200219
# * The build/all label is present on a pull request or push.
201220
if: |
202221
needs.setup.outputs.workflow-trigger == 'schedule' ||
203-
needs.setup.outputs.app-changed == 'true' ||
222+
contains(fromJSON(needs.setup.outputs.changed-files).groups, 'app') ||
204223
contains(fromJSON(needs.setup.outputs.labels), 'build/all')
205224
needs:
206225
- setup
@@ -210,7 +229,7 @@ jobs:
210229
with:
211230
# The inputs defined here must be supported in both the build-artifacts-ce and
212231
# build-artifacts-ent workflows. The implementations should seek to keep a compatible interface.
213-
build-all: ${{ contains(fromJSON(needs.setup.outputs.labels), 'build/all') || needs.setup.outputs.workflow-trigger == 'schedule' }}
232+
build-all: ${{ contains(fromJSON(needs.setup.outputs.labels), 'build/all') || needs.setup.outputs.workflow-trigger == 'schedule' || contains(fromJSON(needs.setup.outputs.changed-files).groups, 'gomod') }}
214233
build-date: ${{ needs.setup.outputs.build-date }}
215234
checkout-ref: ${{ needs.setup.outputs.checkout-ref }}
216235
compute-build: ${{ needs.setup.outputs.compute-build }}

0 commit comments

Comments
 (0)