From 20afb1a9fc4d6f54161c0242de08fb431635d97c Mon Sep 17 00:00:00 2001 From: Konrad Pabjan Date: Thu, 17 Aug 2023 12:32:55 -0400 Subject: [PATCH] [Artifacts] Add tests for E2E artifact upload (#1497) * Add tests for E2E artifact upload * Trigger Build * Extra debug logs * Debug dumping GitHub Context * More logging * Minor cleanup * Trigger Build * Unique artifact name * Fix typo * Fix * Try using github-script * Potential fix * Cleanup * More cleanup --- .github/workflows/artifact-tests.yml | 64 +++++++------------ packages/artifact/package.json | 4 +- .../internal/shared/artifact-twirp-client.ts | 3 +- 3 files changed, 27 insertions(+), 44 deletions(-) diff --git a/.github/workflows/artifact-tests.yml b/.github/workflows/artifact-tests.yml index 1ebfa7c5..d3541984 100644 --- a/.github/workflows/artifact-tests.yml +++ b/.github/workflows/artifact-tests.yml @@ -31,11 +31,6 @@ jobs: with: node-version: 16.x - # In order to upload & download artifacts from a shell script, certain env variables need to be set that are only available in the - # node context. This runs a local action that gets and sets the necessary env variables that are needed - - name: Set env variables - uses: ./packages/artifact/__tests__/ci-test-action/ - # Need root node_modules because certain npm packages like jest are configured for the entire repository and it won't be possible # without these to just compile the artifacts package - name: Install root npm packages @@ -50,48 +45,35 @@ jobs: - name: Set artifact file contents shell: bash run: | - echo "non-gzip-artifact-content=hello" >> $GITHUB_ENV - echo "gzip-artifact-content=Some large amount of text that has a compression ratio that is greater than 100%. If greater than 100%, gzip is used to upload the file" >> $GITHUB_ENV - echo "empty-artifact-content=_EMPTY_" >> $GITHUB_ENV + echo "file1=hello from file 1" >> $GITHUB_ENV + echo "file2=hello from file 2" >> $GITHUB_ENV - name: Create files that will be uploaded run: | mkdir artifact-path - echo '${{ env.non-gzip-artifact-content }}' > artifact-path/world.txt - echo '${{ env.gzip-artifact-content }}' > artifact-path/gzip.txt - touch artifact-path/empty.txt + echo '${{ env.file1 }}' > artifact-path/first.txt + echo '${{ env.file2 }}' > artifact-path/second.txt - # We're using node -e to call the functions directly available in the @actions/artifact package - - name: Upload artifacts using uploadArtifact() - run: | - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-1',['artifact-path/world.txt'], process.argv[1]))" "${{ github.workspace }}" - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-2',['artifact-path/gzip.txt'], process.argv[1]))" "${{ github.workspace }}" - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-3',['artifact-path/empty.txt'], process.argv[1]))" "${{ github.workspace }}" + - uses: actions/github-script@v6 + with: + script: | + const artifact = require('./packages/artifact/lib/artifact') - - name: Download artifacts using downloadArtifact() - run: | - mkdir artifact-1-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-1','artifact-1-directory'))" - mkdir artifact-2-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-2','artifact-2-directory'))" - mkdir artifact-3-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-3','artifact-3-directory'))" + const artifactName = 'my-artifact-${{ matrix.runs-on }}' + console.log('artifactName: ' + artifactName) - - name: Verify downloadArtifact() - shell: bash - run: | - packages/artifact/__tests__/test-artifact-file.sh "artifact-1-directory/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "artifact-2-directory/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "artifact-3-directory/artifact-path/empty.txt" "${{ env.empty-artifact-content }}" + const fileContents = ['artifact-path/first.txt','artifact-path/second.txt'] - - name: Download artifacts using downloadAllArtifacts() - run: | - mkdir multi-artifact-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadAllArtifacts('multi-artifact-directory'))" + const uploadResult = await artifact.create().uploadArtifact(artifactName, fileContents, './') + console.log(uploadResult) - - name: Verify downloadAllArtifacts() - shell: bash - run: | - packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-1/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-2/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-3/artifact-path/empty.txt" "${{ env.empty-artifact-content }}" + const success = uploadResult.success + const size = uploadResult.size + const id = uploadResult.id + + if (!success) { + throw new Error('Failed to upload artifact') + } else { + console.log(`Successfully uploaded artifact ${id}`) + } + diff --git a/packages/artifact/package.json b/packages/artifact/package.json index 25fbc984..9845862e 100644 --- a/packages/artifact/package.json +++ b/packages/artifact/package.json @@ -10,8 +10,8 @@ ], "homepage": "https://github.com/actions/toolkit/tree/main/packages/artifact", "license": "MIT", - "main": "lib/artifact-client.js", - "types": "lib/artifact-client.d.ts", + "main": "lib/artifact.js", + "types": "lib/artifact.d.ts", "directories": { "lib": "lib", "test": "__tests__" diff --git a/packages/artifact/src/internal/shared/artifact-twirp-client.ts b/packages/artifact/src/internal/shared/artifact-twirp-client.ts index a77d02a4..444e3fac 100644 --- a/packages/artifact/src/internal/shared/artifact-twirp-client.ts +++ b/packages/artifact/src/internal/shared/artifact-twirp-client.ts @@ -1,6 +1,6 @@ import {HttpClient, HttpClientResponse, HttpCodes} from '@actions/http-client' import {BearerCredentialHandler} from '@actions/http-client/lib/auth' -import {info} from '@actions/core' +import {info, debug} from '@actions/core' import {ArtifactServiceClientJSON} from '../../generated' import {getResultsServiceUrl, getRuntimeToken} from './config' @@ -53,6 +53,7 @@ class ArtifactHttpClient implements Rpc { data: object | Uint8Array ): Promise { const url = `${this.baseUrl}/twirp/${service}/${method}` + debug(`Requesting ${url}`) const headers = { 'Content-Type': contentType }