mirror of
				https://gitea.com/actions/upload-artifact.git
				synced 2025-11-04 05:39:12 +08:00 
			
		
		
		
	Add new option to specify behavior if no files found
This commit is contained in:
		
							
								
								
									
										11
									
								
								action.yml
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								action.yml
									
									
									
									
									
								
							@@ -4,10 +4,19 @@ author: 'GitHub'
 | 
				
			|||||||
inputs: 
 | 
					inputs: 
 | 
				
			||||||
  name:
 | 
					  name:
 | 
				
			||||||
    description: 'Artifact name'
 | 
					    description: 'Artifact name'
 | 
				
			||||||
    required: false
 | 
					    default: 'artifact'
 | 
				
			||||||
  path:
 | 
					  path:
 | 
				
			||||||
    description: 'A file, directory or wildcard pattern that describes what to upload'
 | 
					    description: 'A file, directory or wildcard pattern that describes what to upload'
 | 
				
			||||||
    required: true
 | 
					    required: true
 | 
				
			||||||
 | 
					  if-no-files-found:
 | 
				
			||||||
 | 
					    description: >
 | 
				
			||||||
 | 
					      The desired behavior if no files are found using the provided path.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      Available Options:
 | 
				
			||||||
 | 
					        warn: Output a warning but do not fail the action
 | 
				
			||||||
 | 
					        error: Fail the action with an error message
 | 
				
			||||||
 | 
					        suppress: Do not output any warnings or errors, the action does not fail
 | 
				
			||||||
 | 
					    default: 'warn'
 | 
				
			||||||
runs:
 | 
					runs:
 | 
				
			||||||
  using: 'node12'
 | 
					  using: 'node12'
 | 
				
			||||||
  main: 'dist/index.js'
 | 
					  main: 'dist/index.js'
 | 
				
			||||||
@@ -1,8 +1,22 @@
 | 
				
			|||||||
export enum Inputs {
 | 
					export enum Inputs {
 | 
				
			||||||
  Name = 'name',
 | 
					  Name = 'name',
 | 
				
			||||||
  Path = 'path'
 | 
					  Path = 'path',
 | 
				
			||||||
 | 
					  IfNoFilesFound = 'if-no-files-found'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function getDefaultArtifactName(): string {
 | 
					export enum NoFileOptions {
 | 
				
			||||||
  return 'artifact'
 | 
					  /**
 | 
				
			||||||
 | 
					   * Default. Output a warning but do not fail the action
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  warn,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Fail the action with an error message
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  error,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Do not output any warnings or errors, the action does not fail
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  suppress
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										20
									
								
								src/input-helper.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/input-helper.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					import * as core from '@actions/core'
 | 
				
			||||||
 | 
					import {Inputs, NoFileOptions} from './constants'
 | 
				
			||||||
 | 
					import {UploadInputs} from './upload-inputs'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Helper to get all the inputs for the action
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export function getInputs(): UploadInputs {
 | 
				
			||||||
 | 
					  const name = core.getInput(Inputs.Name)
 | 
				
			||||||
 | 
					  const path = core.getInput(Inputs.Path, {required: true})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const ifNoFilesFound = core.getInput(Inputs.IfNoFilesFound)
 | 
				
			||||||
 | 
					  const noFileBehavior: NoFileOptions = NoFileOptions[ifNoFilesFound]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return {
 | 
				
			||||||
 | 
					    artifactName: name,
 | 
				
			||||||
 | 
					    searchPath: path,
 | 
				
			||||||
 | 
					    ifNoFilesFound: noFileBehavior
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,21 +1,38 @@
 | 
				
			|||||||
import * as core from '@actions/core'
 | 
					import * as core from '@actions/core'
 | 
				
			||||||
import {create, UploadOptions} from '@actions/artifact'
 | 
					import {create, UploadOptions} from '@actions/artifact'
 | 
				
			||||||
import {Inputs, getDefaultArtifactName} from './constants'
 | 
					 | 
				
			||||||
import {findFilesToUpload} from './search'
 | 
					import {findFilesToUpload} from './search'
 | 
				
			||||||
 | 
					import {getInputs} from './input-helper'
 | 
				
			||||||
 | 
					import {NoFileOptions} from './constants'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function run(): Promise<void> {
 | 
					async function run(): Promise<void> {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    const name = core.getInput(Inputs.Name, {required: false})
 | 
					    const inputs = getInputs()
 | 
				
			||||||
    const path = core.getInput(Inputs.Path, {required: true})
 | 
					    const searchResult = await findFilesToUpload(inputs.searchPath)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    const searchResult = await findFilesToUpload(path)
 | 
					 | 
				
			||||||
    if (searchResult.filesToUpload.length === 0) {
 | 
					    if (searchResult.filesToUpload.length === 0) {
 | 
				
			||||||
      core.warning(
 | 
					      // No files were found, different use cases warrant different types of behavior if nothing is found
 | 
				
			||||||
        `No files were found for the provided path: ${path}. No artifacts will be uploaded.`
 | 
					      switch (inputs.ifNoFilesFound) {
 | 
				
			||||||
      )
 | 
					        case NoFileOptions.warn: {
 | 
				
			||||||
 | 
					          core.warning(
 | 
				
			||||||
 | 
					            `No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
 | 
				
			||||||
 | 
					          )
 | 
				
			||||||
 | 
					          break
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        case NoFileOptions.error: {
 | 
				
			||||||
 | 
					          core.setFailed(
 | 
				
			||||||
 | 
					            `No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
 | 
				
			||||||
 | 
					          )
 | 
				
			||||||
 | 
					          break
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        case NoFileOptions.suppress: {
 | 
				
			||||||
 | 
					          core.info(
 | 
				
			||||||
 | 
					            `No files were found with the provided path: ${inputs.searchPath}. No artifacts will be uploaded.`
 | 
				
			||||||
 | 
					          )
 | 
				
			||||||
 | 
					          break
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      core.info(
 | 
					      core.info(
 | 
				
			||||||
        `With the provided path, there will be ${searchResult.filesToUpload.length} files uploaded`
 | 
					        `With the provided path, there will be ${searchResult.filesToUpload.length} file(s) uploaded`
 | 
				
			||||||
      )
 | 
					      )
 | 
				
			||||||
      core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
 | 
					      core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -24,7 +41,7 @@ async function run(): Promise<void> {
 | 
				
			|||||||
        continueOnError: false
 | 
					        continueOnError: false
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      const uploadResponse = await artifactClient.uploadArtifact(
 | 
					      const uploadResponse = await artifactClient.uploadArtifact(
 | 
				
			||||||
        name || getDefaultArtifactName(),
 | 
					        inputs.artifactName,
 | 
				
			||||||
        searchResult.filesToUpload,
 | 
					        searchResult.filesToUpload,
 | 
				
			||||||
        searchResult.rootDirectory,
 | 
					        searchResult.rootDirectory,
 | 
				
			||||||
        options
 | 
					        options
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										18
									
								
								src/upload-inputs.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/upload-inputs.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					import {NoFileOptions} from './constants'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface UploadInputs {
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The name of the artifact that will be uploaded
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  artifactName: string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The search path used to describe what to upload as part of the artifact
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  searchPath: string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The desired behavior if no files are found with the provided search path
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  ifNoFilesFound: NoFileOptions
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user