mirror of
https://github.com/hoverkraft-tech/compose-action.git
synced 2026-01-10 06:33:06 +08:00
feat: supports multiple compose files (#8)
Co-authored-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
parent
bbeb376475
commit
234ccc8f01
8
.github/workflows/main.yml
vendored
8
.github/workflows/main.yml
vendored
@ -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
README.md
10
README.md
@ -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
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
6
docker/docker-compose.ci.yml
Normal file
6
docker/docker-compose.ci.yml
Normal file
@ -0,0 +1,6 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
helloworld4:
|
||||
profiles: [profile-2]
|
||||
image: hello-world
|
||||
23
main.js
23
main.js
@ -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 =
|
||||
@ -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
post.js
12
post.js
@ -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(
|
||||
|
||||
17
utils.js
17
utils.js
@ -1,3 +1,5 @@
|
||||
const fs = require("fs");
|
||||
|
||||
module.exports.parseFlags = (flags) => {
|
||||
if (flags != null && typeof flags == "string" && flags.length > 0) {
|
||||
return flags.split(" ");
|
||||
@ -5,3 +7,18 @@ module.exports.parseFlags = (flags) => {
|
||||
|
||||
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;
|
||||
});
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user