Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible CI #3

Open
liemle3893 opened this issue Aug 28, 2020 · 7 comments
Open

Flexible CI #3

liemle3893 opened this issue Aug 28, 2020 · 7 comments

Comments

@liemle3893
Copy link

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
Copy link
Owner

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
Copy link
Author

@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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

I've just created a PR: #4. Please take a look at it.

@DragFAQ
Copy link

DragFAQ commented Nov 22, 2021

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

@flowerinthenight
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants