Deploy to master with refs not working properly

Ha guys, I am learning how to setup a pipeline. At the moment my pipeline(s) are all working except for 1 little part.

What I want is that for each merge request, every part except for the " deploy master" part is run. This is working currently. But after everything is oke and we “merge into master”, then I want to “deploy to master” part to run.

This is working, but the problem now is that GIT had no “files” to work with because the “build stage” only runs on a merge request and therefor no files exist in the “deploy to master” part.

Can anyone tell me how I can fix this? my current code is below:

stages:
    - build
    - test
    - deploy

build project:
    stage: build
    image: node:11
    only:
        - merge_requests
    script:
        - npm install --progress=false
        - npm run build
    artifacts:
        paths:
            - ./dist

test build:
    stage: test
    image: alpine
    only:
        - merge_requests
    script:
        - cd dist
        - grep -q "<title>my website</title>" index.html

deploy master:
    stage: deploy
    only:
        refs:
            - master
    before_script:
        - do something
    script:
        - do something

As said the problem now is that:

On merge we run:

  1. Build project
  2. Test build

which is perfectly fine.

On merge to master we only run:

  1. Deploy to master

While we should run

  1. Build project
  2. Test build (optional but not manditory I recon)
  3. Deploy to master