Trigger workflows of different repositories and exchange data
Tags: githubactions, github
Published: 2024-03-07 15:04:45

Hi, i am Akshay rao
This blog shows the demo of triggering a workflow and exchanging data between two workflow.
I have taken two repo:-
1. chat-app-helm-chart repo(helm-chart repo)
2. real-time-chat-app repo(dev repo)

- helm-chart repo -> it will trigger the workflow in dev repo.
- dev repo -> the workflow will build a image and push it to the docker hub and it will pass the tag which has been attached to the image, in this case i have take the timestamp as the tag.
- helm chart repo -> another workflow will be trigger form the dev repo to pul the image.

![Image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qjomgqbk69zymngu766.png)

**Lets start**
- Create a personal Access token(PAT) with admin, repo read write and workflow permission.
- Save this PAT as a secret in the repository as i hvae saved as `TOKEN_TO_TRIGGER`
- In the helm chart repo make a file trigger.yaml and pull.yaml in ./github/workflows
trigger.yaml
```
name: deploy the app

on:
  push:
    branches:
      main
jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - name: trigger dev repo to build image
        uses: peter-evans/repository-dispatch@v1
        with:
         token: ${{ secrets.TOKEN_TO_TRIGGER }}
         repository: Devops-MLOps/real-time-chat-application
         event-type: my-event
```
pull.yaml
```
name: pull the image

on:
  repository_dispatch:
    types: [my-pull]
jobs:
  run_if_failure:
    if: ${{ !github.event.client_payload.passed }}
    runs-on: ubuntu-latest
    steps: 
      - env:
          TAG: ${{ github.event.client_payload.tag }}
        run: echo $TAG && docker pull aksrao1998/chat-app-be:$TAG
```
- In dev repo ./github/workflow create trigger_build.yaml
```
name: build and push image
on:
  repository_dispatch:
    types: [my-event]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: checkout the repository
        uses: actions/checkout@v2
      - name: docker login
        uses: docker/login-action@v3
        with:
          username: ${{secrets.DOCKER_USER}}
          password: ${{secrets.DOCKER_PASS}}
      - name: timestamp for tag
        id: timestamp
        run: echo "::set-output name=timestamp::$(date +'%Y%m%d%H%M%S')"
      - name: build and push the frontend image 
        uses: docker/build-push-action@v5
        with:
          context: ./client
          file: ./client/Dockerfile
          push : true
          tags: aksrao1998/chat-app-fe:${{ steps.timestamp.outputs.timestamp }}
      - name:  build and push the backend image 
        uses: docker/build-push-action@v5
        with:
          context: ./server
          file: ./server/Dockerfile
          push : true
          tags: aksrao1998/chat-app-be:${{ steps.timestamp.outputs.timestamp }}
      - name: trigger helm repo for pull
        uses: peter-evans/repository-dispatch@v1
        with:
         token: ${{ secrets.TOKEN_TO_TRIGGER }}
         repository: Devops-MLOps/chat-app-helm-chart
         event-type: my-pull
         client-payload: '{"passed": false, "tag": "${{ steps.timestamp.outputs.timestamp }}"}'
```
- Trigger the helm chart trigger workflow by pushing to main branch or manually
![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2lanfv516dgla2zbntx.png)

- Once above build is success then automaically the workflow in the dev repo (with my-event) will be triggered.
![Image 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkckuh9pmj4rlnmz7704.png)

- Once above build is success then automaically the workflow in the pull.yaml in helm repo(my-pull event) will be triggered.
![Image 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3kauf2xlnw25js972p0.png)
- We can exchange the data here tag of a image is sent from dev repo workflow to helm chart repo.
tag in dev repo -> 20240307130426
tag received by helm chart ->20240307130426

I hope that this blog will help you by any means.
i have attached my repos for reference
- https://github.com/Devops-MLOps/chat-app-helm-chart
- https://github.com/Devops-MLOps/real-time-chat-application

Thank you.