|
| 1 | +# This action will keep track of the current crate verision, the last |
| 2 | +# pushed git tag and determine whether the project should be deployed |
| 3 | +# |
| 4 | +# Additionally it will keep track whether THE LAST TAG has a release |
| 5 | +# assigned, if it doesn't unless otherwise specified it will attempt |
| 6 | +# to create one. |
| 7 | + |
| 8 | +name: publish |
| 9 | + |
| 10 | +on: |
| 11 | + workflow_run: |
| 12 | + workflows: |
| 13 | + - tests |
| 14 | + types: |
| 15 | + - completed |
| 16 | + |
| 17 | +jobs: |
| 18 | + publish: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + # This only runs on main after tests have concluded successfully. |
| 21 | + if: github.ref == 'refs/heads/main' && github.event.workflow_run.conclusion == 'success' |
| 22 | + env: |
| 23 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} |
| 24 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + permissions: |
| 26 | + contents: write |
| 27 | + |
| 28 | + steps: |
| 29 | + # Need to checkout the repository before |
| 30 | + # using composite action to setup. |
| 31 | + - name: Checkout Repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + |
| 36 | + |
| 37 | + # Setup the environment with default |
| 38 | + # programs and setup required packages. |
| 39 | + - name: Setup Environment |
| 40 | + uses: ./.github/actions/setup |
| 41 | + with: |
| 42 | + npm-packages: "semver" |
| 43 | + |
| 44 | + |
| 45 | + # Parsing all the previous versions |
| 46 | + # is essential to determining whether a new |
| 47 | + # version should be published. |
| 48 | + # |
| 49 | + # This exports the crate version and the |
| 50 | + # latest git tag which help determine whether |
| 51 | + # a new version should be published. |
| 52 | + - name: Get And Parse Repository Versions |
| 53 | + id: versions |
| 54 | + run: | |
| 55 | + # The version of the main crate which should be the first on the list in the workspace config. |
| 56 | + echo "crate_version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')" \ |
| 57 | + >> $GITHUB_OUTPUT; |
| 58 | +
|
| 59 | + # The last pushed git tag if any. |
| 60 | + last_git_tag="$(git describe --tags --abbrev=0 || echo '')"; |
| 61 | + if [[ -z "$last_git_tag" ]]; then exit 0; fi; |
| 62 | + echo "last_tag=$last_git_tag" >> $GITHUB_OUTPUT; |
| 63 | +
|
| 64 | +
|
| 65 | + # In the case the last git tag is minor than |
| 66 | + # the current crate version, determine that |
| 67 | + # a new version should be published to crates.io. |
| 68 | + # |
| 69 | + # In the case the last git tag doesn't have |
| 70 | + # a release associated to it, determine a release |
| 71 | + # should be created with git cliff. |
| 72 | + - name: Determine What Should Be Done |
| 73 | + id: to_do |
| 74 | + run: | |
| 75 | + crate_version="${{ steps.versions.outputs.crate_version }}"; |
| 76 | + last_tag="${{ steps.versions.outputs.last_tag }}"; |
| 77 | +
|
| 78 | + # If there is no last tag, make up 0.0.0. |
| 79 | + if [ -z "$last_tag" ]; then |
| 80 | + last_tag="0.0.0" |
| 81 | + fi |
| 82 | +
|
| 83 | + # If the last published tag is minor than the crate's tag |
| 84 | + # determine that the crate should be published. |
| 85 | + if semver -r "> $last_tag" "$crate_version"; then |
| 86 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 87 | + else |
| 88 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 89 | + fi |
| 90 | +
|
| 91 | + # If there is no release associated to the last tag |
| 92 | + # determine that a release should be created. |
| 93 | + if ! gh release view "$last_tag" >/dev/null 2>&1; then |
| 94 | + echo "should_release=true" >> $GITHUB_OUTPUT; |
| 95 | + else |
| 96 | + echo "should_release=false" >> $GITHUB_OUTPUT; |
| 97 | + fi |
| 98 | +
|
| 99 | +
|
| 100 | + # In the case it's determined that the crate should be |
| 101 | + # published, then we call cargo publish and create |
| 102 | + # a new tag for the publication in the case the publication |
| 103 | + # succeeded. |
| 104 | + - name: Publish And Create A New Tag |
| 105 | + id: publish |
| 106 | + if: success() && steps.to_do.outputs.should_publish == 'true' |
| 107 | + run: | |
| 108 | + crate_version="${{ steps.versions.outputs.crate_version }}" |
| 109 | +
|
| 110 | + # If cargo publish fails a new release won't be made. |
| 111 | + # Make sure that it exits in case github actions doesn't halt. |
| 112 | + cargo publish || exit 1; |
| 113 | +
|
| 114 | + git tag "$crate_version"; |
| 115 | + git push origin "$crate_version"; |
| 116 | +
|
| 117 | + # We make a version override for which the changelog |
| 118 | + # will be created instead. |
| 119 | + echo "version_override=$crate_version" >> $GITHUB_OUTPUT; |
| 120 | +
|
| 121 | +
|
| 122 | + # In the case that or either it's determined that |
| 123 | + # a release should be created or a new publication |
| 124 | + # is done, then we call git-cliff-action@v4 to process |
| 125 | + # creating a changelog. |
| 126 | + - name: Generate a changelog |
| 127 | + uses: orhun/git-cliff-action@v4 |
| 128 | + if: > |
| 129 | + success() && ( |
| 130 | + steps.to_do.outputs.should_release == 'true' || |
| 131 | + steps.publish.outputs.version_override != '' |
| 132 | + ) |
| 133 | + id: git-cliff |
| 134 | + with: |
| 135 | + config: cliff.toml |
| 136 | + args: --verbose --latest --strip header |
| 137 | + |
| 138 | + |
| 139 | + # We call create-release@v1 in the case the same |
| 140 | + # condition as generating a changelog succeeds. |
| 141 | + - name: Create Release |
| 142 | + uses: actions/create-release@v1 |
| 143 | + if: > |
| 144 | + success() && ( |
| 145 | + steps.to_do.outputs.should_release == 'true' || |
| 146 | + steps.publish.outputs.version_override != '' |
| 147 | + ) |
| 148 | + with: |
| 149 | + tag_name: ${{ steps.versions.outputs.last_tag }} |
| 150 | + release_name: Release ${{ steps.versions.outputs.crate_version }} |
| 151 | + body: ${{ steps.git-cliff.outputs.content }} |
| 152 | + draft: false |
| 153 | + prerelease: false |
| 154 | + env: |
| 155 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments