2020-04-23 18:52:53 +00:00
|
|
|
name: artifact-unit-tests
|
2020-05-14 20:18:21 +00:00
|
|
|
on:
|
|
|
|
push:
|
|
|
|
branches:
|
2020-07-21 15:33:05 +00:00
|
|
|
- main
|
2020-05-14 20:18:21 +00:00
|
|
|
paths-ignore:
|
|
|
|
- '**.md'
|
|
|
|
pull_request:
|
|
|
|
paths-ignore:
|
|
|
|
- '**.md'
|
2020-04-23 18:52:53 +00:00
|
|
|
|
|
|
|
jobs:
|
2023-12-13 17:19:14 +00:00
|
|
|
upload:
|
|
|
|
name: Upload
|
2020-04-23 18:52:53 +00:00
|
|
|
|
|
|
|
strategy:
|
|
|
|
matrix:
|
2020-05-14 20:18:21 +00:00
|
|
|
runs-on: [ubuntu-latest, windows-latest, macos-latest]
|
2020-04-23 18:52:53 +00:00
|
|
|
fail-fast: false
|
|
|
|
|
|
|
|
runs-on: ${{ matrix.runs-on }}
|
|
|
|
|
|
|
|
steps:
|
|
|
|
- name: Checkout
|
2023-12-05 18:54:14 +00:00
|
|
|
uses: actions/checkout@v4
|
2020-04-23 18:52:53 +00:00
|
|
|
|
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
|
2020-04-23 18:52:53 +00:00
|
|
|
with:
|
2023-08-28 14:40:06 +00:00
|
|
|
node-version: 20.x
|
2020-04-23 18:52:53 +00:00
|
|
|
|
|
|
|
# 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
|
2021-12-14 20:50:50 +00:00
|
|
|
|
2020-04-23 18:52:53 +00:00
|
|
|
- name: Create files that will be uploaded
|
|
|
|
run: |
|
2021-12-14 20:50:50 +00:00
|
|
|
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
|
2020-04-23 18:52:53 +00:00
|
|
|
|
2023-12-13 17:19:14 +00:00
|
|
|
- name: Upload Artifacts
|
2023-12-05 18:54:14 +00:00
|
|
|
uses: actions/github-script@v7
|
2023-08-17 16:32:55 +00:00
|
|
|
with:
|
|
|
|
script: |
|
2023-12-06 04:00:07 +00:00
|
|
|
const {default: artifact} = require('./packages/artifact/lib/artifact')
|
2020-04-23 18:52:53 +00:00
|
|
|
|
2023-08-17 18:40:33 +00:00
|
|
|
const artifactName = 'my-artifact-${{ matrix.runs-on }}'
|
|
|
|
console.log('artifactName: ' + artifactName)
|
2020-04-23 18:52:53 +00:00
|
|
|
|
2023-08-17 18:40:33 +00:00
|
|
|
const fileContents = ['artifact-path/first.txt','artifact-path/second.txt']
|
2020-04-23 18:52:53 +00:00
|
|
|
|
2023-12-06 04:00:07 +00:00
|
|
|
const uploadResult = await artifact.uploadArtifact(artifactName, fileContents, './')
|
2023-08-17 18:40:33 +00:00
|
|
|
console.log(uploadResult)
|
2023-08-17 16:32:55 +00:00
|
|
|
|
2023-08-17 18:40:33 +00:00
|
|
|
const size = uploadResult.size
|
|
|
|
const id = uploadResult.id
|
2023-08-17 16:32:55 +00:00
|
|
|
|
2023-12-05 18:51:51 +00:00
|
|
|
console.log(`Successfully uploaded artifact ${id}`)
|
2023-12-06 04:00:07 +00:00
|
|
|
|
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')
|
|
|
|
}
|
2023-08-17 18:40:33 +00:00
|
|
|
verify:
|
2023-12-13 17:19:14 +00:00
|
|
|
name: Verify
|
2023-08-17 18:40:33 +00:00
|
|
|
runs-on: ubuntu-latest
|
2023-12-13 17:19:14 +00:00
|
|
|
needs: [upload]
|
2023-08-17 18:40:33 +00:00
|
|
|
steps:
|
|
|
|
- name: Checkout
|
2023-12-05 18:54:14 +00:00
|
|
|
uses: actions/checkout@v4
|
2023-08-17 18:40:33 +00:00
|
|
|
|
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
|
2023-08-17 18:40:33 +00:00
|
|
|
with:
|
2023-08-28 14:40:06 +00:00
|
|
|
node-version: 20.x
|
2023-08-17 18:40:33 +00:00
|
|
|
|
|
|
|
# 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
|
2023-08-17 18:40:33 +00:00
|
|
|
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-08-17 18:40:33 +00:00
|
|
|
|
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-08-17 18:40:33 +00:00
|
|
|
|
2023-12-13 17:19:14 +00:00
|
|
|
const listResult = await artifactClient.listArtifacts({latest: true, findBy})
|
2023-08-17 18:40:33 +00:00
|
|
|
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.`
|
|
|
|
)
|
2023-08-17 18:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}'`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|