diff --git a/.github/actions/publish/action.yaml b/.github/actions/publish/action.yaml new file mode 100644 index 00000000..97e46d6c --- /dev/null +++ b/.github/actions/publish/action.yaml @@ -0,0 +1,13 @@ +name: Publish crate +description: Publishes crate to crates.io +inputs: + token: + description: Cargo login token to use the publish the crate + required: true +runs: + using: composite + steps: + - shell: bash + id: publish_crate + run: | + ${{ github.action_path }}/publish.sh --token ${{ inputs.token }} diff --git a/.github/actions/publish/publish.sh b/.github/actions/publish/publish.sh new file mode 100755 index 00000000..aba73587 --- /dev/null +++ b/.github/actions/publish/publish.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Publishes crate to crates.io + +token="" +while true; do + case $1 in + "--token") + shift + token="$1" + shift + ;; + *) + break + ;; + esac +done + +if [[ "$token" == "" ]]; then + echo "Missing --token option argument, cannot publish crates without it!" && exit 1 +fi + +function publish { + module="$1" + if [[ "$module" == "utoipa" ]]; then + cargo publish + else + cargo publish -p "$module" + fi +} + +echo "$token" | cargo login +while read -r manifest; do + echo "Publishing module $manifest..." + module=$(cargo read-manifest --manifest-path "$manifest" | jq -r .name) + + max_retries=5 + retry=0 + while ! publish "$module" && [[ $retry -lt $max_retries ]]; do + await_time=$((retry*2)) + echo "Failed to publish, Retrying $retry... after $await_time sec." + sleep $await_time + retry=$((retry+1)) + done + if [[ $retry -eq $max_retries ]]; then + echo "Failed to publish crate $module, try to increase await time? Or retries?" && exit 1 + fi +done < <(find . ! -path "*target*" -name "Cargo.toml" | sort -r) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index ca05eb78..9fcd2702 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -16,31 +16,7 @@ jobs: with: fetch-depth: 2 - - name: Cargo publish - run: | - echo ${{ secrets.CARGO_LOGIN }} | cargo login - manifests=$(git diff --name-only ${{ github.sha }}^ ${{ github.sha }} | awk '{if ($0 ~ /.*Cargo.toml/) print $0}' | sort -r) - for manifest in $(echo "$manifests" | xargs); do - module=$(cargo read-manifest --manifest-path "$manifest" | jq -r .name) - if [[ "$module" == "utoipa" ]]; then - retry=0 - while ! cargo publish && [[ $retry -lt 3 ]]; do - echo "Failed to publish, Retrying... $retry after $((retry*1)) sec." - sleep $((retry*1)) - retry=$((retry+1)) - done - if [[ $retry -eq 3 ]]; then - echo "Failed to publish crate utoipa, try to increase wait? Or retries?" && exit 1 - fi - else - retry=0 - while ! cargo publish -p "$module" && [[ $retry -lt 3 ]]; do - echo "Failed to publish, Retrying... $retry after $((retry*1)) sec." - sleep $((retry*1)) - retry=$((retry+1)) - done - if [[ $retry -eq 3 ]]; then - echo "Failed to publish crate $module, try to increase wait? Or retries?" && exit 1 - fi - fi - done + - uses: ./.github/actions/publish + name: Cargo publish + with: + token: ${{ secrets.CARGO_LOGIN }}