Skip to content

Remote Trigger Workflow #19

Remote Trigger Workflow

Remote Trigger Workflow #19

name: Remote Trigger Workflow
on:
workflow_dispatch:
inputs:
message:
description: 'Message from the remote repo'
required: true
default: 'Hello from remote'
data:
description: 'Additional data'
required: false
jobs:
run-remote-triggered-job:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Create draft branch
run: |
TIMESTAMP=$(date +%Y%m%d%H%M%S)
echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV
git checkout -b build-draft-$TIMESTAMP
git push origin build-draft-$TIMESTAMP
- name: Trigger test-all-packages workflow
id: trigger-test-all-packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
response=$(curl -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/actions/workflows/test-all-packages.yml/dispatches \
-d '{"ref":"refs/heads/build-draft-${{ env.TIMESTAMP }}"}' \
-w "%{http_code}" \
-o /dev/null)
if [ "$response" = "204" ]; then
echo "Workflow triggered successfully"
exit 0
else
echo "Failed to trigger workflow"
exit 1
fi
- name: Wait for test-all-packages workflow
id: wait-for-test-all-packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
max_attempts=60
attempt=0
while [ $attempt -lt $max_attempts ]; do
sleep 60 # Wait for 1 minute before checking again
# Get the latest run of test-all-packages workflow
run_info=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/test-all-packages.yml/runs?branch=build-draft-${{ env.TIMESTAMP }}&status=completed&per_page=1")
status=$(echo $run_info | jq -r '.workflow_runs[0].conclusion')
if [ "$status" = "success" ]; then
echo "test-all-packages workflow completed successfully"
echo "success=true" >> $GITHUB_OUTPUT
exit 0
elif [ "$status" = "failure" ] || [ "$status" = "cancelled" ]; then
echo "test-all-packages workflow failed or was cancelled"
echo "success=false" >> $GITHUB_OUTPUT
exit 1
fi
attempt=$((attempt+1))
echo "Waiting for test-all-packages workflow to complete (attempt $attempt of $max_attempts)"
done
echo "Timeout waiting for test-all-packages workflow to complete"
echo "success=false" >> $GITHUB_OUTPUT
exit 1
# - name: Trigger integration workflow
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: |
# curl -X POST \
# -H "Authorization: token $GITHUB_TOKEN" \
# -H "Accept: application/vnd.github.v3+json" \
# https://api.github.com/repos/${{ github.repository }}/actions/workflows/integration.yml/dispatches \
# -d '{"ref":"${{ github.ref }}"}'
- name: Create release branch
if: steps.wait-for-test-all-packages.outputs.success == 'true'
run: |
git checkout -b build-release-${{ env.TIMESTAMP }}
git push origin build-release-${{ env.TIMESTAMP }}
# delete draft branch
- name: Run triggered action
run: |
echo "Workflow triggered remotely!"
echo "Message: ${{ github.event.inputs.message }}"
echo "Additional data: ${{ github.event.inputs.data }}"