Pushing to stage branch triggers deployment to stage app but builds from master branch

As the title says, when I push to stage, I expect the latest commits from my stage branch to appear in my stage app on Heroku. The build triggers a deployment to Heroku, but it’s pushing my master branch, not my stage branch. FWIW, this config file used to work as expected. Not sure what happened.

See config.yml below:

workflows:
  version: 2
  build-deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master

  build-deploy-stage:
    jobs:
      - build
      - deploy-stage:
          requires:
            - build
          filters:
            branches:
              only: stage
version: 2
jobs:
  build:
    working_directory: ~/doc-uploader
    docker:
      - image: circleci/ruby:2.6.3-node-browsers
        environment:
          PGHOST: localhost
          PGUSER: doc-uploader
          RAILS_ENV: test

      - image: postgres:9.6
        environment:
          POSTGRES_HOST_AUTH_METHOD: trust
          POSTGRES_USER: doc-uploader
          POSTGRES_DB: doc-uploader_test

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run:
          name: install dependencies
          command: |
            bundle install --jobs=4 --retry=3 --path vendor/bundle

      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}

      - run:
          name: Database setup
          command: bundle exec rake db:setup

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      # Run Tests
      - run:
          name: run tests
          command: |
            mkdir /tmp/test-results
            TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
            bundle exec rspec --format progress \
                            --out /tmp/test-results/rspec.xml \
                            --format progress \
                            $TEST_FILES

      # collect reports
      - store_test_results:
          path: /tmp/test-results
      - store_artifacts:
          path: /tmp/test-results
          destination: test-results

  deploy:
    docker:
      - image: buildpack-deps:trusty

    working_directory: ~/doc-uploader

    steps:
      - checkout
      - run:
          name: Deploy Master to Heroku
          command: |
            git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git

  deploy-stage:
    docker:
      - image: buildpack-deps:trusty

    working_directory: ~/doc-uploader

    steps:
      - checkout
      - run:
          name: Deploy Stage to Heroku
          command: |
            git push --force https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_STAGE_APP_NAME.git

2 things

  • check the value of CIRCLE_BRANCH in the “Preparing environment variables” step

  • If you have any form of caching for the checked-out git repo, make sure that you have not been switching branches as you have worked on the project. I’ve not spent much time looking at the checkout script or built-in caching, but I have wasted a lot of my own time on a situation where I checkout one branch and the script continued to use that branch regardless of other changes as a cached copy of the git repo will use the branch already cached when it is updated with say a ‘git pull’ command.

Thanks so much for your response.

I checked the Preparing environment variables step and see the following:

CIRCLE_BRANCH=stage

So looks like there’s no issue there.

I’m not sure how to look into the caching issue - my most recent commits are on GitHub, would the caching issue be between GitHub and circleci?

Yes, the caching issue I found was at the CircleCI end - the most basic being caused by the checkout command that creates a local git repo and then maintains it with pull commands, which has caused me issues with self-hosted runners that are persistent by default.

You are using the CircleCI provided save_cache and restore_cache steps, but I do not know much about them as I use self-hosted runners that do not support these commands.

I figured it out - In case anyone else is experiencing this issue, here’s what I did:

I noticed the following in the “Deploy [branch] to Heroku” step:

warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

So I changed my deploy jobs to include this command. I then noticed this in the “Deploy [branch] to Heroku” logs:

remote: Pushed to branch other than [main, master], skipping build. 

Here’s the final version of my deploy jobs that takes care of the two issues mentioned above:

deploy:
    docker:
      - image: buildpack-deps:trusty

    working_directory: ~/doc-uploader

    steps:
      - checkout
      - run:
          name: Deploy Master to Heroku
          command: |
            git config --global push.default matching
            git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git

  deploy-stage:
    docker:
      - image: buildpack-deps:trusty

    working_directory: ~/doc-uploader

    steps:
      - checkout
      - run:
          name: Deploy Stage to Heroku
          command: |
            git config --global push.default matching
            git push --force https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_STAGE_APP_NAME.git stage:master

Note the state:master after the Heroku URL in the deploy-stage job.

I hope this helps someone!

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.