flowerinthenight / golang-monorepo

An example of a golang-based monorepo.
MIT License
381 stars 36 forks source link

Flexible CI #3

Open liemle3893 opened 4 years ago

liemle3893 commented 4 years ago

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.

flowerinthenight commented 4 years ago

Hi, thanks for this information. I'm in the process of adding support for GitHub Actions as well so let me combine these information so we can support the 3 CI's. By the way, what is this magic number 4b825dc642cb6eb9a060e54bf8d69288fbee4904? Is this documented somewhere?

liemle3893 commented 4 years ago

@flowerinthenight Here is the doc. https://git.wiki.kernel.org/index.php/Aliases

Try some things like:

$ printf '' | git hash-object -t tree --stdin

also work.

flowerinthenight commented 4 years ago

Nice. TIL something. Thank you.

Anyway, I just committed my changes to GitHub Actions. At the moment, the build allows the $COMMIT_RANGE variable to be overridable. You can try setting this variable in GitLab and build.sh will use that range instead.

flowerinthenight commented 4 years ago

If you have a GitLab yml file, you can do a PR, even though it's just wip, so I can add it and connect this repo to GitLab. That way, we can use your commit instead of just me copying your code. If you don't mind, that is.

liemle3893 commented 4 years ago

I've just created a PR: https://github.com/flowerinthenight/golang-monorepo/pull/4. Please take a look at it.

DragFAQ commented 2 years ago

Hi! Any updates? Did you resolve other "Need help" things? As I studied, this is the best monorepo template for golang.

flowerinthenight commented 2 years ago

No I haven't yet. No extra time at the moment.