Skip to content
Permalink
Browse files

Upload release assets to S3 in script/publish-release

  • Loading branch information...
daviwil committed Jun 23, 2018
1 parent fc5c7ad commit 45789f81c026f6e1e032dcfb4468a163008be570
Showing with 93 additions and 20 deletions.
  1. +57 −0 script/lib/upload-to-s3.js
  2. +1 −0 script/package.json
  3. +32 −20 script/publish-release
  4. +3 −0 script/vsts/nightly-release.yml
@@ -0,0 +1,57 @@
'use strict'

const fs = require('fs')
const path = require('path')
const aws = require('aws-sdk')

module.exports = function (s3Key, s3Secret, s3Bucket, directory, assets) {
const s3 = new aws.S3({
accessKeyId: s3Key,
secretAccessKey: s3Secret,
params: { Bucket: s3Bucket }
})

function listExistingAssetsForDirectory (directory) {
return s3.listObjectsV2({ Prefix: directory }).promise().then((res) => {
return res.Contents.map((obj) => { return { Key: obj.Key } })
})
}

function deleteExistingAssets (existingAssets) {
if (existingAssets.length > 0) {
return s3.deleteObjects({ Delete: { Objects: existingAssets } }).promise()
} else {
return Promise.resolve(true)
}
}

function uploadAssets (assets, directory) {
return assets.reduce(
function (promise, asset) {
return promise.then(() => uploadAsset(directory, asset))
}, Promise.resolve())
}

function uploadAsset (directory, assetPath) {
return new Promise((resolve, reject) => {
console.info(`Uploading ${assetPath}`)
const params = {
Key: `${directory}${path.basename(assetPath)}`,
ACL: 'public-read',
Body: fs.createReadStream(assetPath)
}

s3.upload(params, error => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}

return listExistingAssetsForDirectory(directory)
.then(deleteExistingAssets)
.then(() => uploadAssets(assets, directory))
}
@@ -4,6 +4,7 @@
"dependencies": {
"7zip-bin": "^4.0.2",
"async": "2.0.1",
"aws-sdk": "^2.5.2",
"babel-core": "5.8.38",
"coffeelint": "1.15.7",
"colors": "1.1.2",
@@ -5,10 +5,9 @@
const path = require('path')
const glob = require('glob')
const publishRelease = require('publish-release')
const uploadToS3 = require('./lib/upload-to-s3')
const CONFIG = require('./config')

console.log(`Publishing GitHub release ${CONFIG.computedAppVersion}`)

const yargs = require('yargs')
const argv = yargs
.usage('Usage: $0 [options]')
@@ -20,21 +19,34 @@ const argv = yargs
let assetsPath = argv.assetsPath || path.join(CONFIG.repositoryRootPath, 'out')
let assets = glob.sync(path.join(assetsPath, '*(*.exe|*.zip|*.nupkg|*.tar.gz|*.rpm|*.deb|RELEASES*)'))

publishRelease({
token: process.env.GITHUB_TOKEN,
owner: 'atom',
repo: CONFIG.channel !== 'nightly' ? 'atom' : 'atom-nightly-releases',
name: CONFIG.computedAppVersion,
tag: CONFIG.computedAppVersion,
draft: false,
prerelease: CONFIG.channel !== 'stable',
reuseRelease: true,
skipIfPublished: true,
assets
}, function (err, release) {
if (err) {
console.log("An error occurred while publishing the release:\n\n", err)
} else {
console.log("Release published successfully: ", release.html_url)
}
})
console.log(`Uploading release assets for ${CONFIG.computedAppVersion} to S3`)

uploadToS3(
process.env.ATOM_RELEASES_S3_KEY,
process.env.ATOM_RELEASES_S3_SECRET,
process.env.ATOM_RELEASES_S3_BUCKET,
`releases/v${CONFIG.computedAppVersion}/`,
assets).then(
() => {
console.log(`Publishing GitHub release ${CONFIG.computedAppVersion}`)
publishRelease({
token: process.env.GITHUB_TOKEN,
owner: 'atom',
repo: CONFIG.channel !== 'nightly' ? 'atom' : 'atom-nightly-releases',
name: CONFIG.computedAppVersion,
tag: `v${CONFIG.computedAppVersion}`,
draft: false,
prerelease: CONFIG.channel !== 'stable',
reuseRelease: true,
skipIfPublished: true,
assets
}, function (err, release) {
if (err) {
console.log("An error occurred while publishing the release:\n\n", err)
} else {
console.log("Release published successfully: ", release.html_url)
}
})
}).catch((err) => {
console.error('An error occurred while uploading the release:', err)
})
@@ -36,4 +36,7 @@ phases:
$(Build.SourcesDirectory)\script\publish-release.cmd --assets-path "$(System.ArtifactsDirectory)/Binaries"
env:
GITHUB_TOKEN: $(GITHUB_TOKEN)
ATOM_RELEASES_S3_KEY: $(ATOM_RELEASES_S3_KEY)
ATOM_RELEASES_S3_SECRET: $(ATOM_RELEASES_S3_SECRET)
ATOM_RELEASES_S3_BUCKET: $(ATOM_RELEASES_S3_BUCKET)
displayName: Create Nightly Release

0 comments on commit 45789f8

Please sign in to comment.
You can’t perform that action at this time.