How to check CircleCI code and automatically deploy to Heroku

Introduction

This article was created on the assumption that you have read the following. Let CircleCI perform a deployment to Heroku after a commit to the master branch on Github (https://qiita.com/NaokiIshimura/items/a897fba536caa4dd3338)

In this article, I will explain how to configure a CI/CD pipeline with Heroku using CircleCI for myself. Basically, I referred to the official document, so I recommend you to read this as well.

Deploy Configuration (://circleci.com/docs/ja/2.0/deployment-integrations/#heroku)

Development environment

Ruby 2.6.5 Rails 6.0.0 Bundler 2.1.4 MySQL 5.6.50

Build a CI/CD pipeline to Heroku

It is assumed that you have set up the Heroku app, so if you haven't done so already, refer to here and manually ploy to Heroku before doing so.

1) Add environment variables

Add the Heroku application name and Heroku API key as environment variables to automatically probe with CircleCI. (Environment variable settings in CircleCI are here) Hereafter, the application name will be treated as HEROKU_APP_NAME and the Heroku API key will be treated as HEROKU_API_KEY.

2) Automatically deploy master branch with git push

Below is the code.

ruby:.cricleci/config.yml


deploy:
    docker:
      - image: buildpack-deps:trusty
    executor: heroku/default
    steps:
      - checkout # push/Checking merged code
      - run:
          name:Deploy master to Heroku
          command: |
            git push https://heroku:[email protected]/$HEROKU_APP_NAME.git master

By writing this code, it will check the code and perform automatic deployment.

3) workflow settings

ruby:.cricleci/config.yml


workflows:
  version: 2.1
  build-deploy:
    jobs:

      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master

The official documentation allows customization, but as a beginner I haven't reached that area, so I used the workflow that was prepared with the goal of building a pipeline.

Completion code

ruby:.circleci/config.yml


version: 2.1
orbs:
  ruby: circleci/[email protected] 
  heroku: circleci/[email protected]
jobs:
  build:
    docker:
      - image: circleci/ruby:2.6.5-stretch-node
    executor: ruby/default
    steps:
      - checkout
      - run: gem install bundler -v 2.1.4
      - run:
          name: Which bundler?
          command: bundle -v
      - ruby/bundle-install

  deploy:
    docker:
      - image: buildpack-deps:trusty
    executor: heroku/default
    steps:
      - checkout
      - run:
          name:Deploy master to Heroku
          command: |
            git push https://heroku:[email protected]/$HEROKU_APP_NAME.git master

workflows:
  version: 2.1
  build-deploy:
    jobs:

      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master

Now you have a CI/CD pipeline with Heroku.

4201a35bf1d8f48736248ac20ee8fe56.png

You can see deploy under build. This way of writing is just a beginner's edition, so I have not described the execution of the test by RSpec, but I would like you to keep in mind that you can also describe it.

Recommended Posts

How to check CircleCI code and automatically deploy to Heroku
How to deploy on heroku
How to connect Heroku and Sequel
How to deploy
Deploy to Heroku (for second and subsequent edits)
Steps to deploy to Heroku
How to deploy to Heroku from a local docker image
Run Rubocop and RSpec on CircleCI and deploy to ECS
[Linux] Easy commentary! How to check and change permissions
[Part 1] How to deploy Docker containers and static files with CircleCI + ECS + ECR + CloudFront
[AWS] How to check logs
Deploy Vapor Project to Heroku
How to write good code
How to check JSF version
How to specify character code and line feed code in JAXB
How to deploy a simple Java Servlet app on Heroku
How to set character code and line feed code in Eclipse
How to check the extension and size of uploaded files
How to check the database of apps deployed on Heroku
How to use StringBurrer and Arrays.toString.
How to deploy jQuery on Rails
How to deploy Laravel on CentOS 7
How to use EventBus3 and ThreadMode
How to deploy Bootstrap on Rails
How to call classes and methods
How to use equality and equality (how to use equals)
How to deploy an app that references a local jar to heroku
Deploy to EC2 with CircleCi + Capistrano
How to manage Java code automatically generated by jOOQ & Flyway sample
How to convert LocalDate and Timestamp
Deploy Rails on Docker to heroku
How to write easy-to-understand code [Summary 3]
I tested how to use Ruby's test / unit and rock-paper-scissors code.
[RSpec] How to write test code
How to deploy to AWS using NUXTJS official S3 and CloudFront? With docker-compose
AtCoder is called TLE and talks about how to write beautiful code
How to set different source / target versions for production code and test code
How to check Java installed on Mac
How to use OrientJS and OrientDB together
How to publish an application on Heroku
How to call Swift 5.3 code from Objective-C
[Java] How to output and write files!
How to set up and use kapt
Deploy to Heroku [Ruby on Rails] Beginner
How to find the tens and ones
[Easy] How to upgrade Ruby and bundler
How to use substring and substr methods
Note: [Docker] How to start and stop
How to write and explain Dockerfile, docker-compose
How to use @Builder and @NoArgsConstructor together
How to count UTF-8 code points fast
[AWS] How to automatically deploy a Web application created with Rails 6 to ECR / ECS using CircleCI ① Preparation [Container deployment]
heroku deploy
[Spring sample code included] How to create a form and how to get multiple records
How to deploy VS Code Remote Containers in a docker-compose project that includes both the API and the front app
Code that is difficult to debug and parse
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to output Excel and PDF using Excella
[Java] How to use FileReader class and BufferedReader class
How to check the logs in the Docker container
How to deploy a container on AWS Lambda