Skip to content

Commit 7536ee5

Browse files
committed
Make Java test workflow reusable
1 parent ebb6269 commit 7536ee5

File tree

2 files changed

+214
-153
lines changed

2 files changed

+214
-153
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
name: Reusable Java Testing Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
java-versions:
7+
description: 'JSON array of Java versions to test against'
8+
required: false
9+
type: string
10+
default: '["8", "16", "18", "19"]'
11+
platforms:
12+
description: 'JSON array of platforms to run tests on'
13+
required: false
14+
type: string
15+
default: '["ubuntu-latest"]'
16+
test-script:
17+
description: 'Test script to execute'
18+
required: false
19+
type: string
20+
default: './run-tests.sh'
21+
examples-script:
22+
description: 'Examples script to execute'
23+
required: false
24+
type: string
25+
default: './check-examples.sh'
26+
enable-status-reporting:
27+
description: 'Whether to post status checks to datadog-api-spec repo'
28+
required: false
29+
type: boolean
30+
default: false
31+
status-context:
32+
description: 'Context for status checks'
33+
required: false
34+
type: string
35+
default: 'master/unit'
36+
secrets:
37+
PIPELINE_GITHUB_APP_ID:
38+
required: false
39+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
40+
required: false
41+
DD_API_KEY:
42+
required: false
43+
44+
jobs:
45+
pre-commit:
46+
runs-on: ubuntu-latest
47+
if: >
48+
(github.event.pull_request.draft == false &&
49+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
50+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
51+
github.event_name == 'schedule'
52+
steps:
53+
- name: Get GitHub App token
54+
id: get_token
55+
uses: actions/create-github-app-token@v1
56+
with:
57+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
58+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
59+
- uses: actions/checkout@v3
60+
with:
61+
fetch-depth: 0
62+
ref: ${{ github.event.pull_request.head.sha }}
63+
token: ${{ steps.get_token.outputs.token }}
64+
- uses: actions/setup-python@v4
65+
with:
66+
python-version: '3.11'
67+
- name: Install pre-commit
68+
run: python -m pip install pre-commit
69+
- name: set PY
70+
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
71+
- uses: actions/cache@v3
72+
with:
73+
path: ~/.cache/pre-commit
74+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
75+
- name: Install Java
76+
uses: actions/setup-java@v3
77+
with:
78+
java-version: "16"
79+
distribution: "temurin"
80+
cache: "maven"
81+
- id: pre_commit
82+
name: Run pre-commit
83+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
84+
run: |
85+
wget https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar -O google-java-format.jar
86+
pre-commit run --verbose --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
87+
env:
88+
FROM_REF: ${{ github.event.pull_request.base.sha }}
89+
TO_REF: ${{ github.event.pull_request.head.sha }}
90+
- name: Commit changes
91+
if: ${{ failure() }}
92+
run: |-
93+
git add -A
94+
git config user.name "${GIT_AUTHOR_NAME}"
95+
git config user.email "${GIT_AUTHOR_EMAIL}"
96+
git commit -m "pre-commit fixes"
97+
git push origin "HEAD:${HEAD_REF}"
98+
exit 1
99+
env:
100+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
101+
GIT_AUTHOR_EMAIL: "[email protected]"
102+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
103+
- id: pre_commit_schedule
104+
name: Run pre-commit in schedule
105+
if: github.event_name == 'schedule'
106+
run: |
107+
pre-commit run --all-files --show-diff-on-failure --color=always
108+
109+
javadoc:
110+
runs-on: ubuntu-latest
111+
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
112+
steps:
113+
- uses: actions/checkout@v3
114+
- name: Install Java
115+
uses: actions/setup-java@v3
116+
with:
117+
java-version: "8"
118+
distribution: "temurin"
119+
cache: "maven"
120+
- name: Build javadoc
121+
run: mvn javadoc:javadoc
122+
shell: bash
123+
124+
shading:
125+
runs-on: ubuntu-latest
126+
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
127+
steps:
128+
- uses: actions/checkout@v3
129+
- name: Install Java
130+
uses: actions/setup-java@v3
131+
with:
132+
java-version: "8"
133+
distribution: "temurin"
134+
cache: "maven"
135+
- name: Check all dependencies shaded
136+
run: ./shade-test.sh
137+
138+
test:
139+
strategy:
140+
matrix:
141+
java-version: ${{ fromJSON(inputs.java-versions) }}
142+
platform: ${{ fromJSON(inputs.platforms) }}
143+
runs-on: ${{ matrix.platform }}
144+
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
145+
steps:
146+
- name: Checkout code
147+
uses: actions/checkout@v3
148+
- name: Install Java
149+
uses: actions/setup-java@v3
150+
with:
151+
java-version: ${{ matrix.java-version }}
152+
distribution: "temurin"
153+
cache: "maven"
154+
- name: Configure Datadog Test Optimization
155+
uses: datadog/test-visibility-github-action@v2
156+
with:
157+
languages: java
158+
api_key: ${{ secrets.DD_API_KEY }}
159+
- name: Test
160+
run: ${{ inputs.test-script }}
161+
env:
162+
DD_CIVISIBILITY_COMPILER_PLUGIN_AUTO_CONFIGURATION_ENABLED: "false"
163+
164+
examples:
165+
runs-on: ubuntu-latest
166+
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
167+
steps:
168+
- uses: actions/checkout@v3
169+
- name: Install Java
170+
uses: actions/setup-java@v3
171+
with:
172+
java-version: "16"
173+
distribution: "temurin"
174+
cache: "maven"
175+
- name: Check examples
176+
run: ${{ inputs.examples-script }}
177+
shell: bash
178+
179+
report:
180+
runs-on: ubuntu-latest
181+
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting
182+
needs:
183+
- test
184+
- examples
185+
- javadoc
186+
steps:
187+
- name: Get GitHub App token
188+
if: github.event_name == 'pull_request'
189+
id: get_token
190+
uses: actions/create-github-app-token@v1
191+
with:
192+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
193+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
194+
repositories: datadog-api-spec
195+
- name: Post status check
196+
uses: DataDog/github-actions/post-status-check@v2
197+
with:
198+
github-token: ${{ steps.get_token.outputs.token }}
199+
repo: datadog-api-spec
200+
status: ${{ (needs.javadoc.result == 'cancelled' || needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.javadoc.result == 'success' && needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
201+
context: ${{ inputs.status-context }}

.github/workflows/test.yml

Lines changed: 13 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -19,161 +19,21 @@ concurrency:
1919
cancel-in-progress: true
2020

2121
jobs:
22-
pre-commit:
23-
runs-on: ubuntu-latest
22+
java-test:
23+
uses: ./.github/workflows/reusable-java-test.yml
2424
if: >
2525
(github.event.pull_request.draft == false &&
2626
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
2727
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
2828
github.event_name == 'schedule'
29-
steps:
30-
- name: Get GitHub App token
31-
id: get_token
32-
uses: actions/create-github-app-token@v1
33-
with:
34-
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
35-
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
36-
- uses: actions/checkout@v3
37-
with:
38-
fetch-depth: 0
39-
ref: ${{ github.event.pull_request.head.sha }}
40-
token: ${{ steps.get_token.outputs.token }}
41-
- uses: actions/setup-python@v4
42-
with:
43-
python-version: '3.11'
44-
- name: Install pre-commit
45-
run: python -m pip install pre-commit
46-
- name: set PY
47-
run: echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
48-
- uses: actions/cache@v3
49-
with:
50-
path: ~/.cache/pre-commit
51-
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
52-
- name: Install Java
53-
uses: actions/setup-java@v3
54-
with:
55-
java-version: "16"
56-
distribution: "temurin"
57-
cache: "maven"
58-
- id: pre_commit
59-
name: Run pre-commit
60-
if: github.event.action != 'closed' && github.event.pull_request.merged != true
61-
run: |
62-
wget https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar -O google-java-format.jar
63-
pre-commit run --verbose --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
64-
env:
65-
FROM_REF: ${{ github.event.pull_request.base.sha }}
66-
TO_REF: ${{ github.event.pull_request.head.sha }}
67-
- name: Commit changes
68-
if: ${{ failure() }}
69-
run: |-
70-
git add -A
71-
git config user.name "${GIT_AUTHOR_NAME}"
72-
git config user.email "${GIT_AUTHOR_EMAIL}"
73-
git commit -m "pre-commit fixes"
74-
git push origin "HEAD:${HEAD_REF}"
75-
exit 1
76-
env:
77-
HEAD_REF: ${{ github.event.pull_request.head.ref }}
78-
- id: pre_commit_schedule
79-
name: Run pre-commit in schedule
80-
if: github.event_name == 'schedule'
81-
run: |
82-
pre-commit run --all-files --show-diff-on-failure --color=always
83-
84-
javadoc:
85-
runs-on: ubuntu-latest
86-
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
87-
steps:
88-
- uses: actions/checkout@v3
89-
- name: Install Java
90-
uses: actions/setup-java@v3
91-
with:
92-
java-version: "8"
93-
distribution: "temurin"
94-
cache: "maven"
95-
- name: Build javadoc
96-
run: mvn javadoc:javadoc
97-
shell: bash
98-
99-
shading:
100-
runs-on: ubuntu-latest
101-
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
102-
steps:
103-
- uses: actions/checkout@v3
104-
- name: Install Java
105-
uses: actions/setup-java@v3
106-
with:
107-
java-version: "8"
108-
distribution: "temurin"
109-
cache: "maven"
110-
- name: Check all dependencies shaded
111-
run: ./shade-test.sh
112-
113-
test:
114-
strategy:
115-
matrix:
116-
# see DEVELOPMENT.md for information why we're using this specific combination
117-
# of JDKs to run unit tests on
118-
java-version: ["8", "16", "18", "19"]
119-
120-
runs-on: ubuntu-latest
121-
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
122-
steps:
123-
- name: Checkout code
124-
uses: actions/checkout@v3
125-
- name: Install Java
126-
uses: actions/setup-java@v3
127-
with:
128-
java-version: ${{ matrix.java-version }}
129-
distribution: "temurin"
130-
cache: "maven"
131-
- name: Configure Datadog Test Optimization
132-
uses: datadog/test-visibility-github-action@v2
133-
with:
134-
languages: java
135-
api_key: ${{ secrets.DD_API_KEY }}
136-
- name: Test
137-
run: ./run-tests.sh
138-
env:
139-
DD_CIVISIBILITY_COMPILER_PLUGIN_AUTO_CONFIGURATION_ENABLED: "false"
140-
141-
examples:
142-
runs-on: ubuntu-latest
143-
if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule'
144-
steps:
145-
- uses: actions/checkout@v3
146-
- name: Install Java
147-
uses: actions/setup-java@v3
148-
with:
149-
java-version: "16"
150-
distribution: "temurin"
151-
cache: "maven"
152-
- name: Check examples
153-
run: ./check-examples.sh
154-
shell: bash
155-
156-
report:
157-
runs-on: ubuntu-latest
158-
if: always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
159-
needs:
160-
- test
161-
- examples
162-
- javadoc
163-
steps:
164-
- name: Get GitHub App token
165-
if: github.event_name == 'pull_request'
166-
id: get_token
167-
uses: actions/create-github-app-token@v1
168-
with:
169-
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
170-
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
171-
repositories: datadog-api-spec
172-
- name: Post status check
173-
uses: DataDog/github-actions/post-status-check@v2
174-
with:
175-
github-token: ${{ steps.get_token.outputs.token }}
176-
repo: datadog-api-spec
177-
status: ${{ (needs.javadoc.result == 'cancelled' || needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.javadoc.result == 'success' && needs.test.result == 'success' && needs.examples.result
178-
== 'success' && 'success' || 'failure' }}
179-
context: master/unit
29+
with:
30+
java-versions: '["8", "16", "18", "19"]'
31+
platforms: '["ubuntu-latest"]'
32+
test-script: './run-tests.sh'
33+
examples-script: './check-examples.sh'
34+
enable-status-reporting: ${{ contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') }}
35+
status-context: 'master/unit'
36+
secrets:
37+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
38+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
39+
DD_API_KEY: ${{ secrets.DD_API_KEY }}

0 commit comments

Comments
 (0)