From 6a9034d6925f26109ff8883900ab3512366b624f Mon Sep 17 00:00:00 2001 From: Rob Herley Date: Wed, 13 Dec 2023 12:19:14 -0500 Subject: [PATCH] update artifact workflow tests --- .github/workflows/artifact-tests.yml | 102 ++++++++++++++++++--------- 1 file changed, 69 insertions(+), 33 deletions(-) diff --git a/.github/workflows/artifact-tests.yml b/.github/workflows/artifact-tests.yml index 4c7c6ab6..aa0ab371 100644 --- a/.github/workflows/artifact-tests.yml +++ b/.github/workflows/artifact-tests.yml @@ -1,5 +1,3 @@ -# Temporarily disabled while v2.0.0 of @actions/artifact is under development - name: artifact-unit-tests on: push: @@ -12,8 +10,8 @@ on: - '**.md' jobs: - build: - name: Build + upload: + name: Upload strategy: matrix: @@ -42,19 +40,13 @@ jobs: npm run tsc working-directory: packages/artifact - - name: Set artifact file contents - shell: bash - run: | - 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.file1 }}' > artifact-path/first.txt - echo '${{ env.file2 }}' > artifact-path/second.txt + echo -n 'hello from file 1' > artifact-path/first.txt + echo -n 'hello from file 2' > artifact-path/second.txt - - name: Upload Artifacts using actions/github-script@v7 + - name: Upload Artifacts uses: actions/github-script@v7 with: script: | @@ -73,9 +65,16 @@ jobs: console.log(`Successfully uploaded artifact ${id}`) + try { + await artifact.uploadArtifact(artifactName, fileContents, './') + throw new Error('should have failed second upload') + } catch (err) { + console.log('Successfully blocked second artifact upload') + } verify: + name: Verify runs-on: ubuntu-latest - needs: [build] + needs: [upload] steps: - name: Checkout uses: actions/checkout@v4 @@ -96,35 +95,72 @@ jobs: npm run tsc working-directory: packages/artifact - - name: List artifacts using actions/github-script@v7 + - name: List and Download Artifacts uses: actions/github-script@v7 with: script: | - const {default: artifact} = require('./packages/artifact/lib/artifact') + const {default: artifactClient} = require('./packages/artifact/lib/artifact') - const workflowRunId = process.env.GITHUB_RUN_ID - const repository = process.env.GITHUB_REPOSITORY - const repositoryOwner = repository.split('/')[0] - const repositoryName = repository.split('/')[1] + const {readFile} = require('fs/promises') + const path = require('path') - const listResult = await artifact.listArtifacts(workflowRunId, repositoryOwner, repositoryName, '${{ secrets.GITHUB_TOKEN }}') + const findBy = { + repositoryOwner: process.env.GITHUB_REPOSITORY.split('/')[0], + repositoryName: process.env.GITHUB_REPOSITORY.split('/')[1], + token: '${{ secrets.GITHUB_TOKEN }}', + workflowRunId: process.env.GITHUB_RUN_ID + } + + const listResult = await artifactClient.listArtifacts({latest: true, findBy}) console.log(listResult) const artifacts = listResult.artifacts + const expected = [ + 'my-artifact-ubuntu-latest', + 'my-artifact-windows-latest', + 'my-artifact-macos-latest' + ] - if (artifacts.length !== 3) { - throw new Error('Expected 3 artifacts but only found ' + artifacts.length + ' artifacts') - } + const foundArtifacts = artifacts.filter(artifact => + expected.includes(artifact.name) + ) - const artifactNames = artifacts.map(artifact => artifact.name) - if (!artifactNames.includes('my-artifact-ubuntu-latest')){ - throw new Error("Expected artifact list to contain an artifact named my-artifact-ubuntu-latest but it's missing") - } - if (!artifactNames.includes('my-artifact-windows-latest')){ - throw new Error("Expected artifact list to contain an artifact named my-artifact-windows-latest but it's missing") - } - if (!artifactNames.includes('my-artifact-macos-latest')){ - throw new Error("Expected artifact list to contain an artifact named my-artifact-macos-latest but it's missing") + if (foundArtifacts.length !== 3) { + console.log('Unexpected length of found artifacts', foundArtifacts) + throw new Error( + `Expected 3 artifacts but found ${foundArtifacts.length} artifacts.` + ) } console.log('Successfully listed artifacts that were uploaded') + + const files = [ + {name: 'artifact-path/first.txt', content: 'hello from file 1'}, + {name: 'artifact-path/second.txt', content: 'hello from file 2'} + ] + + for (const artifact of foundArtifacts) { + const {downloadPath} = await artifactClient.downloadArtifact(artifact.id, { + path: artifact.name, + findBy + }) + + console.log('Downloaded artifact to:', downloadPath) + + for (const file of files) { + const filepath = path.join( + process.env.GITHUB_WORKSPACE, + downloadPath, + file.name + ) + + console.log('Checking file:', filepath) + + const content = await readFile(filepath, 'utf8') + if (content.trim() !== file.content.trim()) { + throw new Error( + `Expected file '${file.name}' to contain '${file.content}' but found '${content}'` + ) + } + } + }