Skip to content

Commit

Permalink
feat: make docker-compose cwd aware
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
  • Loading branch information
neilime committed Apr 2, 2024
1 parent 43a233a commit 6d5d942
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 22 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/__check-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,27 @@ jobs:
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | grep docker-helloworld4-1
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | (grep docker-helloworld-1 && echo "Unexpected service is running" && exit 1) || true
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.ci.yml ps | (grep docker-helloworld3-1 && echo "Unexpected service is running" && exit 1) || true
test-action-with-cwd:
runs-on: ubuntu-latest
name: Test with cwd
steps:
- uses: actions/checkout@v4
- uses: hoverkraft-tech/ci-github-nodejs/actions/setup-node@0.4.2

- name: Act
uses: ./
with:
compose-file: "docker-compose.yml"
cwd: "./docker"
services: |
helloworld2
helloworld3
- name: "Assert: only expected services are running"
run: |
docker-compose -f ./docker/docker-compose.yml ps
docker-compose -f ./docker/docker-compose.yml ps | grep docker-helloworld2-1
docker-compose -f ./docker/docker-compose.yml ps | grep docker-helloworld3-1
docker-compose -f ./docker/docker-compose.yml ps | (grep docker-helloworld-1 && echo "Unexpected service helloworld is running" && exit 1) || true
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
29 changes: 16 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
name: 'Docker Compose Action'
description:
'This action runs your docker-compose file and clean up before action finished'
author: 'Hoverkraft'
name: "Docker Compose Action"
description: "This action runs your docker-compose file and clean up before action finished"
author: "Hoverkraft"
branding:
icon: anchor
color: gray-dark

inputs:
compose-file:
description: 'Relative path to compose file(s). It can be a list of files.'
description: "Relative path to compose file(s). It can be a list of files."
required: false
default: './docker-compose.yml'
default: "./docker-compose.yml"
services:
description: 'Services to perform docker-compose up.'
description: "Services to perform docker-compose up."
required: false
up-flags:
description: 'Additional options to pass to `docker-compose up` command.'
description: "Additional options to pass to `docker-compose up` command."
required: false
default: ''
default: ""
down-flags:
description: 'Additional options to pass to `docker-compose down` command.'
description: "Additional options to pass to `docker-compose down` command."
required: false
default: ''
default: ""
compose-flags:
description: 'Additional options to pass to `docker-compose` command.'
description: "Additional options to pass to `docker-compose` command."
required: false
default: ''
default: ""
cwd:
description: "Current working directory"
required: false
default: ${{ github.workspace }}
runs:
using: node20
main: dist/index.js
Expand Down
12 changes: 10 additions & 2 deletions dist/index.js

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

12 changes: 10 additions & 2 deletions dist/post.js

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

12 changes: 9 additions & 3 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe("run", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
}));

await main.run(main.RunAction.UP);
Expand All @@ -52,7 +53,7 @@ describe("run", () => {
// Verify that all of the functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
1,
'inputs: {"composeFiles":[],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[]}'
'inputs: {"composeFiles":[],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/current/working/dir"}'
);
expect(warnMock).toHaveBeenNthCalledWith(1, "no compose files found");
expect(upMock).not.toHaveBeenCalled();
Expand All @@ -66,6 +67,7 @@ describe("run", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
}));

upMock.mockResolvedValueOnce();
Expand All @@ -76,7 +78,7 @@ describe("run", () => {
// Verify that all of the functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
1,
'inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[]}'
'inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/current/working/dir"}'
);

expect(upMock).toHaveBeenCalledWith({
Expand All @@ -85,6 +87,7 @@ describe("run", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
});

expect(setFailedMock).not.toHaveBeenCalled();
Expand All @@ -98,6 +101,7 @@ describe("run", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
}));

upMock.mockRejectedValueOnce(new Error("unkown error"));
Expand All @@ -116,6 +120,7 @@ describe("run", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
}));

downMock.mockResolvedValueOnce();
Expand All @@ -126,7 +131,7 @@ describe("run", () => {
// Verify that all of the functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
1,
'inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[]}'
'inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/current/working/dir"}'
);

expect(downMock).toHaveBeenCalledWith({
Expand All @@ -135,6 +140,7 @@ describe("run", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
});

expect(setFailedMock).not.toHaveBeenCalled();
Expand Down
6 changes: 6 additions & 0 deletions src/services/docker-compose.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("DockerComposeService", () => {
composeFlags: [],
upFlags: [],
downFlags: [],
cwd: "/current/working/dir",
};

await service.up(inputs);
Expand All @@ -38,6 +39,7 @@ describe("DockerComposeService", () => {
commandOptions: [],
config: ["docker-compose.yml"],
log: true,
cwd: "/current/working/dir",
});
});

Expand All @@ -48,6 +50,7 @@ describe("DockerComposeService", () => {
composeFlags: [],
upFlags: ["--build"],
downFlags: [],
cwd: "/current/working/dir",
};

await service.up(inputs);
Expand All @@ -57,6 +60,7 @@ describe("DockerComposeService", () => {
commandOptions: ["--build"],
config: ["docker-compose.yml"],
log: true,
cwd: "/current/working/dir",
});
});
});
Expand All @@ -69,6 +73,7 @@ describe("DockerComposeService", () => {
composeFlags: [],
upFlags: [],
downFlags: ["--volumes", "--remove-orphans"],
cwd: "/current/working/dir",
};

await service.down(inputs);
Expand All @@ -78,6 +83,7 @@ describe("DockerComposeService", () => {
commandOptions: ["--volumes", "--remove-orphans"],
config: [],
log: true,
cwd: "/current/working/dir",
});
});
});
Expand Down
1 change: 1 addition & 0 deletions src/services/docker-compose.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class DockerComposeService {
config: inputs.composeFiles,
log: true,
composeOptions: inputs.composeFlags,
cwd: inputs.cwd,
};
}
}
22 changes: 22 additions & 0 deletions src/services/input.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe("InputService", () => {
expect(inputs.composeFiles).toEqual(["file1"]);
});
});

describe("services", () => {
it("should return given services input", () => {
getMultilineInputMock.mockImplementation((inputName) => {
Expand All @@ -89,6 +90,7 @@ describe("InputService", () => {
expect(inputs.services).toEqual(["service1", "service2"]);
});
});

describe("compose-flags", () => {
it("should return given compose-flags input", () => {
getMultilineInputMock.mockReturnValue([]);
Expand Down Expand Up @@ -116,6 +118,7 @@ describe("InputService", () => {
expect(inputs.composeFlags).toEqual([]);
});
});

describe("up-flags", () => {
it("should return given up-flags input", () => {
getMultilineInputMock.mockReturnValue([]);
Expand Down Expand Up @@ -143,6 +146,7 @@ describe("InputService", () => {
expect(inputs.upFlags).toEqual([]);
});
});

describe("down-flags", () => {
it("should return given down-flags input", () => {
getMultilineInputMock.mockReturnValue([]);
Expand Down Expand Up @@ -170,5 +174,23 @@ describe("InputService", () => {
expect(inputs.downFlags).toEqual([]);
});
});

describe("cwd", () => {
it("should return given cwd input", () => {
getMultilineInputMock.mockReturnValue([]);
getInputMock.mockImplementation((inputName) => {
switch (inputName) {
case InputNames.Cwd:
return "cwd";
default:
return "";
}
});

const inputs = service.getInputs();

expect(inputs.cwd).toEqual("cwd");
});
});
});
});
Loading

0 comments on commit 6d5d942

Please sign in to comment.