How to not repeat CI step when broader pipeline is triggered?

Let’s say I have pipeline with 2 steps:

  • Tests - run on every push to master
  • Deploy - run when new tag is created.

Current behavior is: when I push to master any commit, tests will run on that commit, when I push tag pointing to the same commit, tests will run again, despite them already being finished on exactly the same
code and environment.

What I want to achieve: when I push to master, tests will run, as above. When I push tag pointing to the commit for which tests have already passed, it should skip directly to deploy. If tests are scheduled or running, it should wait for them to finish. If tests failed, it should immediately fail. If the pipeline wasn’t triggered on push, then and only then it should run tests before deploying.

Is this possible with GitLab CI?