123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- const core = require('@actions/core');
- const S3 = require('aws-sdk/clients/s3');
- const fs = require('fs');
- const path = require('path');
- const shortid = require('shortid');
- const klawSync = require('klaw-sync');
- const slash = require('slash');
- const { lookup } = require('mime-types');
- const AWS_KEY_ID = core.getInput('aws_key_id', {
- required: true
- });
- const SECRET_ACCESS_KEY = core.getInput('aws_secret_access_key', {
- required: true
- });
- const BUCKET = core.getInput('aws_bucket', {
- required: true
- });
- const SOURCE_DIR = core.getInput('source_dir', {
- required: true
- });
- const DESTINATION_DIR = core.getInput('destination_dir', {
- required: false
- });
- const s3 = new S3({
- accessKeyId: AWS_KEY_ID,
- secretAccessKey: SECRET_ACCESS_KEY
- });
- const destinationDir = DESTINATION_DIR === '/' ? shortid() : DESTINATION_DIR;
- const paths = klawSync(SOURCE_DIR, {
- nodir: true
- });
- function upload(params) {
- return new Promise(resolve => {
- s3.upload(params, (err, data) => {
- if (err) core.error(err);
- core.info(`uploaded - ${data.Key}`);
- core.info(`located - ${data.Location}`);
- resolve(data.Location);
- });
- });
- }
- function run() {
- const sourceDir = slash(path.join(process.cwd(), SOURCE_DIR));
- return Promise.all(
- paths.map(p => {
- const fileStream = fs.createReadStream(p.path);
- const bucketPath = slash(path.join(destinationDir, slash(path.relative(sourceDir, p.path))));
- const params = {
- Bucket: BUCKET,
- ACL: 'public-read',
- Body: fileStream,
- Key: bucketPath,
- ContentType: lookup(p.path) || 'text/plain'
- };
- return upload(params);
- })
- );
- }
- run()
- .then(locations => {
- core.info(`object key - ${destinationDir}`);
- core.info(`object locations - ${locations}`);
- core.setOutput('object_key', destinationDir);
- core.setOutput('object_locations', locations);
- })
- .catch(err => {
- core.error(err);
- core.setFailed(err.message);
- });
|