diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index e56ce31b..59ebeb64 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -2,7 +2,20 @@ name: Publish packages on NPM on: release: types: [released] - workflow_dispatch: {} # Allow for manual trigger. + workflow_dispatch: + inputs: + channel: + description: 'Publish channel (latest or dev)' + required: true + default: 'dev' + type: choice + options: + - latest + - dev + version: + description: 'Version to publish (leave empty to use current version in package.json)' + required: false + type: string jobs: publish: @@ -17,10 +30,78 @@ jobs: node-version: '24' registry-url: 'https://registry.npmjs.org' + - name: Validate and set channel + id: channel + run: | + # Defaults to 'latest' for release events, otherwise uses dispatch input (which defaults to 'dev'). + CHANNEL="${{ github.event.inputs.channel || 'latest' }}" + BRANCH="${{ github.ref_name }}" + + # Restrict 'latest' channel to master branch only + if [ "$CHANNEL" = "latest" ] && [ "$BRANCH" != "master" ]; then + echo "Error: The 'latest' channel can only be published from the 'master' branch." + echo "Current branch: $BRANCH" + echo "Use the 'dev' channel for publishing from other branches." + exit 1 + fi + + echo "channel=$CHANNEL" >> $GITHUB_OUTPUT + echo "Publishing to channel: $CHANNEL from branch: $BRANCH" + + - name: Validate and set version + id: version + run: | + VERSION="${{ github.event.inputs.version }}" + CHANNEL="${{ steps.channel.outputs.channel }}" + BRANCH="${{ github.ref_name }}" + + # If version is provided, validate it + if [ -n "$VERSION" ]; then + echo "Using provided version: $VERSION" + + # Restrict version override on master branch to non-latest channels only + if [ "$BRANCH" = "master" ] && [ "$CHANNEL" = "latest" ]; then + echo "Error: Cannot provide a custom version when publishing to 'latest' channel from the 'master' branch." + echo "For 'latest' channel on master, the version from package.json must be used." + exit 1 + fi + + # For dev channel, verify version ends with -dev. + if [ "$CHANNEL" = "dev" ]; then + if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+-dev\.[0-9]+$'; then + echo "Error: Dev channel versions must end with -dev. (e.g., 1.0.0-dev.0)" + exit 1 + fi + fi + + echo "version=$VERSION" >> $GITHUB_OUTPUT + echo "has_version=true" >> $GITHUB_OUTPUT + else + echo "No version provided, will use current package.json version" + echo "has_version=false" >> $GITHUB_OUTPUT + fi + - run: yarn + + - name: Set version if provided + if: steps.version.outputs.has_version == 'true' + run: | + VERSION="${{ steps.version.outputs.version }}" + echo "Setting version to: $VERSION" + yarn version:all "$VERSION" - run: yarn workspace @datadog/rollup-plugin buildBasic - run: export BUILD_PLUGINS_ENV=production - - run: yarn publish:all + + - name: Publish to NPM + run: | + CHANNEL="${{ steps.channel.outputs.channel }}" + if [ "$CHANNEL" = "dev" ]; then + echo "Publishing to dev channel with --tag dev" + yarn loop --no-private npm publish --tag dev + else + echo "Publishing to latest channel" + yarn loop --no-private npm publish + fi env: ADD_BUILD_PLUGINS: 1 DD_GITHUB_JOB_NAME: Publish to NPM # Needs to be the same as the job to have CI Vis link the spans. @@ -28,6 +109,7 @@ jobs: - name: Log version published run: | VERSION="$(yarn workspace @datadog/webpack-plugin info @datadog/webpack-plugin --json | jq -r '.children.Version')" + CHANNEL="${{ steps.channel.outputs.channel }}" HEADERS=( -H "Content-Type: application/json" -H "X-Datadog-Origin: build-plugins" @@ -36,11 +118,12 @@ jobs: DATA="{ \"ddsource\": \"github\", \"service\": \"build-plugins\", - \"message\": \"Latest version published: $VERSION\", + \"message\": \"Version published to $CHANNEL channel: $VERSION\", \"status\": \"success\", \"env\": \"production\", \"team\": \"language-foundations\", - \"version\": \"$VERSION\" + \"version\": \"$VERSION\", + \"channel\": \"$CHANNEL\" }" URL="https://http-intake.logs.datadoghq.com/api/v2/logs" curl -X POST "${HEADERS[@]}" -d "$DATA" "$URL" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8d2f003b..505b789d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,10 @@ - [Open Source compliance](#open-source-compliance) - [Documentation](#documentation) - [Publish a new public version](#publish-a-new-public-version) + - [Recommended: Using GitHub Release (Automatic)](#recommended-using-github-release-automatic) + - [Alternative: Using Workflow Dispatch (Manual)](#alternative-using-workflow-dispatch-manual) - [Publish a new dev version](#publish-a-new-dev-version) + - [Using GitHub Actions Workflow](#using-github-actions-workflow) - [Work with Datadog's Frontend monorepo](#work-with-datadogs-frontend-monorepo) - [Requirements](#requirements) - [Develop on both our monorepo and `build-plugins` locally](#develop-on-both-our-monorepo-and-build-plugins-locally) @@ -293,6 +296,8 @@ We try to keep the documentation as up to date as possible. ## Publish a new public version +### Recommended: Using GitHub Release (Automatic) + 1. Run [this action](https://github.com/DataDog/build-plugins/actions/workflows/bump.yaml) with the type of bump you need (`patch`, `minor`, `major`) and keep the `master` branch as target. ![Bump workflow](/packages/assets/src/publish/run-workflow.png) @@ -323,27 +328,44 @@ We try to keep the documentation as up to date as possible. Usually, after 4-5min, your version will be available from NPM. +### Alternative: Using Workflow Dispatch (Manual) -## Publish a new dev version +You can also manually trigger the [publish workflow](https://github.com/DataDog/build-plugins/actions/workflows/publish.yaml) from the `master` branch: -You can publish a version in the `dev` channel so you can easily test your changes in a different repository's CI: +1. Ensure you're on the `master` branch +2. Update the version in package.json if needed (via the bump workflow or manually) +3. Go to the [publish workflow](https://github.com/DataDog/build-plugins/actions/workflows/publish.yaml) +4. Click "Run workflow" +5. Select the `master` branch +6. Set `channel` to `latest` +7. Leave `version` empty (it will use the version from package.json) +8. Run the workflow -1. First you need to bump the version with a marker for the channel, ex: `0.4.2-dev.0` so we don't occupy a version of the `latest` channel. +> [!IMPORTANT] +> When publishing to the `latest` channel from `master`, you cannot provide a custom version, the workflow will use the version from package.json to ensure version integrity. -```bash -# Set your dev version locally (you may need to run it twice to circonvent a yarn bug) -yarn version:all 0.4.2-dev -``` -2. Then publish the packages (you'll need an NPM token for this): +## Publish a new dev version -```bash -# First add your write token -yarn config set npmAuthToken $NPM_WRITE_TOKEN +You can publish a version in the `dev` channel so you can easily test your changes in a different repository's CI. -# Publish everything to the dev channel -yarn publish:all --tag=dev -``` +### Using GitHub Actions Workflow + +The easiest way to publish a dev version is using the GitHub Actions workflow: + +1. Go to the [publish workflow](https://github.com/DataDog/build-plugins/actions/workflows/publish.yaml) +2. Click "Run workflow" +3. Select your feature branch (or `master` if publishing from there) +4. Set `channel` to `dev` +5. Set `version` to your dev version (e.g., `3.0.9-dev.0`) + - The version **must** follow the format `X.Y.Z-dev.N` + - For example: `3.0.9-dev.0`, `3.0.9-dev.1`, etc. +6. Run the workflow + +> [!NOTE] +> - Dev versions can be published from any branch +> - You can provide a custom version when publishing to the `dev` channel +> - The workflow will validate the version format and fail if it doesn't match `X.Y.Z-dev.N` Wait a few minutes, and you're good to go. @@ -398,21 +420,14 @@ This will update the versions of all the plugins we use in the monorepo (webpack ### Publish a dev/alpha/beta version of the plugins to consume in Datadog's Frontend monorepo -If you want to test your `build-plugins`'s changes in our monorepo's CI, you can publish a dev version of the plugins: +If you want to test your `build-plugins`'s changes in our monorepo's CI, you can publish a dev version of the plugins. -```bash -# Use a version with a marker for the channel, ex: 2.5.1-dev-0 -yarn version:all 2.5.1-dev-0 - -# Publish everything to the dev channel -# You will need $NPM_DD_WRITE_TOKEN set in your environment -YARN_NPM_AUTH_TOKEN=$NPM_DD_WRITE_TOKEN yarn publish:all --tag=dev -``` +Follow the instructions in [Publish a new dev version](#publish-a-new-dev-version) to publish your dev version (e.g., `2.5.1-dev.0`). -Once published, in the repository: +Once published, in the monorepo: ```bash -yarn update-build-plugins 2.5.1-dev-0 +yarn update-build-plugins 2.5.1-dev.0 ``` Commit and push the changes.