Different artifacts depending on job success

Hello,

assume the following:
- I have a build process that produces a binary and a log file
- both files are created even if the build command fails
- the build command takes a long time

I want to automate the build in this setup with GitLab CI/CD.
As a result I wish for:
- a link to the latest successfully build binary
- a link to the latest log file (especially if the build command fails)

One CI/CD configuration I can think about that realizes this is:

build:
    script:
        - build_command
artifacts:
    paths:
        - binary

create_log:
    script:
        - build_command
artifacts:
    when: always
    paths:
        - log_file

However the costly build_command is run here twice!
I can also not put the binary as an artifact in the second job (deleting the
first one since it would upload a broken artifact on failure).

An example for this type of build commands is given by LaTeX compilers.

Am I missing a possible solution?

Preferably I would be allowed to specify the when clause for every artifact
separately. This would allow the more efficient CI/CD configuration

build:
    script:
        - build_command
artifacts:
    paths:
        on_success:
            - binary
        always:
            - log_file

It is probably not the most elegant way to introduce three new keywords
(always, on_success, on_failure) but it is better than having no way of
specifying the behaviour at all.

Thanks for any help.
Best regards,
Franz

PS: A similar question was asked here on StackOverflow.

Would archiving both files within the first build job work? artifacts.paths is an array so you can pass multiple files to it like this:

build:
    script:
        - build_command
    artifacts:
      paths:
        - binary
        - log_file

Sadly no. My build_command produces a binary, even if the job fails. That means for example a link to the binary would be invalid after a pipeline fails.

I just saw that the indentation in the examples is messed up but somehow I cannot edit the question.
Of course all artifacts blocks should be indented once.