azput.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* eslint-disable camelcase */
  2. const { BlobServiceClient } = require('@azure/storage-blob');
  3. const path = require('node:path');
  4. // TODO(vertedinde): This variable is a test variable in GHA, sending test
  5. // artifacts to a test account. Change to the real electron artifacts
  6. // storage account when ready.
  7. const { ELECTRON_ARTIFACTS_BLOB_STORAGE } = process.env;
  8. if (!ELECTRON_ARTIFACTS_BLOB_STORAGE) {
  9. console.error('Missing required ELECTRON_ARTIFACTS_BLOB_STORAGE environment variable.');
  10. process.exit(1);
  11. }
  12. const blobServiceClient = BlobServiceClient.fromConnectionString(ELECTRON_ARTIFACTS_BLOB_STORAGE);
  13. const args = require('minimist')(process.argv.slice(2));
  14. let { prefix = '/', key_prefix = '', _: files } = args;
  15. if (prefix && !prefix.endsWith(path.sep)) prefix = path.resolve(prefix) + path.sep;
  16. function filenameToKey (file) {
  17. file = path.resolve(file);
  18. if (file.startsWith(prefix)) file = file.substr(prefix.length - 1);
  19. return key_prefix + (path.sep === '\\' ? file.replace(/\\/g, '/') : file);
  20. }
  21. let anErrorOccurred = false;
  22. function next (done) {
  23. const file = files.shift();
  24. if (!file) return done();
  25. const key = filenameToKey(file);
  26. const [containerName, ...keyPath] = key.split('/');
  27. const blobKey = keyPath.join('/');
  28. console.log(`Uploading '${file}' to container '${containerName}' with key '${blobKey}'...`);
  29. const containerClient = blobServiceClient.getContainerClient(containerName);
  30. const blockBlobClient = containerClient.getBlockBlobClient(blobKey);
  31. blockBlobClient.uploadFile(file)
  32. .then((uploadBlobResponse) => {
  33. console.log(`Upload block blob ${blobKey} successfully: https://artifacts.electronjs.org/${key}`, uploadBlobResponse.requestId);
  34. })
  35. .catch((err) => {
  36. console.error(err);
  37. anErrorOccurred = true;
  38. })
  39. .then(() => next(done));
  40. }
  41. next(() => {
  42. process.exit(anErrorOccurred ? 1 : 0);
  43. });