Gitlab ci for staging and prod

let’s say we have repository and dev create branch and write code on that branch and test it then after they pull request to merge to main branch
devops engineer needs to write a gitlab ci pipeline
my question is I want in the pipeline to test and build the app then deploy it to staging env if dev push code changes in other branches
then in case of pull request I want to deploy the application to the production env
how can I achieve that in the pipeline, can you provide me with an example