Giter Club home page Giter Club logo

golang-monorepo's Introduction

GitHub Actions CircleCI
main CircleCI

Overview

This is an example of a golang-based monorepo. It has the following features:

  • Only build the services or cmds that are modified in a commit;
  • Build all services and/or cmds that are affected by changes in common codes (i.e. pkg);
  • Build all services and/or cmds that are affected by changes in vendor codes.

For now, CircleCI 2.1 and GitHub Actions are supported. But since it uses bash scripts and Makefiles, it should be fairly straightforward to port to TravisCI or AppVeyor, etc.

At the moment, CI is setup with GO111MODULE=on and GOFLAGS=-mod=vendor environment variables enabled during build. See sample dockerfile for more details.

How does it work

During CI builds, build.sh iterates the updated files within the commit range (CIRCLE_COMPARE_URL environment variable in CircleCI) or the modified files within a single commit (when the value is not a valid range), excluding hidden files, pkg, and vendor folders. It will then try to walk up the directory path until it can find a Makefile (excluding root Makefile). Once found, the root Makefile will include that Makefile and call the custom rule as target, thus, initiating the build.

When the changes belong to either pkg or vendor, the script will then try to determine the services (and cmds) that have dependencies using the go list command. All dependent services will then be built using the same process described above.

You can override the COMMIT_RANGE environment variable for your own CI. If this is set, build.sh will use its value. You also want to set CIRCLE_SHA1 to your commit SHA (CIRCLE_SHA1 is CircleCI-specific). Example for GitHub Actions is here. Something like:

# If your commit range is correct:
COMMIT_RANGE: aaaaa..bbbbb
CIRCLE_SHA1: aaaaa

# If no valid commit range:
COMMIT_RANGE: <your_commit_sha>
CIRCLE_SHA1: <your_commit_sha>

Directory structure

  • services/ - Basically, long running services.
  • cmd/ - CLI-based tools that are not long running.
  • pkg/ - Shared codes, or libraries common across the repo.
  • vendor/ - Third party codes from different vendors.

Although we have this structure, there is no limitation into where should you put your services/cmds. Any subdirectory structure is fine as long as a Makefile is provided.

How to add a service/cmd

A reference template named samplesvc is provided. Basically, these are the things that you need to do:

  • Create a new directory for your service under services/ or tool under cmd/. You may copy the samplesvc contents to your new directory.
  • Update the dockerfile inside your new service directory. Note that during build, this dockerfile is copied to the root directory (to be able to access pkg and vendor directories).
  • Update the Makefile with your own values. You need to at least update the MODULE variable with your service name. The only required rule is the custom part so you may need to change that as well (i.e. name of the dockerfile used in docker build).
  • [Optional] Update the deploy.sh script for your deployment needs.

golang-monorepo's People

Contributors

flowerinthenight avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

golang-monorepo's Issues

Flexible CI

Let's user edit ci file to correct their pipeline.

eg:

# .circleci/config.yaml
version: 2.1
jobs:
  build:
    working_directory: /go/src/github.com/liemle3893/golang-monorepo/
    docker:
    - image: golang:1.15
    steps:
    - checkout
    - setup_remote_docker:
        version: 18.06.0-ce
    - run:
        name: build
        shell: /bin/bash
        environment: 
          CI_COMMIT_BEFORE_SHA: << pipeline.git.base_revision >>
          CI_COMMIT_SHA: <<pipeline.git.revision>>
          COMMIT_ID: <<pipeline.git.revision>>
        command: |
          chmod +x ./build.sh
          ./build.sh

Where:

# Extract commit range (or single commit).
COMMIT_RANGE_SEP="..."
if [[ "x$CI_COMMIT_BEFORE_SHA" == "x" ]]; then
  # CI_COMMIT_BEFORE_SHA defined 
  GIT_COMMIT_NUMBER=`git log --oneline | wc -l | bc`
  if [[ $GIT_COMMIT_NUMBER == "1" ]]; then 
    # 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is magic id which is always exists
    CI_COMMIT_BEFORE_SHA="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
    COMMIT_RANGE_SEP=" "
  else
    CI_COMMIT_BEFORE_SHA=`git rev-parse HEAD~1`
  fi
fi
if [[ "x$CI_COMMIT_SHA" == "x" ]]; then
  # CI_COMMIT_SHA is not defined
  CI_COMMIT_SHA=`git rev-parse HEAD`
fi

COMMIT_RANGE="${CI_COMMIT_BEFORE_SHA}${COMMIT_RANGE_SEP}${CI_COMMIT_SHA}"

This way, I can flexible using Gitlab or Circle CI as my choice.

Note: This also solve the problem when repo only have 1 commit.

Reference local package

Hey @flowerinthenight

I'm wondering about how you reference local packages.

I see you reference pkg/utils at samplesvc/main.go:7 directly from github. Done this why, pkg/util will be downloaded from github using the new go modules behaviour - is my understanding there correct?

Say I have a case where I'd like to make some experimental changes to pkg/utils - would I have to push those experimental changes up to github before they become available to samplesvc? Or will sampelsvc just reference it locally?

Thanks for putting this monorepo together :) And thanks for your help!

Handling dependency change if vendor is .gitignored?

Some teams don't want to include the vendor directory in git and they prefer the CI system to download the dependencies during build time with caching (e.g in CircleCI).

What would be the best way to check if the git diff of $COMMIT_RANGE for Gopkg.lock (dep) or go.sum (go mod) modules should trigger a build for cmds and services?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.