golang-monorepo icon indicating copy to clipboard operation
golang-monorepo copied to clipboard

Flexible CI

Open liemle3893 opened this issue 3 years ago • 7 comments

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.

liemle3893 avatar Aug 28 '20 19:08 liemle3893

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?

flowerinthenight avatar Aug 29 '20 02:08 flowerinthenight

@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.

liemle3893 avatar Aug 29 '20 05:08 liemle3893

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 avatar Aug 29 '20 05:08 flowerinthenight

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.

flowerinthenight avatar Aug 29 '20 06:08 flowerinthenight

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

liemle3893 avatar Aug 29 '20 18:08 liemle3893

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

DragFAQ avatar Nov 22 '21 09:11 DragFAQ

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

flowerinthenight avatar Nov 30 '21 16:11 flowerinthenight