publish method

  1. @override
Future<int> publish()
override

Executes the GitHub Releases publishing workflow.

Performs the complete GitHub release publishing process including variable processing, release management, and asset uploads. Handles both file and directory uploads with proper error handling.

Publishing workflow:

  1. Process variables and configure authentication
  2. Initialize GitHub API client with token
  3. Find existing release or create new one
  4. Upload files/directory contents as release assets
  5. Handle errors and provide detailed logging

File handling:

  • Single files: Upload directly as release asset
  • Directories: Upload all matching binary files
  • Binary type filtering: Only files matching binaryType

Returns exit code:

  • 0 = Success (all assets uploaded)
  • 1 = Error (upload failed or file issues)

Throws exception for configuration or API errors.

Implementation

@override
Future<int> publish() async {
  final argumentBuilder = Arguments.fromJson(
    await variables.processMap(toJson()),
    variables: variables,
  );
  await argumentBuilder.printJob();

  final arguments = await argumentBuilder.arguments;

  argumentBuilder._dio.options.headers["Authorization"] =
      "token ${await variables.process(token)}";
  logger.logDebug.call(
    "Starting upload with `$publisher ${arguments.join(" ")}`",
  );
  logger.logDebug.call("Initializing Github API client");

  final uploadUrl =
      (await argumentBuilder._getReleaseUploadUrl().catchError((e) => null) ??
          await argumentBuilder._getLatestReleaseUploadUrl().catchError(
                (e) => null,
              ) ??
          await argumentBuilder._createRelease().catchError((e) => null));
  if (uploadUrl == null) {
    logger.logErrorVerbose.call("Failed to get upload URL");
    return 1;
  }

  logger.logInfo(
    "${await FileSystemEntity.isDirectory(filePath) ? "Directory" : "File"} detected on path: $filePath",
  );
  if (await FileSystemEntity.isDirectory(filePath)) {
    logger.logInfo("Path is a directory");
    logger.logInfo("NOTE : All files in the directory will be uploaded");
    for (var file in Directory(filePath).listSync()) {
      if (file is File && file.path.endsWith(binaryType)) {
        final downloadUrl = await argumentBuilder.uploadFile(uploadUrl, file);
        if (downloadUrl == null) continue;
        logger.logDebug.call(
          "${file.path} uploaded successfully: $downloadUrl",
        );
      }
    }
  } else {
    if (!await File(filePath).exists()) {
      logger.logErrorVerbose.call("File does not exist");
      return 1;
    }
    final downloadUrl = await argumentBuilder.uploadFile(
      uploadUrl,
      File(filePath),
    );
    if (downloadUrl == null) return 1;

    logger.logDebug.call("File uploaded successfully: $downloadUrl");
  }
  return 0;
}