Skip to content

Commit

Permalink
Merge pull request #6 from felipecsl/master
Browse files Browse the repository at this point in the history
Add working-directory option
  • Loading branch information
miloserdow authored Apr 19, 2020
2 parents f5aefae + f353711 commit a1ed9ca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Environment where deploy is to be performed to. E.g. "production", "staging". De
### `enc_rsa_key_pth`
**Required** Path to the encrypted key. Default `"config/deploy_id_rsa_enc"`.

### `working-directory`
The directory from which to run the deploy commands, including `bundle install`.

## Outputs
No outputs

Expand Down
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'Capistrano deploy'
description: 'Deploy an application using capistrano to the given target'
branding:
icon: 'arrow-right-circle'
icon: 'arrow-right-circle'
color: 'black'
inputs:
target: # string
Expand All @@ -14,6 +14,11 @@ inputs:
description: 'Path to SSH private key encrypted with deploy_key'
required: true
default: 'config/deploy_id_rsa_enc'
runs:
working-directory:
description: 'The directory from which to run the deploy command'
required: false
default: ''

runs:
using: 'node12'
main: 'lib/run.js'
48 changes: 27 additions & 21 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,68 @@ const core = require("@actions/core");
const io = require("@actions/io");
const toolrunner = require("@actions/exec/lib/toolrunner");

function install_deps() {
function install_deps(options) {
return __awaiter(this, void 0, void 0, function* () {
let runner = null;
runner = new toolrunner.ToolRunner('gem', ['install', 'bundler:1.17.3', '--no-document']);
runner = new toolrunner.ToolRunner('gem', ['install', 'bundler:1.17.3', '--no-document'], options);
yield runner.exec();

let runner2 = null;
runner2 = new toolrunner.ToolRunner('bundle', ['install', '--deployment', '--jobs=4', '--without=production test']);
runner2 = new toolrunner.ToolRunner(
'bundle',
['install', '--deployment', '--jobs=4', '--without=production test'],
options
);
yield runner2.exec();
});
}

function decrypt_key(deploy_key, enc_rsa_key_pth) {
function decrypt_key(deploy_key, enc_rsa_key_pth, options) {
return __awaiter(this, void 0, void 0, function* () {
// Create directory if not exists
yield io.mkdirP('config');
// Create directory if it doesn't yet exists
const configDir = options.cwd ? `${options.cwd}/config` : 'config';
yield io.mkdirP(configDir);

let runner0 = new toolrunner.ToolRunner('openssl', ['version']);
const runner0 = new toolrunner.ToolRunner('openssl', ['version']);
yield runner0.exec();

let runner = new toolrunner.ToolRunner('openssl',
const runner = new toolrunner.ToolRunner('openssl',
['enc', '-d', '-aes-256-cbc', '-md', 'sha512', '-salt', '-in',
enc_rsa_key_pth, '-out', 'config/deploy_id_rsa', '-k', deploy_key, '-a']);
enc_rsa_key_pth, '-out', 'config/deploy_id_rsa', '-k', deploy_key, '-a'], options);
yield runner.exec();

let runner1 = new toolrunner.ToolRunner('chmod', ['0600', 'config/deploy_id_rsa']);
const runner1 = new toolrunner.ToolRunner('chmod', ['0600', 'config/deploy_id_rsa'], options);
yield runner1.exec();

const authSock = '/tmp/ssh-auth.sock'
let runner2 = new toolrunner.ToolRunner('ssh-agent', ['-a', authSock]);
const runner2 = new toolrunner.ToolRunner('ssh-agent', ['-a', authSock]);
yield runner2.exec();

core.exportVariable('SSH_AUTH_SOCK', authSock);
let runner3 = new toolrunner.ToolRunner('ssh-add', ['config/deploy_id_rsa']);
const runner3 = new toolrunner.ToolRunner('ssh-add', ['config/deploy_id_rsa'], options);
yield runner3.exec();
});
}

function deploy(target) {
function deploy(target, options) {
return __awaiter(this, void 0, void 0, function* () {
let args = [];
if (!target) {
args = ['exec', 'cap', 'deploy'];
} else {
args = ['exec', 'cap', target, 'deploy'];
}
let runner = new toolrunner.ToolRunner('bundle', args);
const runner = new toolrunner.ToolRunner('bundle', args, options);
yield runner.exec();
});
}

function run() {
return __awaiter(this, void 0, void 0, function* () {
let target = core.getInput('target');
let deploy_key = core.getInput('deploy_key');
let enc_rsa_key_pth = core.getInput('enc_rsa_key_pth');
const target = core.getInput('target');
const deploy_key = core.getInput('deploy_key');
const enc_rsa_key_pth = core.getInput('enc_rsa_key_pth');
const working_directory = core.getInput('working-directory');

if (!deploy_key) {
core.setFailed('No deploy key given');
Expand All @@ -83,10 +89,10 @@ function run() {
if (!enc_rsa_key_pth) {
core.setFailed('Encrypted RSA private key undefined');
}

yield install_deps();
yield decrypt_key(deploy_key, enc_rsa_key_pth);
yield deploy(target);
const options = working_directory ? { cwd: working_directory, } : {};
yield install_deps(options);
yield decrypt_key(deploy_key, enc_rsa_key_pth, options);
yield deploy(target, options);
});
}

Expand Down

0 comments on commit a1ed9ca

Please sign in to comment.