Skip to content

Commit f09aece

Browse files
committed
Make the typescript CI in a reusable workflow
1 parent d277ed9 commit f09aece

File tree

5 files changed

+298
-97
lines changed

5 files changed

+298
-97
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Reusable Complete CI Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
node-versions:
12+
description: 'JSON array of Node.js versions to test against'
13+
required: false
14+
type: string
15+
default: '["16", "18"]'
16+
platforms:
17+
description: 'JSON array of platforms to run tests on'
18+
required: false
19+
type: string
20+
default: '["ubuntu-latest"]'
21+
test-script:
22+
description: 'Test script to execute'
23+
required: false
24+
type: string
25+
default: './run-tests.sh'
26+
examples-script:
27+
description: 'Examples script to execute'
28+
required: false
29+
type: string
30+
default: './check-examples.sh'
31+
node-version:
32+
description: 'Node.js version to use'
33+
required: false
34+
type: string
35+
default: '16'
36+
37+
secrets:
38+
PIPELINE_GITHUB_APP_ID:
39+
required: false
40+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
41+
required: false
42+
# Integration test secrets
43+
DD_API_KEY:
44+
required: false
45+
DD_CLIENT_API_KEY:
46+
required: false
47+
DD_CLIENT_APP_KEY:
48+
required: false
49+
SLEEP_AFTER_REQUEST:
50+
required: false
51+
52+
jobs:
53+
pre-commit:
54+
uses: ./.github/workflows/reusable-pre-commit.yml
55+
with:
56+
target-branch: ${{ inputs.target-branch }}
57+
enable-commit-changes: false # Don't auto-commit in external CI
58+
secrets:
59+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
60+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
61+
62+
test:
63+
uses: ./.github/workflows/reusable-typescript-test.yml
64+
with:
65+
target-branch: ${{ inputs.target-branch }}
66+
node-versions: ${{ inputs.node-versions }}
67+
platforms: ${{ inputs.platforms }}
68+
test-script: ${{ inputs.test-script }}
69+
secrets:
70+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
71+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
72+
73+
examples:
74+
uses: ./.github/workflows/reusable-examples.yml
75+
with:
76+
target-branch: ${{ inputs.target-branch }}
77+
examples-script: ${{ inputs.examples-script }}
78+
node-version: ${{ inputs.node-version }}
79+
80+
integration:
81+
uses: ./.github/workflows/reusable-integration-test.yml
82+
with:
83+
target-branch: ${{ inputs.target-branch }}
84+
secrets:
85+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
86+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
87+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
88+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
89+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
90+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST }}
91+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Reusable Examples Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
examples-script:
12+
description: 'Examples script to execute'
13+
required: false
14+
type: string
15+
default: './check-examples.sh'
16+
node-version:
17+
description: 'Node.js version to use for examples'
18+
required: false
19+
type: string
20+
default: '16'
21+
secrets:
22+
PIPELINE_GITHUB_APP_ID:
23+
required: false
24+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
25+
required: false
26+
27+
jobs:
28+
examples:
29+
runs-on: ubuntu-latest
30+
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'
31+
steps:
32+
- uses: actions/checkout@v3
33+
with:
34+
repository: DataDog/datadog-api-client-typescript
35+
ref: ${{ inputs.target-branch || github.ref }}
36+
- name: Set up Node ${{ inputs.node-version }}
37+
uses: actions/setup-node@v3
38+
with:
39+
node-version: ${{ inputs.node-version }}
40+
cache: 'yarn'
41+
- name: Check examples
42+
run: ${{ inputs.examples-script }}
43+
shell: bash

.github/workflows/test_integration.yml renamed to .github/workflows/reusable-integration-test.yml

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Run Integration Tests
1+
name: Reusable Integration Test Workflow
22

33
permissions:
44
contents: read
@@ -16,6 +16,41 @@ on:
1616
- master
1717
schedule:
1818
- cron: "0 4 * * *"
19+
workflow_call:
20+
inputs:
21+
target-branch:
22+
description: 'Branch to checkout and test (defaults to the calling branch)'
23+
required: false
24+
type: string
25+
default: ''
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: 'integration'
36+
target-repo:
37+
description: 'Repository to post status to'
38+
required: false
39+
type: string
40+
default: 'datadog-api-spec'
41+
secrets:
42+
PIPELINE_GITHUB_APP_ID:
43+
required: false
44+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
45+
required: false
46+
DD_API_KEY:
47+
required: true
48+
DD_CLIENT_API_KEY:
49+
required: true
50+
DD_CLIENT_APP_KEY:
51+
required: true
52+
SLEEP_AFTER_REQUEST:
53+
required: false
1954

2055
concurrency:
2156
group: integration-${{ github.head_ref }}
@@ -48,17 +83,20 @@ jobs:
4883
with:
4984
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
5085
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
51-
repositories: datadog-api-spec
86+
repositories: ${{ inputs.target-repo || 'datadog-api-spec' }}
5287
- name: Checkout code
5388
uses: actions/checkout@v3
89+
with:
90+
repository: DataDog/datadog-api-client-typescript
91+
ref: ${{ inputs.target-branch || github.ref }}
5492
- name: Post pending status check
55-
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
93+
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')
5694
uses: DataDog/github-actions/post-status-check@v2
5795
with:
5896
github-token: ${{ steps.get_token.outputs.token }}
59-
repo: datadog-api-spec
97+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
6098
status: pending
61-
context: integration
99+
context: ${{ inputs.status-context || 'integration' }}
62100
- name: Set up Node 16
63101
uses: actions/setup-node@v3
64102
with:
@@ -77,20 +115,20 @@ jobs:
77115
DD_TEST_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
78116
DD_TEST_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
79117
RECORD: "none"
80-
SLEEP_AFTER_REQUEST: "${{ vars.SLEEP_AFTER_REQUEST }}"
118+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST || vars.SLEEP_AFTER_REQUEST }}
81119
- name: Post failure status check
82-
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
120+
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')
83121
uses: DataDog/github-actions/post-status-check@v2
84122
with:
85123
github-token: ${{ steps.get_token.outputs.token }}
86-
repo: datadog-api-spec
124+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
87125
status: failure
88-
context: integration
126+
context: ${{ inputs.status-context || 'integration' }}
89127
- name: Post success status check
90-
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')"
128+
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')"
91129
uses: DataDog/github-actions/post-status-check@v2
92130
with:
93131
github-token: ${{ steps.get_token.outputs.token }}
94-
repo: datadog-api-spec
132+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
95133
status: success
96-
context: integration
134+
context: ${{ inputs.status-context || 'integration' }}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Reusable Pre-commit Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
enable-commit-changes:
12+
description: 'Whether to commit and push pre-commit fixes'
13+
required: false
14+
type: boolean
15+
default: true
16+
secrets:
17+
PIPELINE_GITHUB_APP_ID:
18+
required: false
19+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
20+
required: false
21+
22+
jobs:
23+
pre-commit:
24+
runs-on: ubuntu-latest
25+
if: >
26+
(github.event.pull_request.draft == false &&
27+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
28+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
29+
github.event_name == 'schedule'
30+
steps:
31+
- name: Get GitHub App token
32+
if: inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
33+
id: get_token
34+
uses: actions/create-github-app-token@v1
35+
with:
36+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
37+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
38+
- uses: actions/checkout@v3
39+
if: github.event.pull_request.head.repo.full_name == github.repository
40+
with:
41+
fetch-depth: 0
42+
repository: DataDog/datadog-api-client-typescript
43+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
44+
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
45+
- uses: actions/checkout@v3
46+
if: github.event.pull_request.head.repo.full_name != github.repository
47+
with:
48+
repository: DataDog/datadog-api-client-typescript
49+
ref: ${{ inputs.target-branch || github.ref }}
50+
- uses: actions/setup-python@v4
51+
with:
52+
python-version: '3.11'
53+
- name: Install pre-commit
54+
run: python -m pip install pre-commit
55+
- name: set PY
56+
run: echo "PY=$(python -c 'import hashlib, sys, platform;print(hashlib.sha256(platform.python_version().encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_ENV
57+
- uses: actions/cache@v3
58+
with:
59+
path: ~/.cache/pre-commit
60+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
61+
- id: pre_commit
62+
name: Run pre-commit
63+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
64+
run: |
65+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
66+
env:
67+
FROM_REF: ${{ github.event.pull_request.base.sha }}
68+
TO_REF: ${{ github.event.pull_request.head.sha }}
69+
- name: Commit changes
70+
if: failure() && inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
71+
run: |-
72+
git add -A
73+
git config user.name "${GIT_AUTHOR_NAME}"
74+
git config user.email "${GIT_AUTHOR_EMAIL}"
75+
git commit -m "pre-commit fixes"
76+
git push origin "HEAD:${HEAD_REF}"
77+
exit 1
78+
env:
79+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
80+
GIT_AUTHOR_EMAIL: "[email protected]"
81+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
82+
- id: pre_commit_schedule
83+
name: Run pre-commit in schedule
84+
if: github.event_name == 'schedule'
85+
run: |
86+
pre-commit run --all-files --show-diff-on-failure --color=always

0 commit comments

Comments
 (0)