Skip to content
JackSparrow414
Go back

Integrating GitHub with CircleCI, with a Sample Configuration

Table of contents

Open Table of contents

Integrating GitHub with CircleCI

A Brief Explanation of CI (Continuous Integration)

CI stands for Continuous Integration. When changes are submitted, automated steps run before merging, such as code-style checks and unit tests. After a merge, other steps can run automatically, such as packaging and deployment to development or testing environments.

CircleCI’s official explanation of CI.

CI Tools

Common CI tools include Jenkins, CircleCI, and Travis CI. I will not examine their relative strengths and weaknesses here.

Using CircleCI

It is straightforward to use once you learn the syntax of config.yml.

Authorizing CircleCI to Access a GitHub Project

  1. On the CircleCI website, click Go to App and authorize your GitHub account. Go to App entry on the CircleCI website

  2. Click set up project to connect your project to CircleCI. Set Up Project button on the CircleCI Projects page

Writing config.yml

The official configuration reference provides the overall structure of config.yml, shown below. CircleCI configuration documentation showing the overall config.yml structure Using that structure, the sample file below illustrates the configuration.

The basic CI execution flow is:

Retrieve a workflow from workflows and check its execution conditions. If those conditions are met,

execute the jobs under the workflow.

Configuration under workflows is simple:

Each job has three main parts:

Common options in steps are:

version: 2.1
jobs: # a collection of steps
  # Name the job build. Runs not using Workflows must have a `build` job as the entry point.
  build:
    # Configure the working directory.
    working_directory: ~/repo
    # Run the steps with Docker.
    docker:
      - image: circleci/openjdk:8-jdk-stretch
      # A collection of executable commands: the steps to execute in this job.
    steps:
      # Check out source code to the working directory.
      - checkout

      - restore_cache: # restore the saved cache after the first run or if `pom.xml` has changed
          # Read about caching dependencies: https://circleci.com/docs/2.0/caching/
          key: v1-repo-{{ checksum "pom.xml" }}

      - run:
          name: get dependency
          # Get the project dependencies.
          command: mvn dependency:go-offline

      - save_cache: # saves the project dependencies
          paths:
            - ~/.m2
          key: v1-repo-{{ checksum "pom.xml" }}
      - run:
          name: build start
          command: echo "build start"

      - run:
          name: build project
          # Package the project here.
          command: mvn package
      # Command to run on success.
      - run:
          name: bulid success
          command: echo "build success"
          when: on_success
      # Command to run on failure.
      - run:
          name: build fail
          command: echo "build failur"
          when: on_fail
      # Save test results.
      - store_test_results: # uploads the test metadata from the `target/surefire-reports` directory so that it can show up in the CircleCI dashboard.
          # Upload test results for display in Test Summary: https://circleci.com/docs/2.0/collect-test-data/
          path: target/surefire-reports

      - store_artifacts: # store the uberjar as an artifact
          # Upload test summary for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
          path: target/griantBaby-0.0.1-SNAPSHOT.jar
        # See https://circleci.com/docs/2.0/deployment-integrations/ for deploy examples
workflows:
  version: 2
  # Define three workflows.

  # Trigger on each commit.
  #  commit-workflow:
  #    jobs:
  #      - build:

  # Run when someone forks the project and submits a PR. Use filters to define the matching branch; only accepts a regular expression.
  # This generally duplicates commit-workflow, so keep only this workflow here.
  fork-commit-workfolw:
    jobs:
      - build:
          filters:
            branches:
              only: /^[A-Za-z0-9].*/
  # Scheduled trigger, consisting of triggers and the jobs to run.
  scheduled-workfolw:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - build

Testing Whether the CircleCI Configuration Takes Effect

Modify code on the local dev branch, stage and commit it, push to the remote, and create a PR against master. CircleCI automatically starts checks when the PR is created, as shown below. GitHub PR page showing passing CircleCI checks

The CircleCI console also shows this checking workflow. CircleCI Pipelines page showing project build statuses Click a workflow to inspect its details. Here you can see that it really runs in a Docker environment. CircleCI job details showing the Docker environment and execution steps

The names we defined for run steps appear here. Click a step to see its details.

When a step fails, you can also inspect the failure information: Maven error logs in a failed CircleCI build step

You can also see that after the job fails, the failure command we configured really executes. CircleCI running the configured failure handler after a build fails

Notes

  1. I recommend using the example above as a starting point alongside the official documentation. If you still do not know how to write the configuration, the documentation also offers many examples. Configuration examples listed in the CircleCI documentation

  2. When developing an open-source GitHub project locally, use a separate branch instead of developing on master. Even if nobody else uses it yet, the workflow should still be sound. If you are the only developer, create a dev branch and submit each change through a PR. Merge after CI checks pass, and develop good workflow habits.

  3. For deeper use of CircleCI, there is no need to search online first: the official documentation is the best source of answers. Documentation. Read it carefully, and answers are usually easy to find.

A Final Thought

I wrote this article simply to record my recent experience connecting a GitHub project to CI. I also hope more people will learn about and participate in open source.

I have an open-source project on GitHub that currently needs collaborators. If interested, see the project.

I look forward to working with you :smile::smile::smile:


Share this post:

Previous Post
Using ShardingSphere Database Middleware: ShardingProxy (Part 1), Data Sharding and Read/Write Splitting
Next Post
Getting Started with Redis (Part 2): Basic Data Types and Commands

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.