bitsbeats / drone-tree-config

Drone helper for mono repositories.
Apache License 2.0
103 stars 24 forks source link

v0.3.4 fails to trigger builds on master branch in monorepo #17

Closed bartimaeus closed 3 years ago

bartimaeus commented 4 years ago

I love this plugin! Everything was running great on ECS with two service definitions:

drone-ci

drone-runner

Yesterday all of my github webhooks on my master branch were failing to trigger builds. The logs only show a failed response of did not find drone.yml. All other branches were building fine.

Today, I specified the previous version bitsbeats/drone-tree-config:0.3.3 and everything is working again.

I would like to be able to troubleshoot this locally and see what's exactly going wrong.

foosinn commented 4 years ago

Hey,

sorry for the late reply. Can you add some logging on your own? Especialy around the chanes that came with the last Version:

https://github.com/bitsbeats/drone-tree-config/commit/3e82ec0e352783e7e6189c25f8c90dde89b57889#diff-9a2cdc7907fa6a4bfc94da76b035c282R52

Namely repoBranch, commitBranch, before and after.

Im unsure why this is causing issues.

Thanks!

bartimaeus commented 4 years ago

Thanks for the response @foosinn! I tried running everything locally with the latest build and it appears to build the master branch; however, now it is always building the same sub-project and referencing the same commit every time.

I am new to go, but tried looking through the source code and found that drone-tree-config calls the github api asking for the difference between two commits.

Below is an example output I get from pushing a commit on a master branch: GitHub Webhook -> GitHub Diff -> drone-tree-config log.

GitHub Webhook

{
  ...
  "compare": 
  "https://github.com/org/monorepo/compare/2974355b58a8...1132d604c2e0",
    "commits": [
      {
        "id": "fb8c7401619411a8e2ab5faffccd2ba6ce8a0e88",
        "tree_id": "f1271afa1d56c36f6936959f8f78ea1d755041bd",
        "distinct": false,
        "message": "Ignore local .env.production file",
        "timestamp": "2020-03-09T11:40:30-06:00",
        "url": "https://github.com/org/monorepo/commit/fb8c7401619411a8e2ab5faffccd2ba6ce8a0e88",
        "author": {
          "name": "Eric Shelley",
          "email": "eric@webdesignbakery.com",
          "username": "bartimaeus"
        },
        "committer": {
          "name": "Eric Shelley",
          "email": "eric@webdesignbakery.com",
          "username": "bartimaeus"
        },
        "added": [],
        "removed": [],
        "modified": [
        "packages/manage/.gitignore"
        ]
      },
      {
        "id": "1132d604c2e054125389943105c3a333aa6b9426",
        "tree_id": "60745b1959163b42237d0fca04247a71b76b6fc8",
        "distinct": true,
        "message": "Merge branch 'develop'\n\n* develop:\n  Ignore local .env.production file",
        "timestamp": "2020-03-09T11:40:57-06:00",
        "url": "https://github.com/org/monorepo/commit/1132d604c2e054125389943105c3a333aa6b9426",
        "author": {
          "name": "Eric Shelley",
          "email": "eric@webdesignbakery.com",
          "username": "bartimaeus"
        },
        "committer": {
          "name": "Eric Shelley",
          "email": "eric@webdesignbakery.com",
          "username": "bartimaeus"
        },
        "added": [],
        "removed": [],
        "modified": [
          "packages/manage/.gitignore"
        ]
      }
    ],
    ...
}

GitHub Diff API Response

{
    ...
    "files": [
        {
            "sha": "2538b99d3538b702c705cc6b9b24f99cd7dd070a",
            "filename": "packages/manage/.gitignore",
            "status": "modified",
            "additions": 1,
            "deletions": 0,
            "changes": 1,
            ...
            "patch": "@@ -16,6 +16,7 @@\n .env.local\n .env.development.local\n .env.test.local\n+.env.production\n .env.production.local\n \n npm-debug.log*"
        }
    ]
}

drone-tree-config log

drone_tree_config            | time="2020-03-09T19:10:39Z" level=debug msg="3a511a34-c67b-4603-a766-33b73e89eb60 changed files: \n  packages/app/src/referrals/referrals/DetailsDrawer.js"

No matter what I push to the master branch, it appears to always try to build packages/app because it thinks I changed DetailsDrawer.js. I was hoping that a fresh github oauth app and running drone-tree-config locally would produce a different result, but it did not.

Could there be a git cache somewhere?

foosinn commented 4 years ago

Sorry for the long delay. I was out of office last week.

Is the default branch configured in drone the master branch?

bartimaeus commented 4 years ago

No worries @foosinn. I appreciate your help. I have two pipelines in the file. The first one works fine and is triggered on the develop branch. The second pipeline is triggered on the master branch

Here is my .drone.yml

---
kind: pipeline
name: manage [staging]

steps:
  - name: restore npm cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /staging-manage
    settings:
      restore: true
      mount:
        - ./packages/manage/node_modules

  - name: test & build
    image: node
    environment:
      GENERATE_SOURCEMAP: true
      REACT_APP_API_ENDPOINT:
        from_secret: staging_main_api_url
    commands:
      - cd packages/manage
      - yarn install
      - yarn test
      - yarn build

  - name: sync
    image: mesosphere/aws-cli
    environment:
      AWS_ACCESS_KEY_ID:
        from_secret: staging_aws_access_key_id
      AWS_SECRET_ACCESS_KEY:
        from_secret: staging_aws_secret_access_key
      S3_BUCKET:
        from_secret: staging_manage_bucket_name
      DISTRIBUTION_ID:
        from_secret: staging_manage_cloudfront_distribution_id
    commands:
      - aws s3 sync packages/manage/build s3://$S3_BUCKET --cache-control "max-age=14400,public"
      - aws s3 cp s3://$S3_BUCKET/service-worker.js s3://$S3_BUCKET/service-worker.js --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type application/javascript
      - aws s3 cp s3://$S3_BUCKET/index.html s3://$S3_BUCKET/index.html --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type text/html
      - aws cloudfront create-invalidation --distribution-id $${DISTRIBUTION_ID} --paths '/*'

  - name: rebuild npm cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /staging-manage
    settings:
      rebuild: true
      mount:
        - ./packages/manage/node_modules

  - name: slack
    image: plugins/slack
    settings:
      webhook:
        from_secret: slack_webhook
      channel: status
      template: >
        {{#success build.status}}
          *success* <{{build.link}}|manage ({{build.branch}})> by {{build.author}}
        {{else}}
          *failure* <{{build.link}}|manage ({{build.branch}})> by {{build.author}}
        {{/success}}
    when:
      status: [success, failure]

volumes:
  - name: cache
    host:
      path: /mnt/efs/drone/cache

trigger:
  branch:
    - develop

---
kind: pipeline
name: manage [production]

steps:
  - name: restore npm cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /production-manage
    settings:
      restore: true
      mount:
        - ./packages/manage/node_modules

  - name: test & build
    image: node
    environment:
      GENERATE_SOURCEMAP: true
      REACT_APP_API_ENDPOINT:
        from_secret: production_main_api_url
    commands:
      - cd packages/manage
      - yarn install
      - yarn test
      - yarn build
  - name: sync
    image: mesosphere/aws-cli
    environment:
      AWS_ACCESS_KEY_ID:
        from_secret: production_aws_access_key_id
      AWS_SECRET_ACCESS_KEY:
        from_secret: production_aws_secret_access_key
      S3_BUCKET:
        from_secret: production_manage_bucket_name
      DISTRIBUTION_ID:
        from_secret: production_manage_cloudfront_distribution_id
    commands:
      - aws s3 sync packages/manage/build s3://$S3_BUCKET --cache-control "max-age=14400,public"
      - aws s3 cp s3://$S3_BUCKET/service-worker.js s3://$S3_BUCKET/service-worker.js --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type application/javascript
      - aws s3 cp s3://$S3_BUCKET/index.html s3://$S3_BUCKET/index.html --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type text/html
      - aws cloudfront create-invalidation --distribution-id $${DISTRIBUTION_ID} --paths '/*'

  - name: rebuild npm cache
    image: drillster/drone-volume-cache
    volumes:
      - name: cache
        path: /production-manage
    settings:
      rebuild: true
      mount:
        - ./packages/manage/node_modules

  - name: slack
    image: plugins/slack
    settings:
      webhook:
        from_secret: slack_webhook
      channel: status
      template: >
        {{#success build.status}}
          *success* <{{build.link}}|manage ({{build.branch}})> by {{build.author}}
        {{else}}
          *failure* <{{build.link}}|manage ({{build.branch}})> by {{build.author}}
        {{/success}}
    when:
      status: [success, failure]

volumes:
  - name: cache
    host:
      path: /mnt/efs/drone/cache

trigger:
  branch:
    - master
foosinn commented 4 years ago

Hey, can you please validate with the latest release? (v0.3.6)

bartimaeus commented 4 years ago

Hi @foosinn, I just tried with v0.3.6. Same error. It recognized the commit I pushed, but started processing an old commit in a different directory. I wish I knew go better.

bartimaeus commented 4 years ago

@foosinn, I believe I found the issue. My repos are setup so that develop is the main branch and automatically deploys to staging. The master branch is the branch that will automatically deploy to production.

Because my repo's default branch is set to develop, this line will always set before to develop when commits are made to the master branch.

foosinn commented 3 years ago

Can you verify using v0.4.1?