-
Notifications
You must be signed in to change notification settings - Fork 1
feat(actions/posthog): a reusable action for sending usage metrics to PostHog #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+101
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d61582
feat: a reusable action for sending usage metrics to PostHog
xamebax 643c7ec
Splits sending event into smaller steps, renames event
xamebax 2720ea8
Limits building output just to properties, sends event using external…
xamebax 38f8a95
Apply suggestions from code review
xamebax 6b98300
fix indentation
xamebax 62e6030
adds per job duration metrics to posthog export
xamebax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| name: PostHog Capture | ||
| description: Send a workflow analytics event to PostHog | ||
|
|
||
| inputs: | ||
| api_key: | ||
| description: "PostHog project API key" | ||
| required: true | ||
| gha_repository: | ||
| description: "GitHub repository where the reusable workflow lives, in the format 'owner/repo' (e.g. 'entur/gha-helm')" | ||
| required: true | ||
| workflow_inputs: | ||
| description: "Workflow inputs as a JSON string (e.g. toJSON(inputs))" | ||
| required: false | ||
| default: "{}" | ||
| workflow_name: | ||
| description: "Name of the workflow. Used as the event name in PostHog (lint, deploy, etc.)" | ||
| required: true | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Resolve action version | ||
| id: workflow_version | ||
| env: | ||
| GHA_REPOSITORY: ${{ inputs.gha_repository }} | ||
| shell: bash | ||
| run: | | ||
| set -o pipefail | ||
|
|
||
| # Resolve version by finding this reusable workflow's entry in the caller's run. | ||
| # (GITHUB_WORKFLOW_REF is the caller's ref, not the reusable workflow's ref) | ||
| RUN_DATA=$(curl -sf \ | ||
| -H "Authorization: Bearer $GITHUB_TOKEN" \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" || echo "{}") | ||
|
|
||
| GHA_SHA=$(echo "$RUN_DATA" | jq -r --arg repo "$GHA_REPOSITORY" \ | ||
| '(.referenced_workflows // [])[] | select(.path | startswith($repo)) | .sha' | head -1) | ||
| GHA_REF=$(echo "$RUN_DATA" | jq -r '(.referenced_workflows // [])[] | select(.path | startswith($GHA_REPOSITORY)) | .ref // ""' | head -1) | ||
|
|
||
| WORKFLOW_VERSION="" | ||
| if [ -n "$GHA_REF" ]; then | ||
| WORKFLOW_VERSION="${GHA_REF}" | ||
| elif [ -n "$GHA_SHA" ]; then | ||
| WORKFLOW_VERSION="${GHA_SHA:0:7}" | ||
| fi | ||
|
|
||
| echo "Workflow version: $WORKFLOW_VERSION" | ||
| echo "workflow_version=${WORKFLOW_VERSION}" >> "$GITHUB_OUTPUT" | ||
| - name: Build properties | ||
| id: build_properties | ||
| shell: bash | ||
| env: | ||
| GITHUB_TOKEN: ${{ github.token }} | ||
| GHA_REPOSITORY: ${{ inputs.gha_repository }} | ||
| WORKFLOW_INPUTS: ${{ inputs.workflow_inputs }} | ||
| WORKFLOW_NAME: ${{ inputs.workflow_name }} | ||
| WORKFLOW_VERSION: ${{ steps.workflow_version.outputs.workflow_version }} | ||
| run: | | ||
| set -o pipefail | ||
|
|
||
| # Strip inputs whose key names suggest sensitive content | ||
| SAFE_INPUTS=$(echo "$WORKFLOW_INPUTS" | jq -c ' | ||
| with_entries( | ||
| select( | ||
| (.key | test("token|secret|key|password|credential|auth"; "i")) | not | ||
| ) | ||
| ) | ||
| ') | ||
|
|
||
| properties=$(jq -n \ | ||
| --arg event "$GITHUB_EVENT_NAME" \ | ||
| --argjson inputs "$SAFE_INPUTS" \ | ||
| --arg name "$WORKFLOW_NAME" \ | ||
| --arg repo "$GHA_REPOSITORY" \ | ||
| --arg run_id "$GITHUB_RUN_ID" \ | ||
| --arg version "$WORKFLOW_VERSION" \ | ||
| '{ | ||
| event_name: $event, | ||
| gha_repository: $repo, | ||
| run_id: $run_id, | ||
| workflow_name: $name, | ||
| workflow_version: $version | ||
| } | ||
| + ($inputs | with_entries(.key = ("input_" + .key)))' | ||
| ) | ||
| echo "properties=$(echo "$properties" | jq -c .)" >> "$GITHUB_OUTPUT" | ||
| - name: Send event to PostHog | ||
| uses: PostHog/posthog-github-action@58dea254b598fb5d469c0699c98af8288a7f7650 # v1.2.0 | ||
| continue-on-error: true | ||
| with: | ||
| capture-job-durations: 'true' | ||
| capture-run-duration: 'true' | ||
| event: ${{ inputs.workflow_name }} | ||
| github-token: ${{ github.token }} | ||
| posthog-api-host: https://eu.i.posthog.com | ||
| posthog-token: ${{ inputs.api_key }} | ||
| properties: ${{ steps.build_properties.outputs.properties }} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| # Intellij IDEA | ||
| .idea | ||
| .idea | ||
| .vscode/settings.json | ||
| .DS_Store |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.