Search code examples
gitlabgitlab-ciheroku-cli

run command after deploy using Gitlab CI/CD


I'm setting up Gitlab CI/CD to automate deployment to heroku app with every push.

currently my .gitlab-ci.yml file looks like

production:
  type: deploy
  script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl
  - dpl --provider=heroku --app=koober-production --api-key=$HEROKU_PRODUCTION_API_KEY
  only:
  - master

This works fine and deployment is successful and application is working.

But, I need to run few commands after successful deployment to migrate database.

At present, I need to do this manually by running command from terminal

heroku run python manage.py migrate -a myapp

How can I automate this to run this command after deployment?


Solution

  • Solved using --run flag to run command using dpl

    stages:
      - deploy
    
    production:
      stage: deploy
      script:
      - apt-get update -qy
      - apt-get install -y ruby-dev
      - gem install dpl
      - dpl --provider=heroku --app=koober-production --api-key=$HEROKU_PRODUCTION_API_KEY --run='python manage.py migrate && python manage.py create_initial_users'
      only:
      - master