Skip to content

Build, Release, and Publish #21

Build, Release, and Publish

Build, Release, and Publish #21

Workflow file for this run

name: Build, Release, and Publish
on:
workflow_dispatch:
inputs:
partner_chains_smart_contracts_sha:
description: "Commit SHA or branch to build from"
partner-chains-smart-contracts-tag:
description: "Specify a new tag or leave empty for default (default: package.json version)"
required: false
create_draft_release_page:
type: boolean
description: "Create a draft release page"
publish_to_npm:
type: boolean
description: "Publish to NPM"
jobs:
partner-chains-smart-contracts:
runs-on: [self-hosted, nixos]
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.partner_chains_smart_contracts_sha }}
- name: Build
run: nix build ./#pc-contracts-release-bundle
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: partner-chains-smart-contracts
path: result/release.zip
partner-chains-smart-contracts-process:
runs-on: ubuntu-latest
needs: partner-chains-smart-contracts
outputs:
pc_contracts_cli: ${{ steps.set_filename_vars.outputs.pc_contracts_cli }}
version: ${{ steps.set_version.outputs.version }}
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: partner-chains-smart-contracts
path: ./non-arch
- uses: geekyeggo/delete-artifact@v5
with:
name: partner-chains-smart-contracts
- name: Unzip release.zip
run: |
mkdir -p ./non-arch/unzipped
unzip ./non-arch/release.zip -d ./non-arch/unzipped
- name: Set version
id: set_version
run: |
if [ -n "${{ github.event.inputs.partner-chains-smart-contracts-tag }}" ]; then
echo "version=${{ github.event.inputs.partner-chains-smart-contracts-tag }}" >> $GITHUB_OUTPUT
else
echo "version=$(jq -r '.version' ./non-arch/unzipped/package.json)" >> $GITHUB_OUTPUT
fi
- name: Set filename variables
id: set_filename_vars
run: |
PC_CONTRACTS_CLI="pc-contracts-cli-v${{ steps.set_version.outputs.version }}"
echo "pc_contracts_cli=${PC_CONTRACTS_CLI}" >> $GITHUB_OUTPUT
- name: Zip the artifact
run: |
cd ./non-arch/unzipped
zip -r "../../${{ steps.set_filename_vars.outputs.pc_contracts_cli }}.zip" ./*
cd ../../
- name: Upload zipped Artifact
uses: actions/upload-artifact@v4
with:
name: "${{ steps.set_filename_vars.outputs.pc_contracts_cli }}.zip"
path: "${{ steps.set_filename_vars.outputs.pc_contracts_cli }}.zip"
release:
runs-on: ubuntu-latest
needs: partner-chains-smart-contracts-process
if: ${{ github.event.inputs.create_draft_release_page == 'true' }}
steps:
- name: Download zipped artifact
uses: actions/download-artifact@v4
with:
name: "${{ needs.partner-chains-smart-contracts-process.outputs.pc_contracts_cli }}.zip"
path: ./
- name: Check if release already exists
id: check_release
run: |
tag="v${{ needs.partner-chains-smart-contracts-process.outputs.version }}"
release_response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/$tag")
if echo "$release_response" | grep -q '"message": "Not Found"'; then
echo "release_exists=false" >> $GITHUB_ENV
echo "::set-output name=release_exists::false"
else
echo "release_exists=true" >> $GITHUB_ENV
echo "::set-output name=release_exists::true"
echo "release_id=$(echo $release_response | jq -r .id)" >> $GITHUB_ENV
echo "::set-output name=release_id::$(echo $release_response | jq -r .id)"
fi
- name: Create draft release
id: create_release
if: ${{ steps.check_release.outputs.release_exists == 'false' }}
run: |
tag="v${{ needs.partner-chains-smart-contracts-process.outputs.version }}"
release_response=$(curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d '{"tag_name": "'$tag'", "name": "'$tag'", "body": "Draft release for '$tag'", "draft": true}' \
"https://api.github.com/repos/${{ github.repository }}/releases")
echo "release_id=$(echo $release_response | jq -r .id)" >> $GITHUB_ENV
echo "::set-output name=release_id::$(echo $release_response | jq -r .id)"
- name: Upload zipped artifact to release
if: ${{ steps.check_release.outputs.release_exists == 'true' || steps.create_release.outputs.release_id != '' }}
run: |
release_id="${{ steps.create_release.outputs.release_id }}"
if [ -z "$release_id" ]; then
release_id="${{ steps.check_release.outputs.release_id }}"
fi
artifact="${{ needs.partner-chains-smart-contracts-process.outputs.pc_contracts_cli }}.zip"
curl -s -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/octet-stream" \
--data-binary @"$artifact" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/$release_id/assets?name=$(basename $artifact)"
publish:
runs-on: ubuntu-latest
needs: partner-chains-smart-contracts-process
if: ${{ github.event.inputs.publish_to_npm == 'true' }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
steps:
- name: Download zipped Artifact
uses: actions/download-artifact@v4
with:
name: "${{ needs.partner-chains-smart-contracts-process.outputs.pc_contracts_cli }}.zip"
path: ./
- name: Extract Artifact
run: |
unzip "${{ needs.partner-chains-smart-contracts-process.outputs.pc_contracts_cli }}.zip" -d extracted
chmod -R 755 extracted
- name: Update package.json version
run: |
cd extracted
npm version "${{ needs.partner-chains-smart-contracts-process.outputs.version }}" --no-git-tag-version
cd ..
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
registry-url: 'https://registry.npmjs.org/'
- name: Publish package
run: npm publish
working-directory: ./extracted
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}