1
0
Fork 0
toolkit/.github/workflows/artifact-tests.yml

167 lines
4.9 KiB
YAML
Raw Permalink Normal View History

name: artifact-unit-tests
on:
push:
branches:
- main
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
jobs:
2023-12-13 17:19:14 +00:00
upload:
name: Upload
strategy:
matrix:
runs-on: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false
runs-on: ${{ matrix.runs-on }}
steps:
- name: Checkout
2023-12-05 18:54:14 +00:00
uses: actions/checkout@v4
2023-08-28 14:40:06 +00:00
- name: Set Node.js 20.x
2023-12-05 18:54:14 +00:00
uses: actions/setup-node@v4
with:
2023-08-28 14:40:06 +00:00
node-version: 20.x
# 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
run: npm ci
- name: Compile artifact package
run: |
npm ci
npm run tsc
working-directory: packages/artifact
- name: Create files that will be uploaded
run: |
mkdir artifact-path
2023-12-13 17:19:14 +00:00
echo -n 'hello from file 1' > artifact-path/first.txt
echo -n 'hello from file 2' > artifact-path/second.txt
2023-12-13 17:19:14 +00:00
- name: Upload Artifacts
2023-12-05 18:54:14 +00:00
uses: actions/github-script@v7
with:
script: |
const {default: artifact} = require('./packages/artifact/lib/artifact')
const artifactName = 'my-artifact-${{ matrix.runs-on }}'
console.log('artifactName: ' + artifactName)
const fileContents = ['artifact-path/first.txt','artifact-path/second.txt']
const uploadResult = await artifact.uploadArtifact(artifactName, fileContents, './')
console.log(uploadResult)
const size = uploadResult.size
const id = uploadResult.id
2023-12-05 18:51:51 +00:00
console.log(`Successfully uploaded artifact ${id}`)
2023-12-13 17:19:14 +00:00
try {
await artifact.uploadArtifact(artifactName, fileContents, './')
throw new Error('should have failed second upload')
} catch (err) {
console.log('Successfully blocked second artifact upload')
}
verify:
2023-12-13 17:19:14 +00:00
name: Verify
runs-on: ubuntu-latest
2023-12-13 17:19:14 +00:00
needs: [upload]
steps:
- name: Checkout
2023-12-05 18:54:14 +00:00
uses: actions/checkout@v4
2023-08-28 14:40:06 +00:00
- name: Set Node.js 20.x
2023-12-05 18:54:14 +00:00
uses: actions/setup-node@v4
with:
2023-08-28 14:40:06 +00:00
node-version: 20.x
# 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
run: npm ci
- name: Compile artifact package
run: |
npm ci
npm run tsc
working-directory: packages/artifact
2023-12-13 17:19:14 +00:00
- name: List and Download Artifacts
2023-12-05 18:54:14 +00:00
uses: actions/github-script@v7
with:
script: |
2023-12-13 17:19:14 +00:00
const {default: artifactClient} = require('./packages/artifact/lib/artifact')
const {readFile} = require('fs/promises')
const path = require('path')
2023-12-13 17:19:14 +00:00
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
}
2023-12-13 17:19:14 +00:00
const listResult = await artifactClient.listArtifacts({latest: true, findBy})
console.log(listResult)
const artifacts = listResult.artifacts
2023-12-13 17:19:14 +00:00
const expected = [
'my-artifact-ubuntu-latest',
'my-artifact-windows-latest',
'my-artifact-macos-latest'
]
const foundArtifacts = artifacts.filter(artifact =>
expected.includes(artifact.name)
)
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')
2023-12-13 17:19:14 +00:00
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}'`
)
}
}
}