Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/set target permissions #15

Merged
merged 14 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ The name of the version to deploy. Default: sha value of the commit triggering

A pipe-delimited list of artisan commands to run after the deployment. Example: `clear-compiled|migrate|optimize`

### `writableDirectories`

A pipe-delimited list of paths relative to the application root that need to be writable by the web server. Example: `storage` for Laravel, `application/cache|application/logs` for Kohana.

### `artifact`

The path to the deployable tarball. Default: `./artifact.tar.gz`
Expand All @@ -43,6 +47,11 @@ The number of versions to keep on the target VM. The most recent number of conf
All others will be removed. The default behavior is to not remove anything. This can also be achieved with
an explicit value of `"0"`

### `postDeploymentCommands`

A pipe-delimited list of commands to run after the deployment. These will be run in the directory to which
the code was deployed.

## Example usage

```
Expand All @@ -54,6 +63,8 @@ with:
version:"v1.0.0"
key: "~/identityfile"
artisanCommands: "config:cache|migrate"
postDeploymentCommands: "sudo supervisorctl restart foo-worker"
writableDirectories: "storage"
artifact: "some-other-tarball.tar.gz"
numberOfVersionsToKeep: 5
```
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
description: "Number of most recent versions to keep. (0 = all)"
required: false
default: "0"
postDeploymentCommands:
description: "Pipe-delimited list of commands to run as part of the deployment"
required: false
default: ""

runs:
using: 'node12'
Expand Down
36 changes: 33 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,16 +956,28 @@ function getRequiredInput(input) {
return capturedInput;
}

function parseArrayFromPipeDelimitedString(pipeDelimitedString) {
return pipeDelimitedString.split("|").filter(x => x !== "");
}

function getOptions() {
return {
user: getRequiredInput("user"),
host: getRequiredInput("host"),
versionsRoot: getRequiredInput("versionsRoot"),
version: core.getInput("version") || process.env.GITHUB_SHA || "",
key: getRequiredInput("key"),
artisanCommands: core.getInput("artisanCommands").split("|"),
artisanCommands: parseArrayFromPipeDelimitedString(
core.getInput("artisanCommands")
),
writableDirectories: parseArrayFromPipeDelimitedString(
core.getInput("writableDirectories")
),
artifact: core.getInput("artifact"),
versionsToKeep: core.getInput("numberOfVersionsToKeep") || 0
versionsToKeep: core.getInput("numberOfVersionsToKeep") || 0,
postDeploymentCommands: parseArrayFromPipeDelimitedString(
core.getInput("postDeploymentCommands")
)
};
}

Expand Down Expand Up @@ -1020,7 +1032,14 @@ async function setTargetPermissions(options) {
const { versionsRoot, version } = options;
const path = `${versionsRoot}/${version}`;
await executeSSH(options, `chmod -R 770 ${path}`);
await executeSSH(options, `chmod -R 775 ${path}/storage`);
/* eslint-disable no-restricted-syntax */
for (const dir of options.writableDirectories) {
// eslint-disable-next-line no-await-in-loop
await executeSSH(
options,
`stat ${path}/${dir} >/dev/null && chmod -R 775 ${path}/${dir}`
);
}
}

async function executeArtisan(options) {
Expand All @@ -1036,6 +1055,16 @@ async function executeArtisan(options) {
/* eslint-enable no-restricted-syntax */
}

async function executePostDeploymentCommands(options) {
const { versionsRoot, version } = options;
/* eslint-disable no-restricted-syntax */
for (const command of options.postDeploymentCommands) {
// eslint-disable-next-line no-await-in-loop
await executeSSH(options, `cd ${versionsRoot}/${version}; ${command}`);
}
/* eslint-enable no-restricted-syntax */
}

async function updateSymlink(options) {
const { version, versionsRoot } = options;
await executeSSH(options, `rm -f ${versionsRoot}/current`);
Expand Down Expand Up @@ -1083,6 +1112,7 @@ async function main() {
await updateVersionDirectoryTimeStamp(options);
await setTargetPermissions(options);
await executeArtisan(options);
await executePostDeploymentCommands(options);
await updateSymlink(options);
await removeOldVersions(options);
}
Expand Down
36 changes: 33 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ function getRequiredInput(input) {
return capturedInput;
}

function parseArrayFromPipeDelimitedString(pipeDelimitedString) {
return pipeDelimitedString.split("|").filter(x => x !== "");
}

function getOptions() {
return {
user: getRequiredInput("user"),
host: getRequiredInput("host"),
versionsRoot: getRequiredInput("versionsRoot"),
version: core.getInput("version") || process.env.GITHUB_SHA || "",
key: getRequiredInput("key"),
artisanCommands: core.getInput("artisanCommands").split("|"),
artisanCommands: parseArrayFromPipeDelimitedString(
core.getInput("artisanCommands")
),
writableDirectories: parseArrayFromPipeDelimitedString(
core.getInput("writableDirectories")
),
artifact: core.getInput("artifact"),
versionsToKeep: core.getInput("numberOfVersionsToKeep") || 0
versionsToKeep: core.getInput("numberOfVersionsToKeep") || 0,
postDeploymentCommands: parseArrayFromPipeDelimitedString(
core.getInput("postDeploymentCommands")
)
};
}

Expand Down Expand Up @@ -74,7 +86,14 @@ async function setTargetPermissions(options) {
const { versionsRoot, version } = options;
const path = `${versionsRoot}/${version}`;
await executeSSH(options, `chmod -R 770 ${path}`);
await executeSSH(options, `chmod -R 775 ${path}/storage`);
/* eslint-disable no-restricted-syntax */
for (const dir of options.writableDirectories) {
// eslint-disable-next-line no-await-in-loop
await executeSSH(
options,
`stat ${path}/${dir} >/dev/null && chmod -R 775 ${path}/${dir}`
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a /* eslint-enabled no-restricted-syntax */ after the for

}

async function executeArtisan(options) {
Expand All @@ -90,6 +109,16 @@ async function executeArtisan(options) {
/* eslint-enable no-restricted-syntax */
}

async function executePostDeploymentCommands(options) {
const { versionsRoot, version } = options;
/* eslint-disable no-restricted-syntax */
for (const command of options.postDeploymentCommands) {
// eslint-disable-next-line no-await-in-loop
await executeSSH(options, `cd ${versionsRoot}/${version}; ${command}`);
}
/* eslint-enable no-restricted-syntax */
}

async function updateSymlink(options) {
const { version, versionsRoot } = options;
await executeSSH(options, `rm -f ${versionsRoot}/current`);
Expand Down Expand Up @@ -137,6 +166,7 @@ async function main() {
await updateVersionDirectoryTimeStamp(options);
await setTargetPermissions(options);
await executeArtisan(options);
await executePostDeploymentCommands(options);
await updateSymlink(options);
await removeOldVersions(options);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "laravel-vm-deployment-action",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down