Help to improve pipeline

Hello.
There is a gitlab-ci.yml file:

stages:
    - build
    - push
    - deploy

Dockerbuild:
  stage: build
  before_script:
    - 'command -v ssh-agent >/dev/null || ( apk add --update openssh )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan $ADDRESS >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - scp -o StrictHostKeyChecking=no file $SSH_USER@$ADDRESS:/home/$SSH_USER/

  tags:
    - shell

Dockerpush:
  stage: push
  before_script:
    - 'command -v ssh-agent >/dev/null || ( apk add --update openssh )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan $ADDRESS >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts 
  script:
    - scp -o StrictHostKeyChecking=no file $SSH_USER@$ADDRESS:/home/$SSH_USER/
  tags:
    - shell

Deploy:
  stage: deploy
  before_script:
    - 'command -v ssh-agent >/dev/null || ( apk add --update openssh )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan $ADDRESS >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - scp -o StrictHostKeyChecking=no file $SSH_USER@$ADDRESS:/home/$SSH_USER/
  tags:
    - shell

For every stage it should do the same before_script.
Is it possible don’t repeat it every time and make little bit shorter?

You could try put the before script in the ‘default’ keyword, then it will run before every job.