Skip to content

Instantly share code, notes, and snippets.

@crstauf
Last active July 20, 2023 17:30
Show Gist options
  • Save crstauf/0180b506e43f9e3d8730b995afb11849 to your computer and use it in GitHub Desktop.
Save crstauf/0180b506e43f9e3d8730b995afb11849 to your computer and use it in GitHub Desktop.
Trigger a GitHub Action on plugin update. #github #plugin #update #trigger
<?php
/**
* Plugin URI: https://gist.github.com/crstauf/0180b506e43f9e3d8730b995afb11849
* Author: Caleb Stauffer
* Author URI: https://develop.calebstauffer.com
*/
defined( 'WPINC' ) || die();
if ( ! defined( 'INIT_GITHUB_ACTION_TOKEN' ) || ! constant( 'INIT_GITHUB_ACTION_TOKEN' ) ) {
return;
}
add_action( 'upgrader_process_complete', static function ( $upgraded, $extra ) {
if ( empty( $extra['plugins'] ) ) {
return;
}
$url = sprintf(
'https://api.github.com/repos/%s/actions/workflows/%s/dispatches',
'', // {organization}/{repo}
'' // workflow filename
);
wp_remote_post( $url, array(
'blocking' => false,
'headers' => array(
'Accept' => 'application/vnd.github+json',
'X-GitHub-Api-Version' => '2022-11-28',
'Authorization' => sprintf( 'Bearer %s', constant( 'INIT_GITHUB_ACTION_TOKEN' ) ),
),
'body' => wp_json_encode( array(
'ref' => '', // base branch name
) ),
) );
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment