Skip to content

Commit

Permalink
feat: supports multiple compose files (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Emilien Escalle <[email protected]>
  • Loading branch information
neilime and neilime authored Oct 5, 2022
1 parent bbeb376 commit 234ccc8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ jobs:
compose-file: "./docker/docker-compose.yml"
compose-flags: "--profile profile-1"
down-flags: "--volumes"
- uses: ./
with:
compose-file: |
./docker/docker-compose.yml
./docker/docker-compose.ci.yml
services: |
helloworld2
helloworld4
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ This action runs your docker-compose file and clean up before action finished.

**Optional** The name of the compose file. Default `"./docker-compose.yml"`.

It can be a list of files:

```yml
compose-file: |
docker-compose.yml
docker-compose.ci.yml
```
### `services`

**Optional** Just perform `docker-compose up` to one service instead of all of them
Expand All @@ -22,7 +30,7 @@ This action runs your docker-compose file and clean up before action finished.

### `compose-flags`

**Optional** Used to specify flags to pass to the `docker-compose` command. Default is none. A full list of flags can be found in the [docker-compose documentation](https://docs.docker.com/compose/reference/#command-options-overview-and-help).
**Optional** Used to specify flags to pass to the `docker-compose` command. Default is none. A full list of flags can be found in the [docker-compose documentation](https://docs.docker.com/compose/reference/#command-options-overview-and-help).

## Example usage

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: "Docker Compose Action"
description: "Run your docker-compose file"
inputs:
compose-file: # id of input
description: "relative path to compose file"
description: "relative path to compose file(s)"
required: false
default: "./docker-compose.yml"
compose-flags: # id of input
Expand Down
6 changes: 6 additions & 0 deletions docker/docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3.8"

services:
helloworld4:
profiles: [profile-2]
image: hello-world
23 changes: 13 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const core = require("@actions/core");
const compose = require("docker-compose");
const fs = require("fs");
const utils = require("./utils");

try {
const composeFile = core.getInput("compose-file");

if (!fs.existsSync(composeFile)) {
console.log(`${composeFile} not exists`);
const composeFiles = utils.parseComposeFiles(
core.getMultilineInput("compose-file")
);
if (!composeFiles.length) {
return;
}

const services = core.getMultilineInput("services", { required: false });;
const services = core.getMultilineInput("services", { required: false });
const options = {
config: composeFile,
config: composeFiles,
log: true,
composeOptions: utils.parseFlags(core.getInput("compose-flags")),
commandOptions: utils.parseFlags(core.getInput("up-flags"))
commandOptions: utils.parseFlags(core.getInput("up-flags")),
};

const promise =
Expand All @@ -25,8 +24,12 @@ try {
: compose.upAll(options);

promise
.then(() => { console.log("compose started"); })
.catch((err) => { core.setFailed(`compose up failed ${JSON.stringify(err)}`); });
.then(() => {
console.log("compose started");
})
.catch((err) => {
core.setFailed(`compose up failed ${JSON.stringify(err)}`);
});
} catch (error) {
core.setFailed(error.message);
}
12 changes: 6 additions & 6 deletions post.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const core = require("@actions/core");
const compose = require("docker-compose");
const fs = require("fs");
const utils = require("./utils");

try {
const composeFile = core.getInput("compose-file");
if (!fs.existsSync(composeFile)) {
console.log(`${composeFile} not exists`);
const composeFiles = utils.parseComposeFiles(
core.getMultilineInput("compose-file")
);
if (!composeFiles.length) {
return;
}

const options = {
config: composeFile,
config: composeFiles,
log: true,
composeOptions: utils.parseFlags(core.getInput("compose-flags")),
commandOptions: utils.parseFlags(core.getInput("up-flags"))
commandOptions: utils.parseFlags(core.getInput("up-flags")),
};

compose.down(options).then(
Expand Down
17 changes: 17 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
const fs = require("fs");

module.exports.parseFlags = (flags) => {
if (flags != null && typeof flags == "string" && flags.length > 0) {
return flags.split(" ");
}

return [];
};

module.exports.parseComposeFiles = (composeFiles) => {
return composeFiles.filter((composeFile) => {
if (!composeFile.length) {
return false;
}

if (!fs.existsSync(composeFile)) {
console.log(`${composeFile} not exists`);
return false;
}

return true;
});
};

0 comments on commit 234ccc8

Please sign in to comment.