hoverkraft-tech / compose-action

This action runs your docker-compose file and clean up before action finished
https://github.com/marketplace/actions/docker-compose-action
MIT License
148 stars 24 forks source link

attach mode is not working #74

Closed mfmbarros closed 3 months ago

mfmbarros commented 3 months ago

Description: A clear and concise description of what the bug is.

Looks like detach mode is enabled by default as I'm not passing any flags and still, no output is observed after docker compose starts the container, which is somehow unexpected since detach mode is disabled by default when using docker-compose cli directly. Nevertheless, I couldn't enable attach mode by passing either "--attach myContainer" or "--attach-dependencies" in up-flags, both ending on the following error:

Run hoverkraft-tech/compose-action@v2.0.0
2.23.0
docker-compose version: 2.23.0
--detach cannot be combined with --abort-on-container-exit, --attach or --attach-dependencies
Error: {"exitCode":1,"err":"--detach cannot be combined with --abort-on-container-exit, --attach or --attach-dependencies\n","out":""}

Action version: hoverkraft-tech/compose-action@v2.0.0

Runner type:

Repro steps:

    - name: Run test container
      uses: hoverkraft-tech/compose-action@v2.0.0
      with:
        compose-file: ${{ inputs.docker-compose-file }}
        up-flags: "--attach-dependencies"
      env:
        TF_VAR_token: ${{ secrets.MY_TOKEN}}

Expected behavior: Expect to run container in attach mode

Actual behavior: When forcing attach mode, action errors with the message provided in description section.

github-actions[bot] commented 3 months ago

Hi, thank for reporting an issue, we will check it out very soon

neilime commented 3 months ago

@mfmbarros we can imagine to pass an option to not detach. But there will be an issue, as you will attach some services, the action will never end up. So the workflow will be stuck.

I may miss something, so can you explain me how you want to deal with attach mode.

mfmbarros commented 3 months ago

My use case is rather related with integration tests where tests are executed when container is started. After tests passed, container gracefully shuts down. I actually could test this by passing "--abort-on-container-exit" and everything went as expected except step got marked as failed precisely because container exited ( even after tests passed with success )

neilime commented 3 months ago

If you enable debug mode you will get logs at the end of the workflow. Can it help?

mfmbarros commented 3 months ago

I think not because if something went wrong on container execution, ideally pipeline should fail and right now it will succeed. Still I tried the debug mode and looks like container logs weren't sent to stdout:

Run hoverkraft-tech/compose-action@v2.0.0
##[debug]inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/runner/_work/terraform-module-test/terraform-module-test"}
2.23.0
docker-compose version: 2.23.0
 Container terraform-module-tests-1  Recreate
 Container terraform-module-tests-1  Recreated
 Container terraform-module-tests-1  Starting
 Container terraform-module-tests-1  Started
docker-compose is up
##[debug]Node Action run completed with exit code 0
##[debug]Finishing: Run test container 

Just to clarify, I enabled debug mode by just setting a secret "ACTIONS_STEP_DEBUG" set to true.

neilime commented 3 months ago

The logs should be displayed in the "Post Run hoverkraft-tech/compose-action" step

mfmbarros commented 3 months ago

Sorry I pasted the wrong task:

##[debug]inputs: {"composeFiles":["docker-compose.yml"],"services":[],"composeFlags":[],"upFlags":[],"downFlags":[],"cwd":"/runner/_work/terraform-module-tests/terraform-module-tests"}
ok
##[debug]docker-compose logs:
##[debug]
 Container terraform-module-tests-1  Stopping
 Container terraform-module-tests-1  Stopped
 Container terraform-module-tests-1  Removing
 Container terraform-module-tests-1  Removed
 Network terraform-module-tests_default  Removing
 Network terraform-module-tests_default  Removed

Logs look empty. But anyway I think this would not fulfil this sort of need as I said, whenever a test got a failure ideally pipeline should fail as well, otherwise step will always be marked as successful even when container got a failure

neilime commented 3 months ago

I've created a pull request (#75) to have a real example by using "attach-dependencies". As you can see, the action step remains stuck:

https://github.com/hoverkraft-tech/compose-action/actions/runs/9552517523/job/26329459869

mfmbarros commented 3 months ago

In your case yes because one of the services is a database server which will be running forever so yes it would not fit that use case, but other cases where a container has to execute a set of tasks and in the end exits gracefully, in theory attach mode would work well in this case.

Said that, seems you were able to test "--attach-dependencies" parameter, however I couldn't and this is actually my real issue here, every time I set it , pipeline breaks with:

{"exitCode":1,"err":"--detach cannot be combined with --abort-on-container-exit, --attach or --attach-dependencies\n","out":""}

and my step configuration looks almost identical with yours. Just double checking, hoverkraft-tech/compose-action@v2.0.0 is the latest version right?

neilime commented 3 months ago

Now I understand your issue. Please can you check with the version v2.0.1

mfmbarros commented 3 months ago

Sorry for not turning this clearer, the error is gone, thanks.

Unfortunately this approach didn't solve my use case as docker-compose is up even after a container exited with code 1, making the step to be marked as success.

Are there any plans to support "run" command ? I tested locally and I think this use case may work using run instead of up

neilime commented 3 months ago

I've update the PR #75, as you can see that I'm checking the exit code in a second step. Maybe this way can achieve your goals

mfmbarros commented 3 months ago

that looks good, going to try.

mfmbarros commented 3 months ago

that worked very well, I'm finally understanding this action flow, not only check exit code did the trick but also container logs are now displayed on post task. Said that, I don't even need attach mode anymore, just have to be sure container execution is finished before exit code assertion and that can be achieved by asserting the State of container.

Thanks for the help