Skip to content

Commit d8f3b04

Browse files
authored
Added github-dispatches (#21)
1 parent a12bf59 commit d8f3b04

7 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Test github-dispatches
2+
3+
on:
4+
push:
5+
paths:
6+
- .github/workflows/github-dispatches.yaml
7+
- github-dispatches/**
8+
pull_request:
9+
paths:
10+
- .github/workflows/github-dispatches.yaml
11+
- github-dispatches/**
12+
13+
jobs:
14+
github-dispatches:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v2
19+
20+
- name: github-dispatches
21+
uses: ./github-dispatches
22+
with:
23+
username_token: ${{ secrets.REPO_TOKEN }}
24+
repo_array: '[{"url":"redhat-cop/rego-policies","event_type":"Trigger","client_payload":{"from-action":"github-dispatches"}}]'

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This repository contains:
88

99
### Actions
1010
- [confbatstest](/confbatstest)
11+
- [github-dispatches](/github-dispatches)
1112
- [redhat-csp-download](/redhat-csp-download)
1213
- [s2i](/s2i)
1314

github-dispatches/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.2
2+
3+
LABEL version="1.0.0"
4+
LABEL repository="http://github.com/redhat-cop/github-actions"
5+
LABEL homepage="http://github.com/redhat-cop/github-actions/github-dispatches"
6+
LABEL maintainer="Red Hat CoP"
7+
LABEL "com.github.actions.name"="github-dispatches"
8+
LABEL "com.github.actions.description"="Triggers a GitHub CI dispatches event."
9+
LABEL "com.github.actions.branding.icon"="monitor"
10+
LABEL "com.github.actions.branding.color"="purple"
11+
12+
RUN microdnf install --assumeyes --nodocs curl && \
13+
microdnf clean all && \
14+
curl --version
15+
16+
RUN export JQ_VERSION=1.6 && \
17+
curl -L -o /tmp/jq-linux64 https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 && \
18+
chmod +x /tmp/jq-linux64 && \
19+
ln -s /tmp/jq-linux64 /usr/local/bin/jq && \
20+
jq --version
21+
22+
ADD entrypoint.sh /entrypoint.sh
23+
ENTRYPOINT ["/entrypoint.sh"]

github-dispatches/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
![Test github-dispatches](https://github.com/redhat-cop/github-actions/workflows/Test%20github-dispatches/badge.svg)
2+
3+
# github-dispatches GitHub Action
4+
5+
This action triggers a GitHub CI [dispatches event](https://blog.marcnuri.com/triggering-github-actions-across-different-repositories).
6+
7+
## Usage
8+
9+
```yaml
10+
- name: github-dispatches
11+
uses: ./github-dispatches
12+
with:
13+
username_token: ${{ secrets.REPO_TOKEN }}
14+
repo_array: '[{"url":"owner/repo","event_type":"Triggered","client_payload":{"customField":"customValue"}}]'
15+
```

github-dispatches/action.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: 'github-dispatches'
2+
description: 'Triggers a GitHub CI dispatches event.'
3+
author: 'Red Hat CoP'
4+
branding:
5+
icon: monitor
6+
color: purple
7+
inputs:
8+
repo_array:
9+
description: "JSON array of repos to trigger"
10+
required: true
11+
username_token:
12+
description: "Personal GitHub access token in the form of 'username:token' which has 'repo' access"
13+
required: true
14+
15+
runs:
16+
using: 'docker'
17+
image: 'Dockerfile'
18+
args:
19+
- ${{ inputs.repo_array }}
20+
- ${{ inputs.username_token }}

github-dispatches/entrypoint.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
exec_dispatches() {
6+
local REPOS="${1}"
7+
local USRNAME_TOKEN="${2}"
8+
9+
echo "${REPOS}" | jq -e "." >/dev/null 2>&1
10+
local jq_code=$?
11+
if [[ "${jq_code}" -ne 0 ]]; then
12+
echo "${jq_code} : Failed to parse 'repo_array' JSON. Failing"
13+
echo "${REPOS}"
14+
exit 1
15+
fi
16+
17+
if [[ -z "${USRNAME_TOKEN}" ]]; then
18+
echo "USRNAME_TOKEN is empty. Skipping."
19+
exit 0
20+
fi
21+
22+
for row in $(echo "${REPOS}" | jq -c ".[]"); do
23+
_jq() {
24+
echo "${row}" | jq -r -c "${1}"
25+
}
26+
27+
echo "Attempting to trigger: ${row}"
28+
29+
url=$(_jq ".url")
30+
data=$(_jq "{event_type: .event_type, client_payload: .client_payload}")
31+
32+
HTTP_STATUS=$(curl --silent --show-error --request POST "https://api.github.com/repos/${url}/dispatches" \
33+
-o /dev/null -w "%{http_code}" \
34+
--header "Accept: application/vnd.github.everest-preview+json" \
35+
--user "${USRNAME_TOKEN}" \
36+
--data "${data}")
37+
38+
if [[ "${HTTP_STATUS}" -ne 204 ]] ; then
39+
echo "Expected 204, got ${HTTP_STATUS} status code. Failng."
40+
exit 1
41+
fi
42+
43+
echo "Completed: ${HTTP_STATUS}"
44+
done
45+
}
46+
47+
exec_dispatches "${1}" "${2}"

redhat-csp-download/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![Test redhat-csp-download](https://github.com/redhat-cop/github-actions/workflows/Test%20redhat-csp-download/badge.svg)
2+
13
# redhat-csp-download GitHub Action
24

35
This action uses [redhat-csp-download](https://github.com/sabre1041/redhat-csp-download) to download resources from the Red Hat Customer Portal,

0 commit comments

Comments
 (0)