Makefiles and GitLab CI stages

Please see this repository for a minimal example of what I’m talking about.

master has the problem, while branch 2-touch-files has a workaround that doesn’t scale well.

I wonder if there is a way to make GitLab CI stages work with Make, such that I can re-use the Makefile targets that I use locally in GitLab CI without invoking the commands manually via .gitlab-ci.yml. I prefer not to repeat these commands and logic.

I believe the reason I see a recompile of the helloworld binary because the timestamp of the helloworld.cpp file is newer (due to checkout) than the helloworld binary. I therefore wonder if there is a way to prevent this from happening, other than touching all artifacts from previous stages in every stage.


I’m afraid that I do not understand the problem in its full extent. Is it about keeping artifacts over stages, or do you want to compile the cpp source code in every job?

From the branch which solves the problem:

I’ve also found the job output at

One thing which comes to mind in looking at the Makefile:

  • Add a make clean target
  • Call this at the end of each job


Hi Michael,

Thanks for taking the time to look at this!
The helloworld binary from the build stage should be transferred to the test stage. It is, but the files which are checked out of git in the test stage have a newer update time than the helloworld binary. This causes make to re-build the helloworld binary.

I’ve added the ls -lh command and the touch example to illustrate that the rebuilding is due to file update times.

If I were to use the touch method, I would need to keep track of make-target dependencies in both the Makefile and .gitlab-ci.yml. Since I would rather not duplicate this, I wonder if there is another way to prevent the artifacts from previous stages to have earlier update times than the freshly checked out commit.

Thanks for mentioning make clean. I would include that in real-world Makefile files.