Skip to content

Commit

Permalink
Fix infinite dispatch event (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
eedorenko committed Feb 18, 2022
1 parent 848e164 commit e5d6eee
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: CD
on:
workflow_run:
workflows: ["CI"]
branches: [main, eedorenko/fix-rate-limit]
branches: [main]
types:
- completed

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
push:
branches:
- main
- eedorenko/fix-rate-limit


jobs:
Expand Down
74 changes: 37 additions & 37 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
name: Publish

on:
repository_dispatch:
types: [sync-success]
# workflow_run:
# workflows: ["CI"]
# branches: [main]
# types:
# - completed
# repository_dispatch:
# types: [sync-success]
workflow_run:
workflows: ["CI"]
branches: [main]
types:
- completed

jobs:
update_helm_chart:
Expand All @@ -18,33 +18,33 @@ jobs:
steps:
- name: Checkout
uses: actions/[email protected]
# - name: Download Image Tags
# uses: dawidd6/action-download-artifact@v2
# with:
# name: image_tags
# workflow: ci.yml
# # run_id: ${{ github.event.client_payload.runid }}
# run_id: ${{ github.event.workflow_run.id }}
# path: ${{ github.workspace }}
# - name: Download Manifests
# uses: dawidd6/action-download-artifact@v2
# with:
# name: manifests
# workflow: ci.yml
# # run_id: ${{ github.event.client_payload.runid }}
# run_id: ${{ github.event.workflow_run.id }}
# path: ${{ github.workspace }}
# - name: Read Image Tags
# run: |
# echo "IMAGE_TAG=$(cat ${{ github.workspace }}/IMAGE_TAG)" >> $GITHUB_ENV
# - name: Generate Manifests
# run: |
# .github/workflows/utils/generate-manifests.sh ${{ github.workspace }}/manifests gen_manifests hld_only
# env:
# APP_BUILD_VERSION: ${{ env.IMAGE_TAG }}
# ORCHESTRATOR_PAT: ${{ secrets.ORCHESTRATOR_PAT }}
# - name: Publish Helm Chart
# run: |
# .github/workflows/utils/publish_helm_chart.sh gen_manifests/hld/helm ${{ secrets.HELM_CHARTS_REPO_NAME }} ${{ secrets.HELM_CHARTS_URL }}
# env:
# TOKEN: ${{ secrets.HELM_CHARTS_PAT }}
- name: Download Image Tags
uses: dawidd6/action-download-artifact@v2
with:
name: image_tags
workflow: ci.yml
# run_id: ${{ github.event.client_payload.runid }}
run_id: ${{ github.event.workflow_run.id }}
path: ${{ github.workspace }}
- name: Download Manifests
uses: dawidd6/action-download-artifact@v2
with:
name: manifests
workflow: ci.yml
# run_id: ${{ github.event.client_payload.runid }}
run_id: ${{ github.event.workflow_run.id }}
path: ${{ github.workspace }}
- name: Read Image Tags
run: |
echo "IMAGE_TAG=$(cat ${{ github.workspace }}/IMAGE_TAG)" >> $GITHUB_ENV
- name: Generate Manifests
run: |
.github/workflows/utils/generate-manifests.sh ${{ github.workspace }}/manifests gen_manifests hld_only
env:
APP_BUILD_VERSION: ${{ env.IMAGE_TAG }}
ORCHESTRATOR_PAT: ${{ secrets.ORCHESTRATOR_PAT }}
- name: Publish Helm Chart
run: |
.github/workflows/utils/publish_helm_chart.sh gen_manifests/hld/helm ${{ secrets.HELM_CHARTS_REPO_NAME }} ${{ secrets.HELM_CHARTS_URL }}
env:
TOKEN: ${{ secrets.HELM_CHARTS_PAT }}
4 changes: 2 additions & 2 deletions internal/setup/flux/sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ metadata:
spec:
interval: 30s
ref:
branch: dev
branch: main
timeout: 20s
url: https://github.com/kaizentm/gitops-manifests
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: gitops-connector-dev
Expand Down
9 changes: 5 additions & 4 deletions src/gitops_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def __init__(self):

def process_gitops_phase(self, phase_data, req_time):
if self._gitops_operator.is_supported_message(phase_data):
self._queue_commit_statuses(phase_data, req_time)
self._notify_orchestrator(phase_data)
commit_id = self._gitops_operator.get_commit_id(phase_data)
if not self._git_repository.is_commit_finished(commit_id):
self._queue_commit_statuses(phase_data, req_time)
self._notify_orchestrator(phase_data, commit_id)
else:
logging.debug(f'Message is not supported: {phase_data}')

Expand All @@ -36,10 +38,9 @@ def _queue_commit_statuses(self, phase_data, req_time):
for commit_status in commit_statuses:
self._global_message_queue.put(item=(req_time, commit_status))

def _notify_orchestrator(self, phase_data):
def _notify_orchestrator(self, phase_data, commit_id):
is_finished, is_successful = self._gitops_operator.is_finished(phase_data)
if is_finished:
commit_id = self._gitops_operator.get_commit_id(phase_data)
self._cicd_orchestrator.notify_on_deployment_completion(commit_id, is_successful)

# Entrypoint for the periodic task to search for abandoned PRs linked to
Expand Down
1 change: 1 addition & 0 deletions src/orchestrators/github_cicd_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def _send_repo_dispatch_event(self, commmit_id, run_id):
url = f'{self.rest_api_url}/{self.gitops_repo_name}/dispatches'
event_type = 'sync-success'
data = {'event_type': event_type, 'client_payload': {'sha': commmit_id, 'runid': run_id}}
logging.info(f'Dispatch event: url {url}; data {data}')
response = requests.post(url=url, headers=self.headers, json=data)
# Throw appropriate exception if request failed
response.raise_for_status()
3 changes: 3 additions & 0 deletions src/repositories/azdo_git_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,6 @@ def get_pr_num(self, commit_id) -> str:
merged_pr_index = comment.index(MERGED_PR)
pr_num = comment[merged_pr_index + len(MERGED_PR): comment.index(":", merged_pr_index)]
return pr_num

def is_commit_finished(self, commit_id):
return False
4 changes: 4 additions & 0 deletions src/repositories/git_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ def get_prs(self, pr_status):
@abstractmethod
def get_commit_message(self, commit_id):
pass

@abstractmethod
def is_commit_finished(self, commit_id):
pass
17 changes: 17 additions & 0 deletions src/repositories/github_git_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ def post_commit_status(self, commit_status):
# Throw appropriate exception if request failed
response.raise_for_status()

def is_commit_finished(self, commit_id):
url = f'{self.rest_api_url}/{self.gitops_repo_name}/commits/{commit_id}/status'

response = requests.get(url=url, headers=self.headers)
# Throw appropriate exception if request failed
response.raise_for_status()

responseJSON = response.json()

# logging.debug(f'Url {url}: Headers {self.headers}: Response {responseJSON}')

state = responseJSON['state']

logging.info(f'Commit {commit_id}: {state}')

return state == 'failure' or state == 'success'

def _map_to_github_state(self, reason):
state_map = {
"Suspended": "error",
Expand Down

0 comments on commit e5d6eee

Please sign in to comment.