diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5c31f3f..24b6072 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,24 +5,33 @@ on: branches: - main - 'release/*' + schedule: + - cron: '0 0 * * *' # Runs at midnight UTC every day jobs: test: + strategy: + matrix: + version: [14, 16, 18, 20] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: ./ + - uses: actions/checkout@v4 + - name: Upload reports using a glob pattern + uses: ./ with: - api-key: ${{secrets.DD_API_KEY}} - datadog-site: datad0g.com + api-key: ${{secrets.DD_API_KEY_CI_VISIBILITY}} logs: "true" - files: ./ci/fixtures/ - service: junit-upload-github-action + files: '**/fixtures/**' + service: junit-upload-github-action-tests env: ci - tags: "foo:bar,alpha:bravo" - - name: Tag pipeline with ci version + tags: "foo:bar,alpha:bravo,test.node.version:${{ matrix.version}}" + node-version: ${{ matrix.version}} + - name: Check that test data can be queried run: | - datadog-ci tag --level pipeline --tags "datadogci.version:`npm list -g @datadog/datadog-ci | grep @datadog/datadog-ci | awk -F' ' '{print $2}' | awk -F'@' '{print $3}' | tr -d '\n'`" + npm install @datadog/datadog-api-client + node ./check-junit-upload.js env: - DATADOG_API_KEY: ${{secrets.DD_API_KEY}} - DATADOG_SITE: datad0g.com \ No newline at end of file + EXTRA_TAGS: "@foo:bar @alpha:bravo @test.node.version:${{ matrix.version}}" + DD_API_KEY: ${{ secrets.DD_API_KEY_CI_VISIBILITY }} + DD_APP_KEY: ${{ secrets.DD_APP_KEY_CI_VISIBILITY }} + DD_SERVICE: junit-upload-github-action-tests \ No newline at end of file diff --git a/README.md b/README.md index ca4f544..0f3d3d9 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ The action has the following options: | `datadog-site` | The Datadog site to upload the files to. | True | `datadoghq.com` | | `files` | Path to file or folder containing XML files to upload | True | `.` | | `concurrency` | Controls the maximum number of concurrent file uploads | True | `20` | -| `node-version` | The node version to use to install the datadog-ci. It must be `>=10.24.1` | True | `16` | +| `node-version` | The node version to use to install the datadog-ci. It must be `>=14` | True | `20` | | `tags` | Optional extra tags to add to the tests | False | | | `env` | Optional environment to add to the tests | False | | | `logs` | When set to "true" enables forwarding content from the XML reports as Logs. The content inside ``, ``, and `` is collected as logs. Logs from elements inside a `` are automatically connected to the test. | False | | diff --git a/action.yaml b/action.yaml index a58346a..f8dac84 100644 --- a/action.yaml +++ b/action.yaml @@ -1,6 +1,6 @@ # Composite action to upload junit test result files to Datadog CI Visibility -name: 'Datadog JUnitXML Upload' -description: 'Upload JUnitXML reports files to Datadog CI Visibility' +name: "Datadog JUnitXML Upload" +description: "Upload JUnitXML reports files to Datadog CI Visibility" inputs: api-key: required: true @@ -23,7 +23,7 @@ inputs: node-version: required: true description: The node version used to install datadog-ci - default: "16" + default: "20" tags: required: false description: Datadog tags to associate with the uploaded test results. @@ -34,24 +34,21 @@ inputs: required: false description: Set to "true" to enable forwarding content from XML reports as logs. extra-args: - default: '' + default: "" description: Extra args to be passed to the datadog-ci cli. required: false runs: using: "composite" steps: - name: Install node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} - - name: Get Datadog CLI - shell: bash - run: npm install -g @datadog/datadog-ci - name: Upload the JUnit files if: ${{ inputs.logs == 'true' }} shell: bash run: | - datadog-ci junit upload \ + npx @datadog/datadog-ci junit upload \ --service ${{ inputs.service }} \ --logs \ --max-concurrency ${{ inputs.concurrency }} \ @@ -66,7 +63,7 @@ runs: if: ${{ inputs.logs != 'true' }} shell: bash run: | - datadog-ci junit upload \ + npx @datadog/datadog-ci junit upload \ --service ${{ inputs.service }} \ --max-concurrency ${{ inputs.concurrency }} \ ${{ inputs.extra-args }} \ diff --git a/check-junit-upload.js b/check-junit-upload.js new file mode 100644 index 0000000..7dd1855 --- /dev/null +++ b/check-junit-upload.js @@ -0,0 +1,61 @@ +'use strict' + +const { client, v2 } = require("@datadog/datadog-api-client") + +const configuration = client.createConfiguration(); +const apiInstance = new v2.CIVisibilityTestsApi(configuration); + +const EXPECTED_NUM_TESTS = 32 + +const params = { + filterQuery: `@test.service:${process.env.DD_SERVICE} @git.commit.sha:${process.env.GITHUB_SHA}`, + filterFrom: new Date(new Date().getTime() + -300 * 1000), // Last 5 minutes + filterTo: new Date(), + pageLimit: 50, +}; + +const CHECK_INTERVAL_SECONDS = 10 +const MAX_NUM_ATTEMPTS = 10 + +function getTestData (extraFilter) { + const finalFilterQuery = `${params.filterQuery} ${extraFilter}` + console.log(`🔎 Querying CI Visibility tests with ${finalFilterQuery}.`) + return apiInstance + .listCIAppTestEvents({ + ...params, + filterQuery: `${finalFilterQuery}`, + }) + .then(data => data.data) + .catch(error => console.error(error)) +} + +function waitFor (waitSeconds) { + return new Promise(resolve => setTimeout(() => resolve(), waitSeconds * 1000)) +} + +async function checkJunitUpload () { + let numAttempts = 0 + let isSuccess = false + let data = [] + while (numAttempts++ < MAX_NUM_ATTEMPTS && !isSuccess) { + data = await getTestData(`test_level:test ${process.env.EXTRA_TAGS}`) + if (data.length === EXPECTED_NUM_TESTS) { + isSuccess = true + } else { + const isLastAttempt = numAttempts === MAX_NUM_ATTEMPTS + if (!isLastAttempt) { + console.log(`🔁 Attempt number ${numAttempts} failed, retrying in ${CHECK_INTERVAL_SECONDS} seconds.`) + await waitFor(CHECK_INTERVAL_SECONDS) + } + } + } + if (isSuccess) { + console.log(`✅ Successful check: the API returned ${data.length} tests.`) + process.exit(0) + } else { + console.log(`❌ Failed check: the API returned ${data.length} tests but ${EXPECTED_NUM_TESTS} were expected.`) + process.exit(1) + } +} + +checkJunitUpload() \ No newline at end of file diff --git a/ci/fixtures/python-unittest.xml b/ci/fixtures/subfolder/python-unittest.xml similarity index 86% rename from ci/fixtures/python-unittest.xml rename to ci/fixtures/subfolder/python-unittest.xml index 6fa9d5f..5d03d19 100644 --- a/ci/fixtures/python-unittest.xml +++ b/ci/fixtures/subfolder/python-unittest.xml @@ -1,19 +1,19 @@ - - + + - + - + Some output @@ -21,7 +21,7 @@ - + - +