Skip to content

Commit

Permalink
Extract inline publish step to separate action
Browse files Browse the repository at this point in the history
* Create seprate action for publish to crates.io making it simplier to maintain.
* Now released crates are not based on lastest change but all will be released.
  • Loading branch information
juhaku committed Feb 14, 2022
1 parent f9853bc commit 49a5220
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 28 deletions.
13 changes: 13 additions & 0 deletions .github/actions/publish/action.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
48 changes: 48 additions & 0 deletions .github/actions/publish/publish.sh
Original file line number Diff line number Diff line change
@@ -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 <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)
32 changes: 4 additions & 28 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

0 comments on commit 49a5220

Please sign in to comment.