Skip to content

Instantly share code, notes, and snippets.

@AgDude
Created November 25, 2016 16:49
Show Gist options
  • Save AgDude/b64c33089c84b257100208eae1067902 to your computer and use it in GitHub Desktop.
Save AgDude/b64c33089c84b257100208eae1067902 to your computer and use it in GitHub Desktop.
Gulp task to upload and deploy to ionic with metadata.
gulp.task('deploy', function () {
runSequence(
'ionic-password',
// 'bump-build',
// ['clean'],
// ['jshint', 'sass', 'config', 'templates'],
// 'bundle',
// 'index',
// 'clean-index',
// 'copy',
'ionic-upload',
'ionic-deploy'
);
});
gulp.task('ionic-upload', ['ionic-password'], function () {
delete require.cache['../package.json'];
var package = require('../package.json');
execSync(`ionic upload -e "${ env_private.ionic_email }" -p ${argv.password} --note ${package.version}`, {stdio: [0,1,2]});
return gulp.src('./', {read: false});
});
gulp.task('ionic-password', function () {
if ( !argv.password ){
return gulp.src('./', {read: false})
.pipe(prompt.prompt({
type: 'password',
message: `Enter ionic cloud password for ${ env_private.ionic_email }`,
name: 'password'
}, function (result) {
argv.password = result.password;
}));
}
return gulp.src('./', {read: false});
});
gulp.task('ionic-deploy', function (done) {
var package = require('../package.json');
var token = require('../private.json').ionic_token;
var channel, channelTag = argv.channel || 'dev';
var snapshot;
var ionicClient = axios.create();
ionicClient.defaults.headers.common.Authorization = `Bearer ${token}`;
ionicClient.defaults.headers.post['Content-Type'] = 'application/json';
ionicClient.defaults.headers.patch['Content-Type'] = 'application/json';
Error.stackTraceLimit = 100;
ionicClient.get(`https://api.ionic.io/deploy/channels/${channelTag}`)
.then(response => {
channel = response.data.data.uuid;
return ionicClient.get('https://api.ionic.io/deploy/snapshots', {
params: {
page_size: 5000
}
});
})
.then(response => {
if ( !response.data.data.length ){ return Promise.reject('No snapshots found'); }
snapshot = _.sortBy(response.data.data, 'created').reverse().shift();
console.log(`Fetching snapshot ${snapshot}`);
return ionicClient.get(`https://api.ionic.io/deploy/snapshots/${snapshot.uuid}`);
})
.then(response => {
var snapshotMeta = response.data.data;
if ( snapshotMeta.note !== package.version ){
console.log(`Got version ${snapshotMeta.note}, expecting version ${package.version}`)
return Promise.reject('Aborted. Retreived wrong snapshot.');
}
var meta_data = _.extend(snapshotMeta.user_metadata, {
version: package.version,
created: snapshot.created
});
return ionicClient.patch(`https://api.ionic.io/deploy/snapshots/${snapshotMeta.uuid}`, {
user_metadata: meta_data
});
})
.then(response => {
console.log(`Deploying ${response.data.data.uuid} to channel ${channelTag}`);
return ionicClient.post('https://api.ionic.io/deploy/deploys', {
channel: channel,
snapshot: response.data.data.uuid
});
})
.then(() => {
console.log('Snapshot meta data sucessfully updated');
done();
})
.catch(err => {
console.error(`Failed to set snapshot meta data. ${err}.`);
if ( err.response ){
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
console.log(err.config.url);
}
done();
});
})
@mssingh018
Copy link

private.json i have to create it for access ionic_token?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment