@sun-asterisk/sunlint
Version:
☀️ SunLint - Multi-language static analysis tool for code quality and security | Sun* Engineering Standards
108 lines (94 loc) • 2.86 kB
JavaScript
/**
* Artifact Upload Service
* Upload artifacts to GitHub Actions using @actions/artifact package
*/
const fs = require('fs');
const path = require('path');
/**
* Upload file as GitHub Actions artifact
* @param {string} filePath - Path to file to upload
* @param {Object} options - Upload options
* @param {string} options.artifactName - Name of artifact
* @param {number} options.retentionDays - Retention days (default: 30)
* @returns {Promise<Object>} Upload result
*/
async function uploadArtifact(filePath, options = {}) {
// Check if running in GitHub Actions
if (process.env.GITHUB_ACTIONS !== 'true') {
return {
success: false,
error: 'Not running in GitHub Actions environment'
};
}
// Validate file exists
if (!fs.existsSync(filePath)) {
return {
success: false,
error: `File not found: ${filePath}`
};
}
try {
// Dynamically import @actions/artifact
// Using dynamic import to avoid dependency issues when not in GitHub Actions
const { DefaultArtifactClient } = await import('@actions/artifact');
const artifact = new DefaultArtifactClient();
const artifactName = options.artifactName || path.basename(filePath);
const retentionDays = options.retentionDays || 30;
// Upload artifact
const uploadResult = await artifact.uploadArtifact(
artifactName,
[filePath],
path.dirname(filePath),
{
retentionDays: retentionDays
}
);
if (uploadResult.failedItems && uploadResult.failedItems.length > 0) {
return {
success: false,
error: `Failed to upload ${uploadResult.failedItems.length} item(s)`,
details: uploadResult
};
}
return {
success: true,
artifactName: artifactName,
size: uploadResult.size || fs.statSync(filePath).size,
id: uploadResult.id,
url: `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`
};
} catch (error) {
// If @actions/artifact is not available, provide helpful message
if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') {
return {
success: false,
error: '@actions/artifact package not found. Install with: npm install @actions/artifact',
fallback: 'Use actions/upload-artifact@v4 in workflow instead'
};
}
return {
success: false,
error: error.message,
stack: error.stack
};
}
}
/**
* Check if artifact upload is available
* @returns {Promise<boolean>}
*/
async function isArtifactUploadAvailable() {
if (process.env.GITHUB_ACTIONS !== 'true') {
return false;
}
try {
await import('@actions/artifact');
return true;
} catch (error) {
return false;
}
}
module.exports = {
uploadArtifact,
isArtifactUploadAvailable
};