diff --git a/.github/gitio_shorter.sh b/.github/gitio_shorter.sh new file mode 100755 index 0000000..ce1aae3 --- /dev/null +++ b/.github/gitio_shorter.sh @@ -0,0 +1,39 @@ +#!/bin/bash +set -eu + +is_registerable() { + local _code="$1" + local _res_code + _res_code="$(curl --retry 3 -s -o /dev/null -w "%{http_code}" "https://git.io/${_code}")" + if [[ "$_res_code" == 302 ]];then + echo "code ${_code} is NOT registerable" >&2 + return 1 + else + echo "code ${_code} is registerable" >&2 + return 0 + fi +} + +shorten() { + local _url="$1" + local _code="$2" + local _res_code + _res_code="$(curl --retry 3 -s -o /dev/null -w "%{http_code}" -F "url=${_url}" -F "code=${_code}" "https://git.io")" + if [[ "$_res_code" == 201 ]];then + echo "Succeed to register ${_code}, yay" >&2 + return 0 + else + echo "Failed to register ${_code} .." >&2 + return 1 + fi +} + +main() { + local _url="$1" + local _code="$2" + is_registerable "$_code" \ + && shorten "$_url" "$_code" \ + && echo "https://git.io/$_code" +} + +main ${1+"$@"} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ea8e756..b78d3e9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -135,10 +135,14 @@ jobs: name: rpm.x86_64 - shell: bash run: | - _url="https://github.com/greymd/teip/releases/download/v${{ steps.vars.outputs.ver }}/$(head -n 1 rpm.x86_64/digest.txt)" + _shorter=./.github/gitio_shorter.sh + _ver=${{ steps.vars.outputs.ver }} + _url="https://github.com/greymd/teip/releases/download/v${_ver}/$(head -n 1 rpm.x86_64/digest.txt)" _sha=$(tail -n 1 rpm.x86_64/digest.txt) - sed -i "/rpm_x86_64_start/,/rpm_x86_64_end/s|https://.*rpm|$_url|g" ./README.md + _short_url=$($_shorter "${_url}" "teip-${_ver}.x86_64.rpm" || $_shorter "${_url}" "teip-${_ver}-${_sha:1:5}.x86_64.rpm" || echo "${_url}") + sed -i "/rpm_x86_64_start/,/rpm_x86_64_end/s|https://.*rpm|$_short_url|g" ./README.md sed -i "/rpm_x86_64_sha256/s/SHA256:.*/SHA256: $_sha/" ./README.md + cat ./README.md - name: Apply artifacts deb uses: actions/download-artifact@v1 @@ -146,10 +150,14 @@ jobs: name: deb.x86_64 - shell: bash run: | - _url="https://github.com/greymd/teip/releases/download/v${{ steps.vars.outputs.ver }}/$(head -n 1 deb.x86_64/digest.txt)" + _shorter=./.github/gitio_shorter.sh + _ver=${{ steps.vars.outputs.ver }} + _url="https://github.com/greymd/teip/releases/download/v${_ver}/$(head -n 1 deb.x86_64/digest.txt)" _sha=$(tail -n 1 deb.x86_64/digest.txt) - sed -i "/deb_x86_64_start/,/deb_x86_64_end/s|https://.*deb|$_url|g" ./README.md + _short_url=$($_shorter "${_url}" "teip-${_ver}.x86_64.deb" || $_shorter "${_url}" "teip-${_ver}-${_sha:1:5}.x86_64.deb" || echo "${_url}") + sed -i "/deb_x86_64_start/,/deb_x86_64_end/s|https://.*deb|$_short_url|g" ./README.md sed -i "/deb_x86_64_sha256/s/SHA256:.*/SHA256: $_sha/" ./README.md + cat ./README.md - name: Release README if: "! contains (steps.vars.outputs.ver, 'beta')"