CI/CD with a dev branch and main branch
I want to enable CI/CD for my application so I created a
.gitlab-ci.yml file for when I deploy to heroku
inside the file I should have something like
- gem install dpl
- dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_PRODUCTION_KEY
In the git repo I have two branches ‘main’ and ‘dev’
when I push to main, It automatically should push the repo to my heroku app with the name HEROKU_APP_NAME
but when I push changes to the dev branch I want to push the repo to a different heroku app HEROKU_APP_NAME_DEV
When you put your
HEROKU_APP_NAME in your CI variables (under Settings → CI/CD → Variables) you can limit the scope of the variable by environment, and so use different values for the same variable on different branches.
It would probably be useful for you to read the docs on environments.
you can set CI/CD variables based on how different rules evaluate. This feature has been added in GitLab 13.10.
Mapping your example with a concrete example of rules, and defining 2 rules to match the default branch (
dev in the branch name. When the rules are matched, they override the default variable.
For testing purposes, I have added the names on top in the
variables section. Suggest moving this into the project settings next to the API KEY.
HEROKU_APP_NAME_DEV: deftextdev # override with manual pipeline run
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
- if: $CI_COMMIT_REF_NAME =~ /dev/
- echo "Deploying to $HEROKU_DEPLOY_APP"
- gem install dpl
- dpl --provider=heroku --app=$HEROKU_DEPLOY_APP --api-key=$HEROKU_PRODUCTION_KEY
The variables section on top is only for easy-testing. Recommend to remove them, and keep using CI/CD variables from the project settings to keep the credentials safe.
Verification in the Pipeline Editor
When run in the default branch, the output changes.heroku_deploy (#2896168186) · Jobs · Michael Friedrich / ci-cd-playground · GitLab
On a branch containing the name
dev the CI script uses the _DEV variable value. heroku_deploy (#2896190604) · Jobs · Michael Friedrich / ci-cd-playground · GitLab
The linked pipelines failed because I do not have Heroku-specific values set.
I hope this helps
i would prefer the example to use different names for the stage where the
heroku_deploy is run.
stage: production in an example where the app name is overridden by something called
_DEV might mislead people.
imho you should always be VERY distinct on naming stuff and obviously a dev branch or MR artifact should not be deployed to production (yes yes there might be edge cases:D)
i would suggest calling the “production” stage “deploy”. this would describe what it actually does and would leave room for other deploy jobs like
@cy4n Thanks for moving the discussion here, and sharing your feedback. I’ve missed to verify the full context when creating an example - updated the config snippet and replaced the screenshot above