#! /bin/bash # # Trigger the package build in Fedora Copr from a GitHub Actions envioronment. # # The build in copr will build against existing PR, or against already pushed # commit. # # This script can be HTTP downloaded, and work in isolation under Travis # environment. Note that this script is prepared to be easily copy-pasted to # any GitHub Actions capable GitHub project and that the original script # 'copr-gh-actions-submit' comes from the # https://github.com/praiskup/copr-ci-tooling repository, so please submit # patches/issues against that project (and keep your copy in sync). # # Copyright (C) 2021 Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. die () { echo >&2 "$*" ; exit 1 ; } # Please set COPR_PR_WEBHOOK (not encrypted) and COPR_PUSH_WEBHOOK (encrypted) # variables in Travis CI. PR_ID=$1 REF=HEAD TYPE=PR if test -n "$PR_ID"; then REF=refs/pull/$PR_ID git fetch origin "+pull/$PR_ID/head:$REF" --depth=1 COPR_WEBHOOK=$COPR_PR_WEBHOOK else COPR_WEBHOOK=$COPR_PUSH_WEBHOOK TYPE=PUSH fi test -z "$COPR_WEBHOOK" && die "webhook unset" webhook_data() { echo "{" echo " \"type\": \"$TYPE\"," test $TYPE = PR && \ echo " \"pr_id\": \"$PR_ID\"," echo " \"git_hash\": \"$(git rev-parse "$REF")\"" echo "}" echo } echo "submitting JSON data:" webhook_data echo # Copr accepts only json content, hack. build_id=$(curl \ --silent \ -X POST \ -H "Content-Type: application/json" \ --data "$(webhook_data)" \ "$COPR_WEBHOOK" ) build_url="https://copr.fedorainfracloud.org/coprs/build/$build_id/" echo "submitted build: $build_url" set -o pipefail sed_search_state='s/.*"state":[[:space:]]*"\([a-z]*\)".*/\1/' url="https://copr.fedorainfracloud.org/api_3/build/$build_id/" echo "checking $url for the build status" while :; do sleep 30 build_status=$(curl -sS --fail "$url" | sed "$sed_search_state") echo "build $build_url status: $build_status" case $build_status in *failed|*canceled) exit 1 ;; *succeeded) exit 0 ;; esac done