|
| 1 | +name: Publish to Hex.pm |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + version: |
| 9 | + description: 'Version to publish (e.g., 0.2.0)' |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Set up Elixir |
| 24 | + uses: erlef/setup-beam@v1 |
| 25 | + with: |
| 26 | + elixir-version: '1.17' |
| 27 | + otp-version: '27' |
| 28 | + |
| 29 | + - name: Restore dependencies cache |
| 30 | + uses: actions/cache@v4 |
| 31 | + with: |
| 32 | + path: deps |
| 33 | + key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} |
| 34 | + restore-keys: ${{ runner.os }}-mix- |
| 35 | + |
| 36 | + - name: Install dependencies |
| 37 | + run: mix deps.get |
| 38 | + |
| 39 | + - name: Update version |
| 40 | + if: github.event_name == 'workflow_dispatch' |
| 41 | + run: | |
| 42 | + sed -i "s/@version \".*\"/@version \"${{ inputs.version }}\"/" mix.exs |
| 43 | +
|
| 44 | + - name: Run tests |
| 45 | + run: mix test |
| 46 | + |
| 47 | + - name: Check formatting |
| 48 | + run: mix format --check-formatted |
| 49 | + |
| 50 | + - name: Configure git |
| 51 | + if: github.event_name == 'workflow_dispatch' |
| 52 | + run: | |
| 53 | + git config user.name "github-actions[bot]" |
| 54 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 55 | +
|
| 56 | + - name: Install git-cliff |
| 57 | + if: github.event_name == 'workflow_dispatch' |
| 58 | + uses: kenji-miyake/setup-git-cliff@v2 |
| 59 | + |
| 60 | + - name: Generate changelog |
| 61 | + if: github.event_name == 'workflow_dispatch' |
| 62 | + run: | |
| 63 | + git-cliff --tag "v${{ inputs.version }}" -o CHANGELOG.md |
| 64 | +
|
| 65 | + - name: Commit version and changelog |
| 66 | + if: github.event_name == 'workflow_dispatch' |
| 67 | + run: | |
| 68 | + git pull --rebase origin main |
| 69 | + git add mix.exs CHANGELOG.md |
| 70 | + if git diff --staged --quiet; then |
| 71 | + echo "No changes to commit" |
| 72 | + else |
| 73 | + git commit -m "Release v${{ inputs.version }}" |
| 74 | + git push |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Create tag and release |
| 78 | + if: github.event_name == 'workflow_dispatch' |
| 79 | + env: |
| 80 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + run: | |
| 82 | + if git rev-parse "v${{ inputs.version }}" >/dev/null 2>&1; then |
| 83 | + echo "Tag v${{ inputs.version }} already exists" |
| 84 | + else |
| 85 | + git tag "v${{ inputs.version }}" |
| 86 | + git push origin "v${{ inputs.version }}" |
| 87 | + fi |
| 88 | + if gh release view "v${{ inputs.version }}" >/dev/null 2>&1; then |
| 89 | + echo "Release v${{ inputs.version }} already exists" |
| 90 | + else |
| 91 | + gh release create "v${{ inputs.version }}" \ |
| 92 | + --title "v${{ inputs.version }}" \ |
| 93 | + --generate-notes |
| 94 | + fi |
| 95 | +
|
| 96 | + - name: Publish to Hex.pm |
| 97 | + env: |
| 98 | + HEX_API_KEY: ${{ secrets.HEX_API_KEY }} |
| 99 | + run: | |
| 100 | + mix hex.publish --yes |
0 commit comments