DEV Community

Cover image for Trigger workflows of different repositories and exchange data
Akshay Rao
Akshay Rao

Posted on

Trigger workflows of different repositories and exchange data

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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 }}"}'
Enter fullscreen mode Exit fullscreen mode
  • Trigger the helm chart trigger workflow by pushing to main branch or manually
    Image 2

  • Once above build is success then automaically the workflow in the dev repo (with my-event) will be triggered.
    Image 3

  • Once above build is success then automaically the workflow in the pull.yaml in helm repo(my-pull event) will be triggered.
    Image 4

  • 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

Thank you.

Top comments (0)