Skip to content

Commit 48f9a66

Browse files
committed
Support JavaScript dependency installation in reusable GitHub Workflow
1 parent 18b95c9 commit 48f9a66

File tree

3 files changed

+169
-63
lines changed

3 files changed

+169
-63
lines changed

.github/workflows/internal-typescript-code-analysis.yml

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -46,78 +46,23 @@ jobs:
4646
runs-on: ubuntu-latest
4747
outputs:
4848
analysis-name: ${{ steps.set-analysis-name.outputs.analysis-name }}
49-
sources-upload-name: ${{ steps.set-sources-upload-name.outputs.sources-upload-name }}
49+
project-version: ${{ env.REACT_ROUTER_VERSION }}
5050

5151
env:
5252
PROJECT_NAME: react-router
5353
# Version variable name matches renovate.json configuration entry
5454
REACT_ROUTER_VERSION: 6.30.2
5555

56-
steps:
57-
- name: (Prepare Code to Analyze) Checkout GIT repository
58-
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
59-
60-
56+
steps:
6157
- name: Set Set output variable 'analysis-name'
6258
id: set-analysis-name
6359
run: echo "analysis-name=${{ env.PROJECT_NAME }}-${{ env.REACT_ROUTER_VERSION }}" >> "$GITHUB_OUTPUT"
64-
65-
- name: Setup temp directory if missing
66-
run: mkdir -p ./temp
67-
68-
- name: Setup Cache for "temp/downloads" folder
69-
uses: actions/cache@v4
70-
with:
71-
path: ./temp/downloads
72-
key:
73-
${{ runner.os }}-${{ hashFiles('**/*.sh') }}
74-
75-
- name: Download ${{ steps.set-analysis-name.outputs.analysis-name }}
76-
working-directory: temp
77-
run: |
78-
mkdir -p ${{ steps.set-analysis-name.outputs.analysis-name }}
79-
cd ${{ steps.set-analysis-name.outputs.analysis-name }}
80-
echo "Working directory: $( pwd -P )"
81-
./../../scripts/downloader/downloadReactRouter.sh ${{ env.REACT_ROUTER_VERSION }}
82-
83-
- name: (Prepare Code to Analyze) Setup pnpm for react-router
84-
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
85-
with:
86-
package_json_file: temp/${{ steps.set-analysis-name.outputs.analysis-name }}/source/${{ steps.set-analysis-name.outputs.analysis-name }}/package.json
87-
88-
- name: (Prepare Code to Analyze) Install dependencies with pnpm
89-
working-directory: temp/${{ steps.set-analysis-name.outputs.analysis-name }}/source/${{ steps.set-analysis-name.outputs.analysis-name }}
90-
run: pnpm install --frozen-lockfile --strict-peer-dependencies
91-
92-
- name: Debug folder structure in temp directory
93-
if: runner.debug == '1'
94-
working-directory: temp
95-
run: |
96-
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
97-
98-
- name: (Prepare Code to Analyze) Generate ARTIFACT_UPLOAD_ID
99-
run: echo "ARTIFACT_UPLOAD_ID=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 10)" >> $GITHUB_ENV
100-
101-
- name: (Prepare Code to Analyze) Set sources-upload-name
102-
id: set-sources-upload-name
103-
run: echo "sources-upload-name=${{ steps.set-analysis-name.outputs.analysis-name }}-analysis-sources_input-${{ env.ARTIFACT_UPLOAD_ID }}" >> "$GITHUB_OUTPUT"
104-
105-
- name: (Prepare Code to Analyze) Upload code to analyze
106-
if: success()
107-
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
108-
with:
109-
name: ${{ steps.set-sources-upload-name.outputs.sources-upload-name }}
110-
path: ./temp/${{ steps.set-analysis-name.outputs.analysis-name }}/source
111-
include-hidden-files: true
112-
if-no-files-found: error
113-
retention-days: 1
114-
115-
11660

11761
analyze-code-graph:
11862
needs: [prepare-code-to-analyze]
11963
uses: ./.github/workflows/public-analyze-code-graph.yml
12064
with:
12165
analysis-name: ${{ needs.prepare-code-to-analyze.outputs.analysis-name }}
122-
sources-upload-name: ${{ needs.prepare-code-to-analyze.outputs.sources-upload-name }}
66+
source-repository: https://github.com/remix-run/react-router.git
67+
source-repository-branch: react-router@${{ needs.prepare-code-to-analyze.outputs.project-version }}
12368
jupyter-pdf: "false"
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Typescript Code Upload Example for Graph Analysis
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
# Ignore changes in documentation, general configuration and reports for push events
8+
paths-ignore:
9+
- '**/*.md'
10+
- '**/*.txt'
11+
- '!requirements.txt'
12+
- '**/*.css'
13+
- '**/*.html'
14+
- '**/*.js'
15+
- '.gitignore'
16+
- '.gitattributes'
17+
- 'renovate.json'
18+
- 'renovate-presets/**'
19+
- 'changelogTemplate.mustache'
20+
- '**.code-workspace'
21+
- '.github/workflows/internal-java-code-analysis.yml'
22+
- '.github/workflows/*documentation.yml'
23+
pull_request:
24+
branches:
25+
- main
26+
# Ignore changes in documentation, general configuration and reports for pull request events
27+
paths-ignore:
28+
- '**/*.md'
29+
- '**/*.txt'
30+
- '!requirements.txt'
31+
- '**/*.css'
32+
- '**/*.html'
33+
- '**/*.js'
34+
- '.gitignore'
35+
- '.gitattributes'
36+
- 'renovate.json'
37+
- 'renovate-presets/**'
38+
- 'changelogTemplate.mustache'
39+
- '**.code-workspace'
40+
- '.github/workflows/internal-java-code-analysis.yml'
41+
- '.github/workflows/*documentation.yml'
42+
43+
jobs:
44+
45+
prepare-code-to-analyze:
46+
runs-on: ubuntu-latest
47+
outputs:
48+
analysis-name: ${{ steps.set-analysis-name.outputs.analysis-name }}
49+
sources-upload-name: ${{ steps.set-sources-upload-name.outputs.sources-upload-name }}
50+
51+
env:
52+
PROJECT_NAME: react-router
53+
# Version variable name matches renovate.json configuration entry
54+
REACT_ROUTER_6_VERSION: 6.30.2
55+
56+
steps:
57+
- name: (Prepare Code to Analyze) Checkout GIT repository
58+
uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
59+
60+
61+
- name: Set Set output variable 'analysis-name'
62+
id: set-analysis-name
63+
run: echo "analysis-name=${{ env.PROJECT_NAME }}-${{ env.REACT_ROUTER_6_VERSION }}" >> "$GITHUB_OUTPUT"
64+
65+
- name: Setup temp directory if missing
66+
run: mkdir -p ./temp
67+
68+
- name: Setup Cache for "temp/downloads" folder
69+
uses: actions/cache@v4
70+
with:
71+
path: ./temp/downloads
72+
key:
73+
${{ runner.os }}-${{ hashFiles('**/*.sh') }}
74+
75+
- name: Download ${{ steps.set-analysis-name.outputs.analysis-name }}
76+
working-directory: temp
77+
run: |
78+
mkdir -p ${{ steps.set-analysis-name.outputs.analysis-name }}
79+
cd ${{ steps.set-analysis-name.outputs.analysis-name }}
80+
echo "Working directory: $( pwd -P )"
81+
./../../scripts/downloader/downloadReactRouter.sh ${{ env.REACT_ROUTER_6_VERSION }}
82+
83+
- name: (Prepare Code to Analyze) Setup pnpm for react-router
84+
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
85+
with:
86+
package_json_file: temp/${{ steps.set-analysis-name.outputs.analysis-name }}/source/${{ steps.set-analysis-name.outputs.analysis-name }}/package.json
87+
88+
- name: (Prepare Code to Analyze) Install dependencies with pnpm
89+
working-directory: temp/${{ steps.set-analysis-name.outputs.analysis-name }}/source/${{ steps.set-analysis-name.outputs.analysis-name }}
90+
run: pnpm install --frozen-lockfile --strict-peer-dependencies
91+
92+
- name: Debug folder structure in temp directory
93+
if: runner.debug == '1'
94+
working-directory: temp
95+
run: |
96+
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
97+
98+
- name: (Prepare Code to Analyze) Generate ARTIFACT_UPLOAD_ID
99+
run: echo "ARTIFACT_UPLOAD_ID=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 10)" >> $GITHUB_ENV
100+
101+
- name: (Prepare Code to Analyze) Set sources-upload-name
102+
id: set-sources-upload-name
103+
run: echo "sources-upload-name=${{ steps.set-analysis-name.outputs.analysis-name }}-analysis-sources_input-${{ env.ARTIFACT_UPLOAD_ID }}" >> "$GITHUB_OUTPUT"
104+
105+
- name: (Prepare Code to Analyze) Upload code to analyze
106+
if: success()
107+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
108+
with:
109+
name: ${{ steps.set-sources-upload-name.outputs.sources-upload-name }}
110+
path: ./temp/${{ steps.set-analysis-name.outputs.analysis-name }}/source
111+
include-hidden-files: true
112+
if-no-files-found: error
113+
retention-days: 1
114+
115+
116+
117+
explore-code-graph:
118+
needs: [prepare-code-to-analyze]
119+
uses: ./.github/workflows/public-analyze-code-graph.yml
120+
with:
121+
analysis-name: ${{ needs.prepare-code-to-analyze.outputs.analysis-name }}
122+
sources-upload-name: ${{ needs.prepare-code-to-analyze.outputs.sources-upload-name }}
123+
jupyter-pdf: "false"
124+
analysis-arguments: "--explore" # Only setup the Graph, do not generate any reports

.github/workflows/public-analyze-code-graph.yml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ on:
3232
containing the content of the 'source' directory for the analysis.
3333
Also supports sub-folders for multiple source code bases.
3434
Please use 'include-hidden-files: true' if you also want to upload the git history.
35+
Note: JavaScript dependencies will NOT be installed automatically when using this option. This needs to be done before uploading the sources.
3536
required: false
3637
type: string
3738
default: ''
@@ -40,6 +41,7 @@ on:
4041
The URL of the source repository to analyze. For now, only GitHub repositories are supported.
4142
This can be used instead of 'sources-upload-name' to directly analyze a repository without uploading artifacts first.
4243
It can also be used in addition to 'sources-upload-name' to analyze both uploaded sources and a repository.
44+
If specified, JavaScript dependencies will be installed automatically if a package.json file is found in the repository.
4345
required: false
4446
type: string
4547
default: ''
@@ -143,7 +145,8 @@ jobs:
143145
distribution: "temurin"
144146
java-version: ${{ matrix.java }}
145147

146-
# "Setup Python" can be skipped if jupyter notebook analysis-results aren't needed
148+
# "Setup Python" could be skipped if jupyter notebook analysis-results aren't needed or .venv is used.
149+
# However, since this is a reuseable workflow, we always do it here.
147150
- name: (Python Setup) Use version ${{ matrix.python }} with Conda package manager Miniforge
148151
if: inputs.use-venv_virtual_python_environment == 'false'
149152
id: prepare-conda-environment
@@ -203,7 +206,31 @@ jobs:
203206
working-directory: temp/${{ inputs.analysis-name }}
204207
run: ./../../scripts/cloneGitRepository.sh --url "${{ inputs.source-repository }}" --branch "${{ inputs.source-repository-branch }}" --history-only "${{ inputs.source-repository-history-only }}" --target "source/${{ inputs.analysis-name }}"
205208

206-
- name: (Code Analysis Setup) Install JavaScript dependencies in cloned source repository if needed
209+
- name: (Code Analysis JavaScript Setup) Detect node version file .nvmrc in cloned source repository
210+
if: inputs.source-repository != ''
211+
working-directory: temp/${{ inputs.analysis-name }}/source/${{ inputs.analysis-name }}
212+
run: echo "nodeVersionFileDetected=$(if [ -f ".nvmrc" ]; then echo "true"; else echo "false"; fi)" >> $GITHUB_ENV
213+
- name: (Code Analysis JavaScript Setup) Detect pnpm project in cloned source repository
214+
if: inputs.source-repository != ''
215+
working-directory: temp/${{ inputs.analysis-name }}/source/${{ inputs.analysis-name }}
216+
run: echo "pnpmDetected=$(if [ -f "pnpm-lock.yaml" ]; then echo "true"; else echo "false"; fi)" >> $GITHUB_ENV
217+
- name: (Code Analysis JavaScript Setup) Setup Node.js with version in .nvmrc for cloned source repository
218+
if: inputs.source-repository != '' && env.nodeVersionFileDetected == 'true'
219+
uses: actions/setup-node@v6.1.0
220+
with:
221+
node-version-file: temp/${{ inputs.analysis-name }}/source/${{ inputs.analysis-name }}/.nvmrc
222+
- name: (Code Analysis JavaScript Setup) Setup Node.js (long-term support version fallback, no .nvmrc) for cloned source repository
223+
if: inputs.source-repository != '' && env.nodeVersionFileDetected != 'true'
224+
uses: actions/setup-node@v6.1.0
225+
with:
226+
node-version: 'lts/*'
227+
- name: (Code Analysis JavaScript Setup) Setup pnpm for cloned source repository
228+
if: inputs.source-repository != '' && env.pnpmDetected == 'true'
229+
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
230+
with:
231+
package_json_file: temp/${{ inputs.analysis-name }}/source/${{ inputs.analysis-name }}/package.json
232+
run_install: false
233+
- name: (Code Analysis JavaScript Setup) Install JavaScript dependencies in cloned source repository if needed
207234
if: inputs.source-repository != ''
208235
working-directory: temp/${{ inputs.analysis-name }}
209236
run: ./../../scripts/installJavaScriptDependencies.sh
@@ -243,16 +270,26 @@ jobs:
243270
id: set-analysis-results-artifact-name
244271
run: echo "uploaded-analysis-results-artifact-name=code-analysis-results-${{ env.ENVIRONMENT_INFO }}" >> $GITHUB_OUTPUT
245272

246-
# Upload successful analysis-results in case they are needed for troubleshooting
273+
# Upload successful analysis-results as the main output artifact
247274
- name: (Code Analysis Results) Archive successful analysis-results
248-
if: success()
275+
if: success() && !contains(inputs.analysis-arguments, '--explore')
249276
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
250277
with:
251278
name: ${{ steps.set-analysis-results-artifact-name.outputs.uploaded-analysis-results-artifact-name }}
252279
path: ./temp/${{ inputs.analysis-name }}/reports/*
253280
if-no-files-found: error
254281
retention-days: ${{ inputs.retention-days }}
255282

283+
# Upload logs if analysis results had been skipped ("--explore" analysis option)
284+
- name: (Code Analysis Results) Archive successful analysis-results
285+
if: success() && contains(inputs.analysis-arguments, '--explore')
286+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
287+
with:
288+
name: ${{ steps.set-analysis-results-artifact-name.outputs.uploaded-analysis-results-artifact-name }}
289+
path: ./temp/${{ inputs.analysis-name }}/runtime/*
290+
if-no-files-found: error
291+
retention-days: ${{ inputs.retention-days }}
292+
256293

257294
# Upload logs and unfinished analysis-results in case of an error for troubleshooting
258295
- name: (Code Analysis Results) Archive failed run with logs and unfinished analysis-results

0 commit comments

Comments
 (0)