|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - master |
| 8 | + paths: |
| 9 | + - Cargo.toml |
| 10 | + - Cargo.lock |
| 11 | + - "contracts/**" |
| 12 | + - .github/workflows/release.yml |
| 13 | + pull_request: |
| 14 | + types: [opened, synchronize, reopened] |
| 15 | + branches: |
| 16 | + - master |
| 17 | + paths: |
| 18 | + - Cargo.toml |
| 19 | + - Cargo.lock |
| 20 | + - "contracts/**" |
| 21 | + - .github/workflows/release.yml |
| 22 | + |
| 23 | +jobs: |
| 24 | + prepare: |
| 25 | + name: Prepare |
| 26 | + runs-on: ubuntu-latest |
| 27 | + outputs: |
| 28 | + rust_protocol_matrix: ${{ steps.setup_matrix.outputs.rust_protocol_matrix }} |
| 29 | + version: ${{ steps.version_info.outputs.version }} |
| 30 | + release_required: ${{ steps.version_info.outputs.release_required }} |
| 31 | + build_required: ${{ steps.version_info.outputs.build_required }} |
| 32 | + target_commit: ${{ steps.version_info.outputs.target_commit }} |
| 33 | + prerelease: ${{ steps.version_info.outputs.prerelease }} |
| 34 | + overwrite_release: ${{ steps.version_info.outputs.overwrite_release }} |
| 35 | + steps: |
| 36 | + - name: Checkout code |
| 37 | + uses: actions/checkout@v4 |
| 38 | + |
| 39 | + - name: Setup matrix |
| 40 | + id: setup_matrix |
| 41 | + run: | |
| 42 | + echo 'rust_protocol_matrix=["icp", "near", "stellar"]' >> "$GITHUB_OUTPUT" |
| 43 | +
|
| 44 | + - name: Get version info |
| 45 | + id: version_info |
| 46 | + shell: bash |
| 47 | + env: |
| 48 | + GH_TOKEN: ${{ github.token }} |
| 49 | + run: | |
| 50 | + echo "Validating contract versions" |
| 51 | +
|
| 52 | + version_candidate="" |
| 53 | + for protocol in $(echo '${{ steps.setup_matrix.outputs.rust_protocol_matrix }}' | jq -r '.[]'); do |
| 54 | + for contract in "contracts/${protocol}"/*; do |
| 55 | + if [ -d "${contract}" ]; then |
| 56 | + contract_version=$(cargo pkgid --manifest-path "${contract}/Cargo.toml" | awk -F'[#@]' '{print $NF}') |
| 57 | + echo " contract: $contract, version: $contract_version" |
| 58 | +
|
| 59 | + if [ -z "$version_candidate" ]; then |
| 60 | + version_candidate="$contract_version" |
| 61 | + elif [ "$version_candidate" != "$contract_version" ]; then |
| 62 | + echo "Version mismatch between contracts" |
| 63 | + echo "Make sure all contracts have the same version" |
| 64 | + echo "All contract protocols:" |
| 65 | + echo " - rust_protocol_matrix: '${{ steps.setup_matrix.outputs.rust_protocol_matrix }}'" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + fi |
| 69 | + done |
| 70 | + done |
| 71 | + echo "Valid version candidate: $version_candidate" |
| 72 | +
|
| 73 | + echo "target_commit=${{ github.sha }}" >> $GITHUB_OUTPUT |
| 74 | +
|
| 75 | + if [ "${{ github.ref }}" == "refs/heads/master" ]; then |
| 76 | + version="$version_candidate" |
| 77 | + echo "Master version: $version" |
| 78 | +
|
| 79 | + if gh release view "$version" --repo ${{ github.repository }} >/dev/null 2>&1; then |
| 80 | + echo "Master release for this version already exists" |
| 81 | + echo "release_required=false" >> $GITHUB_OUTPUT |
| 82 | + else |
| 83 | + echo "New master release required" |
| 84 | + echo "release_required=true" >> $GITHUB_OUTPUT |
| 85 | + fi |
| 86 | +
|
| 87 | + echo "build_required=true" >> $GITHUB_OUTPUT |
| 88 | + echo "prerelease=false" >> $GITHUB_OUTPUT |
| 89 | + echo "overwrite_release=false">> $GITHUB_OUTPUT |
| 90 | + echo "version=$version" >> $GITHUB_OUTPUT |
| 91 | + elif [ "${{ github.event_name }}" == "pull_request" ] && [[ "${{ github.head_ref }}" == release/* ]]; then |
| 92 | + version="prerelease-${{ github.event.number }}" |
| 93 | + echo "Prerelease version: $version" |
| 94 | +
|
| 95 | + echo "build_required=true" >> $GITHUB_OUTPUT |
| 96 | + echo "release_required=true" >> $GITHUB_OUTPUT |
| 97 | + echo "prerelease=true" >> $GITHUB_OUTPUT |
| 98 | + echo "overwrite_release=true">> $GITHUB_OUTPUT |
| 99 | + echo "version=$version" >> $GITHUB_OUTPUT |
| 100 | + else |
| 101 | + echo "This is not a master branch or a release PR" |
| 102 | + echo "build_required=false" >> $GITHUB_OUTPUT |
| 103 | + echo "release_required=false" >> $GITHUB_OUTPUT |
| 104 | + fi |
| 105 | +
|
| 106 | + build-rust: |
| 107 | + name: Build Rust |
| 108 | + if: needs.prepare.outputs.build_required == 'true' |
| 109 | + runs-on: ubuntu-latest |
| 110 | + needs: prepare |
| 111 | + |
| 112 | + steps: |
| 113 | + - name: Checkout code |
| 114 | + uses: actions/checkout@v4 |
| 115 | + |
| 116 | + - name: Setup rust toolchain |
| 117 | + run: rustup toolchain install stable --profile minimal |
| 118 | + |
| 119 | + - name: Setup rust cache |
| 120 | + uses: Swatinem/rust-cache@v2 |
| 121 | + with: |
| 122 | + key: release |
| 123 | + |
| 124 | + - name: Install candid-extractor |
| 125 | + run: | |
| 126 | + TMPDIR=$(mktemp -d) |
| 127 | + VERSION="0.1.5" |
| 128 | + BINARY="candid-extractor-x86_64-unknown-linux-gnu.tar.gz" |
| 129 | +
|
| 130 | + wget -P "$TMPDIR" "https://github.com/dfinity/candid-extractor/releases/download/${VERSION}/${BINARY}" |
| 131 | + tar xzf "$TMPDIR/$BINARY" -C "$TMPDIR" |
| 132 | + mkdir -p "$HOME/.local/bin" |
| 133 | + mv "$TMPDIR/candid-extractor" "$HOME/.local/bin/" |
| 134 | + rm -rf "$TMPDIR" |
| 135 | +
|
| 136 | + candid-extractor --version |
| 137 | +
|
| 138 | + - name: Install wasm-opt |
| 139 | + run: | |
| 140 | + TMPDIR=$(mktemp -d) |
| 141 | + VERSION="version_122" |
| 142 | + BINARY="binaryen-$VERSION-x86_64-linux.tar.gz" |
| 143 | +
|
| 144 | + wget -P "$TMPDIR" "https://github.com/WebAssembly/binaryen/releases/download/$VERSION/$BINARY" |
| 145 | + tar xzf "$TMPDIR/$BINARY" -C "$TMPDIR" |
| 146 | + mkdir -p "$HOME/.local/bin" |
| 147 | + mv "$TMPDIR/binaryen-$VERSION/bin/"* "$HOME/.local/bin/" |
| 148 | +
|
| 149 | + # Cleanup temporary directory |
| 150 | + rm -rf "$TMPDIR" |
| 151 | +
|
| 152 | + wasm-opt --version |
| 153 | +
|
| 154 | + - name: Build |
| 155 | + run: | |
| 156 | + ./scripts/build-rust.sh |
| 157 | +
|
| 158 | + - name: Compress artifacts using gzip |
| 159 | + run: | |
| 160 | + create_archive() { |
| 161 | + local protocol_name=$1 |
| 162 | + local artifacts_folder=$2 |
| 163 | +
|
| 164 | + local archive_name="${protocol_name}.tar.gz" |
| 165 | + local tmp_dir=$(mktemp -d) |
| 166 | +
|
| 167 | + for contract in "contracts/${protocol_name}"/*; do |
| 168 | + if [ -d "${contract}" ]; then |
| 169 | + find "${contract}/res" -maxdepth 1 -type f -exec cp {} "$tmp_dir" \; |
| 170 | + fi |
| 171 | + done |
| 172 | +
|
| 173 | + ls -al "$tmp_dir" |
| 174 | +
|
| 175 | + tar -czf "$archive_name" -C "$tmp_dir" . |
| 176 | + mv "$archive_name" "$artifacts_folder" |
| 177 | + rm -rf "$tmp_dir" |
| 178 | + } |
| 179 | +
|
| 180 | + mkdir -p artifacts |
| 181 | +
|
| 182 | + for protocol in $(echo '${{ needs.prepare.outputs.rust_protocol_matrix }}' | jq -r '.[]'); do |
| 183 | + create_archive "${protocol}" artifacts |
| 184 | + done |
| 185 | +
|
| 186 | + ls -al artifacts |
| 187 | +
|
| 188 | + - name: Upload artifacts |
| 189 | + uses: actions/upload-artifact@v4 |
| 190 | + with: |
| 191 | + name: artifacts-${{ matrix.target }} |
| 192 | + path: artifacts/* |
| 193 | + retention-days: 2 |
| 194 | + |
| 195 | + release: |
| 196 | + name: Release |
| 197 | + if: needs.prepare.outputs.release_required == 'true' |
| 198 | + runs-on: ubuntu-latest |
| 199 | + needs: [prepare, build-rust] |
| 200 | + steps: |
| 201 | + - name: Checkout code |
| 202 | + uses: actions/checkout@v4 |
| 203 | + |
| 204 | + - name: Download Artifact |
| 205 | + uses: actions/download-artifact@v4 |
| 206 | + with: |
| 207 | + path: artifacts/ |
| 208 | + merge-multiple: true |
| 209 | + |
| 210 | + - name: Upload binaries to release |
| 211 | + uses: svenstaro/upload-release-action@v2 |
| 212 | + with: |
| 213 | + repo_token: ${{ secrets.GITHUB_TOKEN }} |
| 214 | + file: artifacts/* |
| 215 | + file_glob: true |
| 216 | + tag: ${{ needs.prepare.outputs.version }} |
| 217 | + release_name: ${{ needs.prepare.outputs.version }} |
| 218 | + prerelease: ${{ needs.prepare.outputs.prerelease }} |
| 219 | + overwrite: ${{ needs.prepare.outputs.overwrite_release }} |
| 220 | + target_commit: ${{ needs.prepare.outputs.target_commit }} |
0 commit comments