How to upload assets to a github release from node.js

Nicolas Joseph

I am trying to automatically post some assets on a Github release I programmatically create.

Here is my upload function:

function uploadFile(fileName, uploadUrl, callback){

  var uploadEndPoint = url.parse(uploadUrl.substring(0,uploadUrl.indexOf('{')));

  options.host = uploadEndPoint.hostname;
  options.path = uploadEndPoint.pathname+'?name=' + fileName;
  options.method = 'POST';
  options.headers['content-type'] = 'application/zip';

  var uploadRequest = https.request(options, callback);

  uploadRequest.on('error', function(e) {
    console.log('release.js - problem with uploadRequest request: ' + e.message);
  });

  var readStream = fs.ReadStream(path.resolve(__dirname,'builds',fileName));
  readStream.pipe(uploadRequest);

  readStream.on('close', function () {
    req.end();
    console.log('release.js - ' + fileName + ' sent to the server');
  });
}

At the end of this I get a 404 not found

I tried auth from token and user/password

I checked the url

I though it might be because of SNI, but I don't know how to check that.

Any clue ? Thanks !

Nicolas Joseph

I found a solution to that problem by NOT using the low level node.js modules and using instead restler which is a library that handles SNI.

Here is how is used it:

  rest = require('restler'),
  path = require('path'),
  fs = require('fs');

  fs.stat(path.resolve(__dirname,'builds',fileName), function(err, stats){
    rest.post(uploadEndPoint.href+'?name=' + fileName, {
      multipart: true,
      username: GITHUB_OAUTH_TOKEN,
      password: '',
      data: rest.file(path.resolve(__dirname,'builds',fileName), null, stats.size, null, 'application/zip')
    }).on('complete', callback);
  });

Hope that will help someone :)

EDIT on 27/02/2015: We recently switched from restler to request.

var 
 request  = require('request'),
 fs = require('fs');

var stats = fs.statSync(filePath);

var options = {
  url: upload_url.replace('{?name}', ''),
  port: 443,
  auth: {
    pass: 'x-oauth-basic',
    user: GITHUB_OAUTH_TOKEN
  },
  json:true,
  headers: {
    'User-Agent': 'Release-Agent',
    'Accept': 'application/vnd.github.v3+json',
    'Content-Type': 'application/zip',
    'Content-Length': stats.size
  },
  qs: {
    name: assetName
  }
};

// Better as a stream
fs.createReadStream(filePath).pipe(request.post(options, function(err, res){
  // Do whatever you will like with the result
}));

The upload_uri can be retrieved through a get request on an existing release or in the response directly after the release creation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Github: Upload release assets with Bash

Azure RM Templates. How to upload assets automatically with VS instead of fetching them from GitHub

Node. Js Upload assets like video images files

How to upload image from "RichTextEditor", using Node.js?

How to upload folders to GCS from node js using client library

Cannot upload files for release in Github

How to release chrome extension from GitHub repository

How to run node js app from github repository?

How to get release count and release download count from Github GraphQL?

How to upload image from assets in flutter to firebase storage?

How to manage assets (css) in MEAN.js & Node.js

Node.js: How to release Mongoose model from memory? (memory leak)

Cannot upload github release asset through API

Upload a github action artifact to LATEST release

How to upload files to the GCloud in Node.js?

Where to upload and how to start a node js app?

How to include assets from node_modules in angular cli project

How do I capture the latest GitHub release of a project from the API?

How to download a JAR from Github Package Registry for a specific Release version

How to publish Travis CI artifacts to GitHub Release from several jobs

How to prevent node.js from hanging when a big file upload is initiated?

How to upload files from url in cloud storage node.js client

How to upload a WAV file to S3 from the /tmp folder in AWS Lambda using Node.js

How to upload artifacts to existing release?

How to import css file from assets folder in nuxt.js

how to access assets/images from the view in Sails.js?

Next.js: How to get static assets from within getStaticProps

How to package assets with a node module

How to upload a project to Github