Always test, but only deploy when on Master or development

Hi guys, I have a working pipeline (I am learning!), but I am left with one question.

I have a “master” and “development” branche. I enforced “merge requests” i.e. people have to work in a feature branch and whey they are done they commit a merge request.

When the merge request is added, it will automatically build my project (that is correct!), it will test my project (this is correct too), but then it will deploy to both Master and Development servers (through FTP and SSH).

What I want is that if the merge request goes to master, it only deploys to master. If the merge request goes to “development”, I want it to only deploy to development.

How can I do this? my current code is deploying to both… I was thinking that adding:

only:

  • master

would mean that this job would only run when something was merged to the master branch but apearently this doesnt work.

My code below:

stages:
    - build
    - test
    - deploy

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

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

deploy test:
    stage: deploy
    only:
        - test
        - merge_requests
    before_script:
        - 'which ssh-agent || ( apt-get install -qq openssh-client )'
        - eval $(ssh-agent -s)
        - ssh-add <(echo "$SSH_PRIVATE_KEY")
        - mkdir -p ~/.ssh
        - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    script:
        - apt-get update -qq && apt-get install -y -qq lftp
        - lftp -c "some commands here";
		- ssh "some commands here";

deploy master:
    stage: deploy
    only:
        - master
        - merge_requests
    before_script:
        - 'which ssh-agent || ( apt-get install -qq openssh-client )'
        - eval $(ssh-agent -s)
        - ssh-add <(echo "$SSH_PRIVATE_KEY")
        - mkdir -p ~/.ssh
        - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    script:
        - apt-get update -qq && apt-get install -y -qq lftp
        - lftp -c "some commands here";
		- ssh "some commands here";

so basically what I want to archieve is:

  1. when merging to master, deploy to master only (deploy master)
  2. when merging to development, deploy to development only (deploy test)