Giter VIP home page Giter VIP logo

semantic-release-replace-plugin's Introduction

Semantic Release Replace Plugin

npm Build Release codecov GitHub contributors semantic-release

The semantic-release-replace-plugin plugin provides functionality to update version strings throughout a project. This enables semantic release to be used in many different languages and build processes.

Read more about Semantic Release.

Install

$ npm install semantic-release-replace-plugin -D

Usage

The following example uses this plugin to demonstrate using semantic-release in a Python package where __VERSION__ is defined in the root __init__.py file.

{
  "plugins": [
    "@semantic-release/commit-analyzer",
    [
      "semantic-release-replace-plugin",
      {
        "replacements": [
          {
            "files": ["foo/__init__.py"],
            "from": "__VERSION__ = \".*\"",
            "to": "__VERSION__ = \"${nextRelease.version}\"",
            "results": [
              {
                "file": "foo/__init__.py",
                "hasChanged": true,
                "numMatches": 1,
                "numReplacements": 1
              }
            ],
            "countMatches": true
          }
        ]
      }
    ],
    [
      "@semantic-release/git",
      {
        "assets": ["foo/*.py"]
      }
    ]
  ]
}

Validation

The presence of the results array will trigger validation that a replacement has been made. This is optional but recommended.

Warning

This plugin will not commit changes unless you specify assets for the @semantic-release/git plugin! This is highlighted below.

[
  "@semantic-release/git",
  {
    "assets": ["foo/*.py"]
  }
]

Options

Please refer to the documentation for more options.

semantic-release-replace-plugin's People

Contributors

bauglir avatar ben-hampson avatar borduhh avatar dependabot[bot] avatar jpoehnelt avatar semantic-release-bot avatar sobolevn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

semantic-release-replace-plugin's Issues

Increment value

Not an issue but more of a question:
Could this be used to increment a value, for example a build number?
Something like

  "from": "__BUILD__ = \"(\d+)\"",
  "to": "__BUILD__ = \"{$1+1}\"",

I'm not sure what the syntax would be...
Thanks!

Cannot find module 'semantic-release-replace-plugin'

Since version 1.2.1 and later I get the following error in my GitHub workflow that runs semantic release:

[5:08:25 AM] [semantic-release] › ℹ  Running semantic-release version 19.0.5
[5:08:25 AM] [semantic-release] › ✖  An error occurred while running semantic-release: Error: Cannot find module 'semantic-release-replace-plugin'
Require stack:
- /home/runner/work/ndb-core/ndb-core/noop.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at resolveFileName (/home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/resolve-from/index.js:29:39)
    at resolveFrom (/home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/resolve-from/index.js:43:9)
    at module.exports (/home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/resolve-from/index.js:46:47)
    at loadPlugin (/home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/semantic-release/lib/plugins/utils.js:51:82)
    at /home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/semantic-release/lib/plugins/index.js:17:37
    at Array.reduce (<anonymous>)
    at module.exports (/home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/semantic-release/lib/plugins/index.js:[14](https://github.com/Aam-Digital/ndb-core/actions/runs/5482968601/jobs/9988838364#step:4:15):34)
    at module.exports (/home/runner/work/_actions/cycjimmy/semantic-release-action/v3/node_modules/semantic-release/lib/get-config.js:84:35)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/runner/work/ndb-core/ndb-core/noop.js' ]
}

I had to downgrade to @google/[email protected]to make it work again.
Maybe this error has something to do with Node 14 which I have to use in order to fix this issue

My (failing) workflow:

  run-semantic-release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          persist-credentials: false
      # Only required temporary: https://github.com/cycjimmy/semantic-release-action/issues/159
      - uses: actions/setup-node@v3
        with:
          node-version: 14
      - name: Semantic Release
        uses: cycjimmy/semantic-release-action@v3
        env:
          GITHUB_TOKEN: ***
          SLACK_WEBHOOK: ***
        with:
          extra_plugins: |
            semantic-release-replace-plugin
            @semantic-release/git
            @semantic-release/github
            semantic-release-slack-bot

Conditional replace file

Hi,

I´m, using this fantatic plugin to generate automated version from my Terraform modules and change automatically the version in locals.tf file with the current version Release.

All this is working perfectly, but I have a problem and I need to implement the change of 2 values in the same file, but there are times that the search for the pattern 2 may or may not be there.

Terraform locals.tf file

 module_tags = {
   "tf:Used"          = "True"
   "tf:Module"        = "module-base-iam-policy"
   "tf:ModuleVersion" = "2.0.3"
 }

 submodule_tags = {
   "tf:Used"                = "True"
   "tf:ParentModule"        = "module-base-iam-policy"
   "tf:ParentModuleVersion" = "2.0.3"
 }
}

@google/semantic-release-replace-plugin

["@google/semantic-release-replace-plugin", {
 "replacements": [
     {
         "files": ["locals.tf"],
         "from": "\"tf:ModuleVersion\" = \".*\"",
         "to": "\"tf:ModuleVersion\" = \"${nextRelease.version}\"",
         "results": [{
             "file": "locals.tf",
             "hasChanged": true,
             "numMatches": 1,
             "numReplacements": 1
         }],
         "countMatches": true
     },
     {
         "files": ["locals.tf"],
         "from": "\"tf:ParentModuleVersion\" = \".*\"",
         "to": "\"tf:ParentModuleVersion\" = \"${nextRelease.version}\"",
         "results": [{
             "file": "locals.tf",
             "hasChanged": true,
             "numMatches": 1,
             "numReplacements": 1
         }],
         "countMatches": true
     }
 ]
}]

The plugin works correctly and makes the change in the 2 patterns provided, but the problem comes in that not all terraform modules carry the local variable submodule_tag.local. So if I have configured to look for the 2 options, if the module has the 2 options it works correctly, but if the module has only one option it fails `Error: Expected match not found!`

Terraform locals.tf file

locals {
 module_tags = {
   "tf:Used"          = "True"
   "tf:Module"        = "module-base-iam-policy"
   "tf:ModuleVersion" = "2.0.3"
 }
}

It would be nice to implement a condition that if you pass a value for example `"optionalFile": true` that if it does not find the parameter to make the replacement, it does not give an error and passes successfully.

@google/semantic-release-replace-plugin

["@google/semantic-release-replace-plugin", {
   "replacements": [
       {
           "files": ["locals.tf"],
           "from": "\"tf:ModuleVersion\" = \".*\"",
           "to": "\"tf:ModuleVersion\" = \"${nextRelease.version}\"",
           "results": [{
               "file": "locals.tf",
               "hasChanged": true,
               "numMatches": 1,
               "numReplacements": 1
           }],
           "countMatches": true
       },
       {
           "files": ["locals.tf"],
           "optionalFile": true,
           "from": "\"tf:ParentModuleVersion\" = \".*\"",
           "to": "\"tf:ParentModuleVersion\" = \"${nextRelease.version}\"",
           "results": [{
               "file": "locals.tf",
               "hasChanged": true,
               "numMatches": 1,
               "numReplacements": 1
           }],
           "countMatches": true
       }
   ]
}]

This will allow to configure the replacement of several parameters, and if one does not exist, to specify it so that it does not give an error.

Thank you very much, I hope you can help me with this implementation that I think will be of great value to the community.

Javier Torres

matches not found in google action

I am trying to use semantic-release-replace-plugin to change version in pyproject.toml and src/demo/__init__.py In the first I have:

[tool.poetry]
...
version = "0.1.5"

In the second:

__version__ = "0.1.5"

My semantic-release config:

branches:
  - main
  - {name: alpha, prerelease: true}
  - {name: beta, prerelease: true}
plugins:
  - "@semantic-release/commit-analyzer"
  - "@semantic-release/release-notes-generator"
  - - "@google/semantic-release-replace-plugin"
    - replacements:
      - files:
        - pyproject.toml
        from: 'version = ".*"'
        to: 'version = "${nextRelease.version}"'
        results:
          - file: pyproject.toml
            hasChanged: true
            numMatches: 1
            numReplacements: 1
        countMatches: true
      - files:
        - src/demo/__init__.py
        from: '__VERSION__ = ".*"'
        to: '__VERSION__ = "${nextRelease.version}"'
        results:
          - file: src/demo/__init__.py
            hasChanged: true
            numMatches: 1
            numReplacements: 1
        countMatches: true
  - - "@semantic-release/git"
    - assets:
      - pyproject.toml
      - src/demo/*.py
  - "@semantic-release/github"

I run using:

  Release:
    needs: Quality
    runs-on: ubuntu-latest
    concurrency: release
    # https://github.community/t/how-do-i-specify-job-dependency-running-in-another-workflow/16482
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' && !contains(github.event.head_commit.message, 'chore(release):')

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 14
      - name: Install semantic-release
        run: npm install -g semantic-release @semantic-release/github @semantic-release/commit-analyzer @semantic-release/git @semantic-release/release-notes-generator "@google/semantic-release-replace-plugin"
        # semantic-release-pypi
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install setuptools
        run: python -m pip install --upgrade setuptools wheel twine
      - name: Release
        env:
          GITHUB_TOKEN: ${{ github.token }}
          # PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
        run: npx semantic-release

This gives me the error below. It would seem that "expected" is no matches, but actual results in match. However, I specify that I do expect a match. (??) Also why does the error message read "expected match not found" when the detail is consistent with "unexpected match found"?

[5:17:45 PM] [semantic-release] › ✖  An error occurred while running semantic-release: Error: Expected match not found!
- Expected
+ Received

  Array [
    Object {
-     "file": "src/demo/__init__.py",
-     "hasChanged": false,
-     "numMatches": 0,
-     "numReplacements": 0,
+     "file": "demo/__init__.py",
+     "hasChanged": true,
+     "numMatches": 1,
+     "numReplacements": 1,
    },
  ]
    at /opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:98:35
    at step (/opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:59:23)
    at Object.next (/opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:40:53)
    at fulfilled (/opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:31:58) {
  pluginName: '@google/semantic-release-replace-plugin'
}
Error: Expected match not found!
- Expected
+ Received

  Array [
    Object {
-     "file": "src/demo/__init__.py",
-     "hasChanged": false,
-     "numMatches": 0,
-     "numReplacements": 0,
+     "file": "demo/__init__.py",
+     "hasChanged": true,
+     "numMatches": 1,
+     "numReplacements": 1,
    },
  ]
    at /opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:98:35
    at step (/opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:59:23)
    at Object.next (/opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:40:53)
    at fulfilled (/opt/hostedtoolcache/node/14.19.0/x64/lib/node_modules/@google/semantic-release-replace-plugin/dist/index.js:31:58) {
  pluginName: '@google/semantic-release-replace-plugin'
}
Error: Process completed with exit code 1.

Release `1.2.7` should be a major release, or at the very least a minor

Release 1.2.7 changes to require engine node >= 18 AND peerDependencies: semantic-release >= 20.1.0, this is (at least in my eyes) a breaking change. Because before nodejs 12 and everything would still work perfectly fine, but now package managers may automatically update to the latest patch version, likely yelling at you and maybe even exiting with a non-0 exit code because it couldnt satisfy the requirements

Consider removing that release and release a 2.0.0 instead, or at the very least a 1.3.0

Error [ERR_REQUIRE_ESM]: require() of ES Module

Hi everyone!

running semantic-release in dry-run mode is raising the following error:

[11:29:37 PM] [semantic-release] › ℹ  Running semantic-release version 21.0.7
[11:29:37 PM] [semantic-release] › ✘  An error occurred while running semantic-release: Error [ERR_REQUIRE_ESM]: require() of ES Module /home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/lodash-es/lodash.js from /home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/semantic-release-replace-plugin/dist/index.js not supported.
Instead change the require of lodash.js in /home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/semantic-release-replace-plugin/dist/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/semantic-release-replace-plugin/dist/index.js:70:19) {
  code: 'ERR_REQUIRE_ESM'
}
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/lodash-es/lodash.js from /home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/semantic-release-replace-plugin/dist/index.js not supported.
Instead change the require of lodash.js in /home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/semantic-release-replace-plugin/dist/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/xmn/.npm/_npx/1bba8fa66517c824/node_modules/semantic-release-replace-plugin/dist/index.js:70:19) {
  code: 'ERR_REQUIRE_ESM'
} 

I am not super familiar with nodejs ... any idea about what is happening?
thanks!

Error running on Gitlab CI

Hello,

I'm trying to run this for a Unity project hosted on Gitlab and am running into the following error.

[2:09:17 AM] [semantic-release] › ℹ  Start step "prepare" of plugin "@google/semantic-release-replace-plugin"
[2:09:17 AM] [semantic-release] › ✖  Failed step "prepare" of plugin "@google/semantic-release-replace-plugin"
[2:09:17 AM] [semantic-release] › ✖  An error occurred while running semantic-release: Error: Results differed from actual! 
- Expected
+ Received
  Array [
    Object {
      "file": "ProjectSettings/ProjectSettings.asset",
-     "hasChanged": true,
-     "numMatches": 1,
-     "numReplacements": 1,
+     "hasChanged": false,
+     "numMatches": 0,
+     "numReplacements": 0,
    },
  ]
    at /builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:94:35
    at step (/builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:59:23)
    at Object.next (/builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:40:53)
    at fulfilled (/builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:31:58) {
  pluginName: '@google/semantic-release-replace-plugin'
}
Error: Results differed from actual! 
- Expected
+ Received
  Array [
    Object {
      "file": "ProjectSettings/ProjectSettings.asset",
-     "hasChanged": true,
-     "numMatches": 1,
-     "numReplacements": 1,
+     "hasChanged": false,
+     "numMatches": 0,
+     "numReplacements": 0,
    },
  ]
    at /builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:94:35
    at step (/builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:59:23)
    at Object.next (/builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:40:53)
    at fulfilled (/builds/amnh/wiam-test/node_modules/@google/semantic-release-replace-plugin/dist/index.js:31:58) {
  pluginName: '@google/semantic-release-replace-plugin'
}
ERROR: Job failed: exit code 1

My .releaserc file looks like this, I basically copied the example and replaced the file and version field:

{
  "branches": ["master"],
  "tagFormat": "v${version}",
  "plugins": [
    [
      "@semantic-release/commit-analyzer",
      {
        "preset": "angular"
      }
    ],
    "@semantic-release/release-notes-generator",
    [
      "@semantic-release/changelog",
      {
        "preset": "angular",
        "changelogFile": "CHANGELOG.md"
      }
    ],
    [
      "@google/semantic-release-replace-plugin",
      {
        "replacements": [
          {
            "files": ["ProjectSettings/ProjectSettings.asset"],
            "from": "bundleVersion: \".*\"",
            "to": "bundleVersion: \"${nextRelease.version}\"",
            "results": [
              {
                "file": "ProjectSettings/ProjectSettings.asset",
                "hasChanged": true,
                "numMatches": 1,
                "numReplacements": 1
              }
            ],
            "countMatches": true
          }
        ]
      }
    ],
    [
      "@semantic-release/git",
      {
        "assets": ["ProjectSettings/ProjectSettings.asset", "CHANGELOG.md"],
        "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
      }
    ]
  ]
}

I'm not sure what that error means, any idea what is going wrong (and is it on my end)?

Thank you!

Multiline mode

Right now it is impossible to write your from regex as ^version = ... because regex is not in multiline mode.
Source: https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L108

Right now both these lines match "from": "version = \".*\"", regex:

[tool.poetry]
version = "1.4.0"

and

[tool.poetry.dependencies]
django-reversion = "^3.0"

I know that I can rewrite my from regex to be stricter on version value, but the easiest way is ^version = \".*\", which is impossible right now.
I also don't want to write something like version = \"\d+\.\d+\.\d+\" because:

  1. It still can match django-reversion = "3.0.1" if I ever want to pin it
  2. It won't match -pre and -beta suffixes and I don't want to make this regex overly complex or sensetive

Feature request to target only specific branches

Hello guys,

thanks for the brilliant plugin.

We are trying to target the changes only for a specific branch. My suggestion would be to have.

        - files:
            - AssemblyInfo.cs
          from: '\[assembly: AssemblyVersion\(".+"\)\]'
          to: '[assembly: AssemblyVersion("${nextRelease.version}")]'
          branches:
            - master
          results:
            - file: AssemblyInfo.cs
              hasChanged: true
              numMatches: 1
              numReplacements: 1
          countMatches: true

Is there a workaround for this right now?


Why do we need that?

Basically C# has to different types of assembly version information:
AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion

AssemblyVersion and AssemblyFileVersion are for end releases and accept only major, minor and patch only as number, for example: 1.2.3, BUT NOT 1.2.3-prelrelease.1

AssemblyInformationalVersion on the other hand accepts major, minor and patch with discriptive information, for example: 1.2.3, AND 1.2.3-prelrelease.1

So we want to change AssemblyVersion and AssemblyFileVersion only when master is the target branch.

We use yaml.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.