|
| 1 | +# Setting up an existing Elixir project |
| 2 | + |
| 3 | +## Before we begin |
| 4 | + |
| 5 | +`semantic-release` uses git tags to determine the current version of a project, so you must make sure your latest version is tagged (and pointing to the right commit). |
| 6 | + |
| 7 | +If you are using a different tag format than `v${version}`, see the [`tagFormat` option](https://semantic-release.gitbook.io/semantic-release/usage/configuration#tagformat). |
| 8 | + |
| 9 | +## Install Node.js |
| 10 | + |
| 11 | +You'll need Node.js `>=18.17`. |
| 12 | + |
| 13 | +The recommended way to install it is with the [`asdf`](https://asdf-vm.com/) version manager (which also supports Elixir) by adding it to your [`.tool-versions`](./.tool-versions) and running `asdf install`. |
| 14 | + |
| 15 | +Otherwise, you can [download it from nodejs.org](https://nodejs.org/en/download/). |
| 16 | + |
| 17 | +## Install the tools |
| 18 | + |
| 19 | +Install the required libraries: |
| 20 | + |
| 21 | +```shell |
| 22 | +npm i --save-dev semantic-release @semantic-release/changelog @semantic-release/git semantic-release-hex |
| 23 | +``` |
| 24 | + |
| 25 | +Add `node_modules` to your `.gitignore`: |
| 26 | + |
| 27 | +```shell |
| 28 | +echo -e "\n# Node.js dependencies directory\nnode_modules" >> .gitignore |
| 29 | +``` |
| 30 | + |
| 31 | +## Configuring `semantic-release` |
| 32 | + |
| 33 | +`semantic-release` is [modular](https://semantic-release.gitbook.io/semantic-release/extending/plugins-list), [extendable](https://semantic-release.gitbook.io/semantic-release/developer-guide/plugin) and [configurable to your preferences](https://semantic-release.gitbook.io/semantic-release/usage/configuration). Here we'll setup a workflow for the `main` branch with the following steps: |
| 34 | + |
| 35 | +1. Analyze the commits to determine the version bump type (if any) |
| 36 | +2. Generate release notes from the commits |
| 37 | +3. Write the release notes to the `CHANGELOG.md` file |
| 38 | +4. Bump the version in `mix.exs` |
| 39 | +5. Create and push a release commit (with the updated `CHANGELOG.md` and `mix.exs` files) |
| 40 | +6. Create a release tag |
| 41 | +7. Create a GitHub release |
| 42 | + |
| 43 | +There's [a few ways](https://semantic-release.gitbook.io/semantic-release/usage/configuration#configuration-file) to configure `semantic-release`, but we'll chose to do it in `package.json` to avoid creating an extra file. Add a `release` key as follow: |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "devDependencies": {...}, |
| 48 | + |
| 49 | + "release": { |
| 50 | + "branches": ["main"], |
| 51 | + "plugins": [ |
| 52 | + "@semantic-release/commit-analyzer", |
| 53 | + "@semantic-release/release-notes-generator", |
| 54 | + "@semantic-release/changelog", |
| 55 | + "semantic-release-hex", |
| 56 | + [ |
| 57 | + "@semantic-release/git", |
| 58 | + { |
| 59 | + "assets": ["CHANGELOG.md", "mix.exs"], |
| 60 | + "message": "chore(release): v${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" |
| 61 | + } |
| 62 | + ], |
| 63 | + "@semantic-release/github" |
| 64 | + ] |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +_Note: if you want your CHANGELOG to include all commit types (and add a bit of color with category emojis) like the one in this demo, checkout [`@insurgent/conventional-changelog-preset`](https://github.com/insurgent-lab/conventional-changelog-preset#readme)_. |
| 70 | + |
| 71 | +# CI configuration (GitHub Actions) |
| 72 | + |
| 73 | +See [`semantic-release` documentation on CI configuration](https://semantic-release.gitbook.io/semantic-release/recipes/ci-configurations) for general information. |
| 74 | + |
| 75 | +See [`examples/release.yml`](./examples/release.yml) and [`examples/test.yml`](./examples/test.yml) for example workflows in an Elixir project. |
| 76 | + |
| 77 | +**If you use branch protection:** See comments in example release workflow. To generate the `CI_GITHUB_TOKEN` secret needed to bypass branch protection when pushing the release commit, see [this StackOverflow answer](https://stackoverflow.com/questions/74744498/github-pushing-to-protected-branches-with-fine-grained-token/76550826#76550826) on how to create a fine-grained token with the right scopes (I know, I really need to [update the docs](https://github.com/semantic-release/semantic-release/issues/2883)). |
| 78 | + |
| 79 | +# Running from the CLI |
| 80 | + |
| 81 | +_Note: If you're concerned about security, you can run `gh auth refresh` after using `gh auth token` to regenerate [`gh`](https://cli.github.com/)'s token and invalidate the previous one (or you can create a fine-grained token by visiting the StackOverflow link in [CI configuration](#ci-configuration-github-actions))._ |
| 82 | + |
| 83 | +## To test everything is working |
| 84 | + |
| 85 | +```shell |
| 86 | +GH_TOKEN=$(gh auth token) npx semantic-release --dry-run |
| 87 | +``` |
| 88 | + |
| 89 | +## To make an actual release |
| 90 | + |
| 91 | +```shell |
| 92 | +GH_TOKEN=$(gh auth token) npx semantic-release --no-ci |
| 93 | +``` |
| 94 | + |
| 95 | +_Note: if your git is configured to automatically sign tags, this won't work [due to a known issue](https://github.com/semantic-release/semantic-release/issues/3065). Run `git config tag.gpgSign false` in your repo first._ |
0 commit comments