name: Deploy to staging on: pull_request: permissions: contents: read id-token: write # Required for workflows that call test.yml (Codecov OIDC) pull-requests: write # Required for deployment comments jobs: # Check if we need to run contract-related jobs check-changes: name: Check for relevant changes runs-on: ubuntu-latest outputs: should-run-contracts: ${{ steps.changes.outputs.contracts }} steps: - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Check for changes uses: dorny/paths-filter@v3 id: changes with: filters: | contracts: - 'common/**' - 'contract/**' - 'mock/**' - 'script/**' test: needs: check-changes if: needs.check-changes.outputs.should-run-contracts == 'true' uses: ./.github/workflows/test.yml with: skip_coverage: true gas-report: needs: check-changes if: needs.check-changes.outputs.should-run-contracts == 'true' uses: ./.github/workflows/gas-report.yml deploy-staging: name: Deploy to staging subaccount needs: [test, gas-report, check-changes] # Run if contracts changed OR if gas-report was skipped if: always() && needs.test.result == 'success' && (needs.gas-report.result == 'success' || needs.gas-report.result == 'skipped') runs-on: ubuntu-latest env: NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID: gh-${{ github.event.number }}.${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }} MARKET_VERSION_ID: market-${{ github.run_id }} MARKET_NAME: default-${{ github.run_id }} steps: - name: Checkout repository uses: actions/checkout@v4 - uses: ./.github/actions/rust - name: Install NEAR CLI uses: ./.github/actions/install-near-cli with: network: "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" rpc-url: "${{ vars.NEAR_CONTRACT_STAGING_RPC_URL }}" rpc_api_key: "${{ secrets.NEAR_CONTRACT_STAGING_RPC_API_KEY }}" - name: Install cargo-near CLI run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.16.1/cargo-near-installer.sh | sh - name: Initialize staging account run: | ACCOUNT_EXISTS=$(./script/ci/account-exists.sh \ --account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}") if [[ -z "$ACCOUNT_EXISTS" ]]; then echo "Account does not already exist, creating" near account create-account fund-myself "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" '20 NEAR' \ use-manually-provided-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ sign-as "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}" \ network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ sign-with-plaintext-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ send echo "NEWLY_CREATED=1" >> $GITHUB_ENV else near tokens "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}" \ send-near "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" '6 NEAR' \ network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ sign-with-plaintext-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ send CONTRACT_EXISTS=$(./script/ci/contract-exists.sh \ --contract "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}") if [[ -z "$CONTRACT_EXISTS" ]]; then echo "Contract already exists on staging account, removing old market versions" ./script/ci/remove-all-versions-from-registry.sh \ --account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --registry "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ --private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" else echo "NEWLY_CREATED=1" >> $GITHUB_ENV fi fi - name: Deploy registry to staging account run: | ./script/ci/deploy-registry.sh \ --account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ --private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ --init $([[ -n "$NEWLY_CREATED" ]] && echo true || echo false) - name: Add market version to registry run: | ./script/ci/add-market-version.sh \ --account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --registry "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --version-key "${{ env.MARKET_VERSION_ID }}" \ --deploy-mode normal \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ --private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" - name: Generate init args run: | echo "INIT_ARGS<> $GITHUB_ENV cargo run --package test-utils --example generate_testnet_configuration >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV - name: Deploy market from registry run: | ./script/ci/deploy-from-registry.sh \ --account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --registry "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ --version-key "${{ env.MARKET_VERSION_ID }}" \ --init-args "${INIT_ARGS}" \ --name "${{ env.MARKET_NAME }}" \ --with-full-access-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ --private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" - name: Generate market ID run: | echo "MARKET_ACCOUNT_ID=${{ env.MARKET_NAME }}.${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" >> $GITHUB_ENV - name: Perform storage deposits run: | BORROW_ID=$(echo "$INIT_ARGS" | jq -r '.configuration.borrow_asset.Nep141') if [ -n "$BORROW_ID" ]; then ./script/ci/storage-deposit.sh \ --account "${MARKET_ACCOUNT_ID}" \ --contract "${BORROW_ID}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ --private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" fi COLLATERAL_ID=$(echo "$INIT_ARGS" | jq -r '.configuration.collateral_asset.Nep141') if [ -n "$COLLATERAL_ID" ]; then ./script/ci/storage-deposit.sh \ --account "${MARKET_ACCOUNT_ID}" \ --contract "${COLLATERAL_ID}" \ --network "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ --private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" fi - name: Comment on pull request env: GH_TOKEN: ${{ github.token }} MARKET_ACCOUNT_ID: ${{ env.MARKET_NAME }}.${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }} run: | truncate -s0 comment.md echo "| Name | Deployment |" >> comment.md echo "| ---- | ---------- |" >> comment.md echo "| Registry | ["'`'"${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}"'`'"](https://$([[ \"${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}\" == \"testnet\" ]] && echo 'testnet.' || echo '')nearblocks.io/address/${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}) |" >> comment.md echo "| Default market | ["'`'"${MARKET_ACCOUNT_ID}"'`'"](https://$([[ \"${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}\" == \"testnet\" ]] && echo 'testnet.' || echo '')nearblocks.io/address/${MARKET_ACCOUNT_ID}) |" >> comment.md echo '' >> comment.md echo "---" >> comment.md echo '' >> comment.md echo '${{ needs.gas-report.outputs.comment }}' >> comment.md gh pr comment "${{ github.event.number }}" -F comment.md --create-if-none --edit-last