Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a9034d692 | ||
|
|
eff198be5b | ||
|
|
16b786a545 | ||
|
|
18ce228b82 |
@@ -1,5 +1,3 @@
|
|||||||
# Temporarily disabled while v2.0.0 of @actions/artifact is under development
|
|
||||||
|
|
||||||
name: artifact-unit-tests
|
name: artifact-unit-tests
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -12,8 +10,8 @@ on:
|
|||||||
- '**.md'
|
- '**.md'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
upload:
|
||||||
name: Build
|
name: Upload
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
@@ -42,19 +40,13 @@ jobs:
|
|||||||
npm run tsc
|
npm run tsc
|
||||||
working-directory: packages/artifact
|
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
|
- name: Create files that will be uploaded
|
||||||
run: |
|
run: |
|
||||||
mkdir artifact-path
|
mkdir artifact-path
|
||||||
echo '${{ env.file1 }}' > artifact-path/first.txt
|
echo -n 'hello from file 1' > artifact-path/first.txt
|
||||||
echo '${{ env.file2 }}' > artifact-path/second.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
|
uses: actions/github-script@v7
|
||||||
with:
|
with:
|
||||||
script: |
|
script: |
|
||||||
@@ -73,9 +65,16 @@ jobs:
|
|||||||
|
|
||||||
console.log(`Successfully uploaded artifact ${id}`)
|
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:
|
verify:
|
||||||
|
name: Verify
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [build]
|
needs: [upload]
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -96,35 +95,72 @@ jobs:
|
|||||||
npm run tsc
|
npm run tsc
|
||||||
working-directory: packages/artifact
|
working-directory: packages/artifact
|
||||||
|
|
||||||
- name: List artifacts using actions/github-script@v7
|
- name: List and Download Artifacts
|
||||||
uses: actions/github-script@v7
|
uses: actions/github-script@v7
|
||||||
with:
|
with:
|
||||||
script: |
|
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 {readFile} = require('fs/promises')
|
||||||
const repository = process.env.GITHUB_REPOSITORY
|
const path = require('path')
|
||||||
const repositoryOwner = repository.split('/')[0]
|
|
||||||
const repositoryName = repository.split('/')[1]
|
|
||||||
|
|
||||||
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)
|
console.log(listResult)
|
||||||
|
|
||||||
const artifacts = listResult.artifacts
|
const artifacts = listResult.artifacts
|
||||||
|
const expected = [
|
||||||
|
'my-artifact-ubuntu-latest',
|
||||||
|
'my-artifact-windows-latest',
|
||||||
|
'my-artifact-macos-latest'
|
||||||
|
]
|
||||||
|
|
||||||
if (artifacts.length !== 3) {
|
const foundArtifacts = artifacts.filter(artifact =>
|
||||||
throw new Error('Expected 3 artifacts but only found ' + artifacts.length + ' artifacts')
|
expected.includes(artifact.name)
|
||||||
}
|
)
|
||||||
|
|
||||||
const artifactNames = artifacts.map(artifact => artifact.name)
|
if (foundArtifacts.length !== 3) {
|
||||||
if (!artifactNames.includes('my-artifact-ubuntu-latest')){
|
console.log('Unexpected length of found artifacts', foundArtifacts)
|
||||||
throw new Error("Expected artifact list to contain an artifact named my-artifact-ubuntu-latest but it's missing")
|
throw new Error(
|
||||||
}
|
`Expected 3 artifacts but found ${foundArtifacts.length} artifacts.`
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Successfully listed artifacts that were uploaded')
|
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}'`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {HttpClient} from '@actions/http-client'
|
|||||||
import * as config from '../src/internal/shared/config'
|
import * as config from '../src/internal/shared/config'
|
||||||
import {internalArtifactTwirpClient} from '../src/internal/shared/artifact-twirp-client'
|
import {internalArtifactTwirpClient} from '../src/internal/shared/artifact-twirp-client'
|
||||||
import {noopLogs} from './common'
|
import {noopLogs} from './common'
|
||||||
|
import {NetworkError, UsageError} from '../src/internal/shared/errors'
|
||||||
|
|
||||||
jest.mock('@actions/http-client')
|
jest.mock('@actions/http-client')
|
||||||
|
|
||||||
@@ -257,9 +258,42 @@ describe('artifact-http-client', () => {
|
|||||||
name: 'artifact',
|
name: 'artifact',
|
||||||
version: 4
|
version: 4
|
||||||
})
|
})
|
||||||
}).rejects.toThrowError(
|
}).rejects.toThrowError(new NetworkError('ENOTFOUND').message)
|
||||||
'Failed to CreateArtifact: Unable to make request: ENOTFOUND\nIf you are using self-hosted runners, please make sure your runner has access to all GitHub endpoints: https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners#communication-between-self-hosted-runners-and-github'
|
expect(mockHttpClient).toHaveBeenCalledTimes(1)
|
||||||
)
|
expect(mockPost).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should properly describe a usage error', async () => {
|
||||||
|
const mockPost = jest.fn(() => {
|
||||||
|
const msgFailed = new http.IncomingMessage(new net.Socket())
|
||||||
|
msgFailed.statusCode = 403
|
||||||
|
msgFailed.statusMessage = 'Forbidden'
|
||||||
|
return {
|
||||||
|
message: msgFailed,
|
||||||
|
readBody: async () => {
|
||||||
|
return Promise.resolve(
|
||||||
|
`{"msg": "insufficient usage to create artifact"}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const mockHttpClient = (
|
||||||
|
HttpClient as unknown as jest.Mock
|
||||||
|
).mockImplementation(() => {
|
||||||
|
return {
|
||||||
|
post: mockPost
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const client = internalArtifactTwirpClient()
|
||||||
|
await expect(async () => {
|
||||||
|
await client.CreateArtifact({
|
||||||
|
workflowRunBackendId: '1234',
|
||||||
|
workflowJobRunBackendId: '5678',
|
||||||
|
name: 'artifact',
|
||||||
|
version: 4
|
||||||
|
})
|
||||||
|
}).rejects.toThrowError(new UsageError().message)
|
||||||
expect(mockHttpClient).toHaveBeenCalledTimes(1)
|
expect(mockHttpClient).toHaveBeenCalledTimes(1)
|
||||||
expect(mockPost).toHaveBeenCalledTimes(1)
|
expect(mockPost).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {info, debug} from '@actions/core'
|
|||||||
import {ArtifactServiceClientJSON} from '../../generated'
|
import {ArtifactServiceClientJSON} from '../../generated'
|
||||||
import {getResultsServiceUrl, getRuntimeToken} from './config'
|
import {getResultsServiceUrl, getRuntimeToken} from './config'
|
||||||
import {getUserAgentString} from './user-agent'
|
import {getUserAgentString} from './user-agent'
|
||||||
import {NetworkError} from './errors'
|
import {NetworkError, UsageError} from './errors'
|
||||||
|
|
||||||
// The twirp http client must implement this interface
|
// The twirp http client must implement this interface
|
||||||
interface Rpc {
|
interface Rpc {
|
||||||
@@ -64,7 +64,7 @@ class ArtifactHttpClient implements Rpc {
|
|||||||
this.httpClient.post(url, JSON.stringify(data), headers)
|
this.httpClient.post(url, JSON.stringify(data), headers)
|
||||||
)
|
)
|
||||||
|
|
||||||
return JSON.parse(body)
|
return body
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Failed to ${method}: ${error.message}`)
|
throw new Error(`Failed to ${method}: ${error.message}`)
|
||||||
}
|
}
|
||||||
@@ -72,34 +72,49 @@ class ArtifactHttpClient implements Rpc {
|
|||||||
|
|
||||||
async retryableRequest(
|
async retryableRequest(
|
||||||
operation: () => Promise<HttpClientResponse>
|
operation: () => Promise<HttpClientResponse>
|
||||||
): Promise<{response: HttpClientResponse; body: string}> {
|
): Promise<{response: HttpClientResponse; body: object}> {
|
||||||
let attempt = 0
|
let attempt = 0
|
||||||
let errorMessage = ''
|
let errorMessage = ''
|
||||||
|
let rawBody = ''
|
||||||
while (attempt < this.maxAttempts) {
|
while (attempt < this.maxAttempts) {
|
||||||
let isRetryable = false
|
let isRetryable = false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await operation()
|
const response = await operation()
|
||||||
const statusCode = response.message.statusCode
|
const statusCode = response.message.statusCode
|
||||||
const body = await response.readBody()
|
rawBody = await response.readBody()
|
||||||
debug(`[Response] - ${response.message.statusCode}`)
|
debug(`[Response] - ${response.message.statusCode}`)
|
||||||
debug(`Headers: ${JSON.stringify(response.message.headers, null, 2)}`)
|
debug(`Headers: ${JSON.stringify(response.message.headers, null, 2)}`)
|
||||||
debug(`Body: ${body}`)
|
const body = JSON.parse(rawBody)
|
||||||
|
debug(`Body: ${JSON.stringify(body, null, 2)}`)
|
||||||
if (this.isSuccessStatusCode(statusCode)) {
|
if (this.isSuccessStatusCode(statusCode)) {
|
||||||
return {response, body}
|
return {response, body}
|
||||||
}
|
}
|
||||||
isRetryable = this.isRetryableHttpStatusCode(statusCode)
|
isRetryable = this.isRetryableHttpStatusCode(statusCode)
|
||||||
errorMessage = `Failed request: (${statusCode}) ${response.message.statusMessage}`
|
errorMessage = `Failed request: (${statusCode}) ${response.message.statusMessage}`
|
||||||
const responseMessage = JSON.parse(body).msg
|
if (body.msg) {
|
||||||
if (responseMessage) {
|
if (UsageError.isUsageErrorMessage(body.msg)) {
|
||||||
errorMessage = `${errorMessage}: ${responseMessage}`
|
throw new UsageError()
|
||||||
|
}
|
||||||
|
|
||||||
|
errorMessage = `${errorMessage}: ${body.msg}`
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
isRetryable = true
|
if (error instanceof SyntaxError) {
|
||||||
errorMessage = error.message
|
debug(`Raw Body: ${rawBody}`)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error instanceof UsageError) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
if (NetworkError.isNetworkErrorCode(error?.code)) {
|
if (NetworkError.isNetworkErrorCode(error?.code)) {
|
||||||
throw new NetworkError(error?.code)
|
throw new NetworkError(error?.code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isRetryable = true
|
||||||
|
errorMessage = error.message
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isRetryable) {
|
if (!isRetryable) {
|
||||||
|
|||||||
@@ -57,3 +57,16 @@ export class NetworkError extends Error {
|
|||||||
].includes(code)
|
].includes(code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UsageError extends Error {
|
||||||
|
constructor() {
|
||||||
|
const message = `Artifact storage quota has been hit. Unable to upload any new artifacts. Usage is recalculated every 6-12 hours.\nMore info on storage limits: https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#calculating-minute-and-storage-spending`
|
||||||
|
super(message)
|
||||||
|
this.name = 'UsageError'
|
||||||
|
}
|
||||||
|
|
||||||
|
static isUsageErrorMessage = (msg?: string): boolean => {
|
||||||
|
if (!msg) return false
|
||||||
|
return msg.includes('insufficient usage')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user