I will just link to 2 good articles how to do this
https://www.bounteous.com/insights/2020/03/11/automate-drupal-deployments/
https://chromatichq.com/blog/drupal-8-deployment-scripts
Would recommend first one as second is basic and some steps should go in other way.