|
| 1 | +name: Publish to GitHub Packages |
| 2 | + |
| 3 | +on: |
| 4 | + # Trigger on pull requests (for testing/validation) - only from same repo |
| 5 | + pull_request: |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + # Trigger on pushes to main branch |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + # Allow manual triggering |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +env: |
| 15 | + NODE_VERSION: lts/jod |
| 16 | + REGISTRY: npm.pkg.github.com |
| 17 | + SCOPE: '@cowprotocol' |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + packages: write |
| 22 | + issues: write |
| 23 | + pull-requests: write |
| 24 | + |
| 25 | +jobs: |
| 26 | + publish: |
| 27 | + name: Publish to GitHub Packages |
| 28 | + runs-on: ubuntu-latest |
| 29 | + |
| 30 | + # Security: Only run on internal PRs, main branch pushes, or external PRs with explicit approval |
| 31 | + if: github.event_name == 'push' || (github.event_name == 'pull_request' && (github.event.pull_request.head.repo.full_name == github.repository || contains(github.event.pull_request.labels.*.name, 'allow-publish'))) |
| 32 | + |
| 33 | + steps: |
| 34 | + - name: Security Check |
| 35 | + run: | |
| 36 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 37 | + echo "🔒 Checking PR security..." |
| 38 | + echo "PR from: ${{ github.event.pull_request.head.repo.full_name }}" |
| 39 | + echo "Target repo: ${{ github.repository }}" |
| 40 | +
|
| 41 | + # Check if it's an internal PR |
| 42 | + if [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ]; then |
| 43 | + echo "✅ Internal PR - proceeding with publish" |
| 44 | + else |
| 45 | + # Check for allow-publish label |
| 46 | + echo "🔍 Checking for 'allow-publish' label..." |
| 47 | + if echo "${{ toJson(github.event.pull_request.labels) }}" | grep -q '"name":"allow-publish"'; then |
| 48 | + echo "✅ External PR with 'allow-publish' label - proceeding with publish" |
| 49 | + else |
| 50 | + echo "❌ External PR without 'allow-publish' label - skipping publish for security" |
| 51 | + echo "💡 To enable publishing for this external PR, add the 'allow-publish' label" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + fi |
| 55 | + else |
| 56 | + echo "✅ Push to main branch - proceeding with publish" |
| 57 | + fi |
| 58 | +
|
| 59 | + - name: Cancel Previous Runs |
| 60 | + |
| 61 | + with: |
| 62 | + access_token: ${{ github.token }} |
| 63 | + |
| 64 | + - name: Remove broken apt repos [Ubuntu] |
| 65 | + run: | |
| 66 | + for apt_file in `grep -lr microsoft /etc/apt/sources.list.d/`; do sudo rm $apt_file; done |
| 67 | +
|
| 68 | + - name: Checkout code |
| 69 | + uses: actions/checkout@v4 |
| 70 | + |
| 71 | + - name: Setup Node.js |
| 72 | + uses: actions/setup-node@v4 |
| 73 | + with: |
| 74 | + node-version: ${{ env.NODE_VERSION }} |
| 75 | + scope: ${{ env.SCOPE }} |
| 76 | + always-auth: true |
| 77 | + |
| 78 | + - name: Setup pnpm |
| 79 | + uses: pnpm/action-setup@v4 |
| 80 | + with: |
| 81 | + version: 10.8.0 |
| 82 | + |
| 83 | + - name: Cache pnpm store |
| 84 | + uses: actions/cache@v4 |
| 85 | + with: |
| 86 | + path: ~/.pnpm-store |
| 87 | + key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 88 | + |
| 89 | + - name: Install dependencies |
| 90 | + run: pnpm install --frozen-lockfile |
| 91 | + |
| 92 | + - name: Generate code |
| 93 | + run: pnpm codegen |
| 94 | + |
| 95 | + - name: Build packages |
| 96 | + run: pnpm build |
| 97 | + |
| 98 | + - name: Run tests |
| 99 | + run: pnpm test |
| 100 | + |
| 101 | + - name: Run linting |
| 102 | + run: pnpm lint |
| 103 | + |
| 104 | + - name: Configure npm for GitHub Packages |
| 105 | + run: | |
| 106 | + # Override registry for @cowprotocol scope to use GitHub Packages |
| 107 | + echo "@cowprotocol:registry=https://${{ env.REGISTRY }}" >> ~/.npmrc |
| 108 | +
|
| 109 | + # Always authenticate |
| 110 | + echo "always-auth=true" >> ~/.npmrc |
| 111 | +
|
| 112 | + # Set auth token |
| 113 | + echo "//${{ env.REGISTRY }}/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> ~/.npmrc |
| 114 | + # Keep default registry as npmjs.org for other packages |
| 115 | + echo "registry=https://registry.npmjs.org/" >> ~/.npmrc |
| 116 | +
|
| 117 | + - name: Determine version strategy |
| 118 | + id: version |
| 119 | + run: | |
| 120 | + COMMIT_HASH=$(echo "${{ github.sha }}" | cut -c1-8) |
| 121 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 122 | + # For PRs, use PR number + commit hash for uniqueness |
| 123 | + VERSION_SUFFIX="pr-${{ github.event.number }}-$COMMIT_HASH" |
| 124 | + echo "version_suffix=$VERSION_SUFFIX" >> $GITHUB_OUTPUT |
| 125 | + echo "tag=pr" >> $GITHUB_OUTPUT |
| 126 | + echo "is_pr=true" >> $GITHUB_OUTPUT |
| 127 | + else |
| 128 | + # For main branch, use latest tag |
| 129 | + BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's/refs\/heads\///') |
| 130 | + VERSION_SUFFIX="${BRANCH_NAME}-$COMMIT_HASH" |
| 131 | + echo "version_suffix=$VERSION_SUFFIX" >> $GITHUB_OUTPUT |
| 132 | + echo "tag=latest" >> $GITHUB_OUTPUT |
| 133 | + echo "is_pr=false" >> $GITHUB_OUTPUT |
| 134 | + fi |
| 135 | +
|
| 136 | + - name: Create pre-release versions for PRs |
| 137 | + if: steps.version.outputs.is_pr == 'true' |
| 138 | + run: | |
| 139 | + # Create pre-release versions for all packages |
| 140 | + find packages -name "package.json" -exec sh -c ' |
| 141 | + for file; do |
| 142 | + echo "Creating pre-release version for $file" |
| 143 | + # Use npm version to create pre-release version |
| 144 | + cd "$(dirname "$file")" |
| 145 | + pnpm version prerelease --preid="${{ steps.version.outputs.version_suffix }}" --no-git-tag-version |
| 146 | + cd - > /dev/null |
| 147 | + done |
| 148 | + ' _ {} \; |
| 149 | +
|
| 150 | + - name: Publish packages to GitHub Packages |
| 151 | + run: | |
| 152 | + # Publish all packages and capture versions |
| 153 | + echo "Publishing packages..." |
| 154 | + pnpm publish -r --filter="./packages/**" --tag ${{ steps.version.outputs.tag }} --no-git-checks --access public > publish_output.txt 2>&1 |
| 155 | +
|
| 156 | + # Show the actual publish output for debugging |
| 157 | + echo "=== Full publish output ===" |
| 158 | + cat publish_output.txt |
| 159 | +
|
| 160 | + # Extract published versions and remove duplicates |
| 161 | + echo "=== Extracting published versions ===" |
| 162 | + grep -o '@cowprotocol/[^@]*@[0-9][^[:space:]]*' publish_output.txt | sort -u > published_versions.txt |
| 163 | + env: |
| 164 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 165 | + |
| 166 | + - name: Restore original versions (PR only) |
| 167 | + if: steps.version.outputs.is_pr == 'true' |
| 168 | + run: | |
| 169 | + # Restore original package.json versions |
| 170 | + git checkout -- packages/*/package.json |
| 171 | +
|
| 172 | + - name: Update PR comment with package info |
| 173 | + if: steps.version.outputs.is_pr == 'true' |
| 174 | + uses: actions/github-script@v7 |
| 175 | + with: |
| 176 | + script: | |
| 177 | + const prVersion = '${{ steps.version.outputs.version_suffix }}'; |
| 178 | + const isExternalPR = '${{ github.event.pull_request.head.repo.full_name }}' !== '${{ github.repository }}'; |
| 179 | +
|
| 180 | + // Read published versions from file |
| 181 | + const fs = require('fs'); |
| 182 | + let publishedVersions = []; |
| 183 | + try { |
| 184 | + const versionsContent = fs.readFileSync('published_versions.txt', 'utf8'); |
| 185 | + publishedVersions = versionsContent.trim().split('\n').filter(line => line.trim()); |
| 186 | + } catch (error) { |
| 187 | + console.log('Could not read published versions file'); |
| 188 | + process.exit(1); |
| 189 | + } |
| 190 | +
|
| 191 | + const now = new Date().toISOString(); |
| 192 | + const timestamp = new Date(now).toLocaleString('en-US', { |
| 193 | + timeZone: 'UTC', |
| 194 | + year: 'numeric', |
| 195 | + month: 'short', |
| 196 | + day: 'numeric', |
| 197 | + hour: '2-digit', |
| 198 | + minute: '2-digit', |
| 199 | + second: '2-digit', |
| 200 | + timeZoneName: 'short' |
| 201 | + }); |
| 202 | +
|
| 203 | + const commentBody = `## 📦 GitHub Packages Published |
| 204 | + **Last updated:** ${timestamp} |
| 205 | +
|
| 206 | + The following packages have been published to GitHub Packages with pre-release version \`${prVersion}\`: |
| 207 | +
|
| 208 | + ${publishedVersions.map(pkg => `- \`${pkg}\``).join('\n')} |
| 209 | +
|
| 210 | + ${isExternalPR ? '> **Note:** This is an external PR with the \`allow-publish\` label enabled.' : ''} |
| 211 | +
|
| 212 | + --- |
| 213 | +
|
| 214 | + ### Installation |
| 215 | +
|
| 216 | + These packages require authentication to install from GitHub Packages. First, configure your token: |
| 217 | +
|
| 218 | + \`\`\`bash |
| 219 | + # Set your GitHub token |
| 220 | + # 1. create one at https://github.com/settings/tokens. Make sure you select the option "Generate new token (classic)" |
| 221 | + # 2. Check only the "read:packages" scope |
| 222 | + # 3. Add the token to the npm config |
| 223 | + npm config set //${{ env.REGISTRY }}/:_authToken YOUR_GITHUB_TOKEN |
| 224 | + \`\`\` |
| 225 | +
|
| 226 | + Then install any of the packages above: |
| 227 | + \`\`\`bash |
| 228 | + # Yarn |
| 229 | + yarn add ${publishedVersions[0]} --registry https://${{ env.REGISTRY }} |
| 230 | +
|
| 231 | + # pnpm |
| 232 | + pnpm install ${publishedVersions[0]} --registry https://${{ env.REGISTRY }} |
| 233 | +
|
| 234 | + # NPM |
| 235 | + npm install ${publishedVersions[0]} --registry https://${{ env.REGISTRY }} |
| 236 | + \`\`\` |
| 237 | +
|
| 238 | + ### View Packages |
| 239 | +
|
| 240 | + You can view the published packages at: https://github.com/cowprotocol/cow-sdk/packages`; |
| 241 | +
|
| 242 | + // Find existing comment from this workflow |
| 243 | + const comments = await github.rest.issues.listComments({ |
| 244 | + owner: context.repo.owner, |
| 245 | + repo: context.repo.repo, |
| 246 | + issue_number: context.issue.number |
| 247 | + }); |
| 248 | +
|
| 249 | + const existingComment = comments.data.find(comment => |
| 250 | + comment.user.type === 'Bot' && |
| 251 | + comment.body.includes('📦 GitHub Packages Published') |
| 252 | + ); |
| 253 | +
|
| 254 | + if (existingComment) { |
| 255 | + // Update existing comment |
| 256 | + await github.rest.issues.updateComment({ |
| 257 | + owner: context.repo.owner, |
| 258 | + repo: context.repo.repo, |
| 259 | + comment_id: existingComment.id, |
| 260 | + body: commentBody |
| 261 | + }); |
| 262 | + console.log('Updated existing comment'); |
| 263 | + } else { |
| 264 | + // Create new comment if none exists |
| 265 | + await github.rest.issues.createComment({ |
| 266 | + owner: context.repo.owner, |
| 267 | + repo: context.repo.repo, |
| 268 | + issue_number: context.issue.number, |
| 269 | + body: commentBody |
| 270 | + }); |
| 271 | + console.log('Created new comment'); |
| 272 | + } |
0 commit comments