Skip to content

Commit

Permalink
CI: Automate publishing releases to hex.pm
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodirlam committed May 19, 2024
1 parent b2b4270 commit 88c855c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 61 deletions.
29 changes: 26 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
on: push

permissions:
contents: write

jobs:
checks:
name: Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
Expand All @@ -25,7 +28,7 @@ jobs:
MIX_ENV: test

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup Erlang & Elixir
uses: erlef/setup-beam@v1
Expand All @@ -35,15 +38,15 @@ jobs:

# Caching
- name: Dependencies cache
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-deps-${{ hashFiles('mix.lock') }}
restore-keys: |
${{ runner.os }}-deps-${{ hashFiles('mix.lock') }}
- name: Build cache
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: _build
key: ${{ runner.os }}-build-test-${{matrix.elixir}}-${{matrix.otp}}-${{ hashFiles('mix.lock') }}
Expand All @@ -65,3 +68,23 @@ jobs:

- name: Code format
run: mix format --check-formatted --dry-run

docs:
name: Docs
needs: [checks]
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/docs/')
uses: ./.github/workflows/publish-docs.yml
secrets: inherit
with:
elixir_version: 1.13.4
otp_version: 24.1.7

release:
name: Release
needs: [checks]
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/v')
uses: ./.github/workflows/publish-release.yml
secrets: inherit
with:
elixir_version: 1.13.4
otp_version: 24.1.7
55 changes: 0 additions & 55 deletions .github/workflows/docs.yml

This file was deleted.

49 changes: 49 additions & 0 deletions .github/workflows/publish-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
on:
workflow_call:
inputs:
elixir_version:
required: true
type: string
otp_version:
required: true
type: string

permissions:
contents: write

jobs:
publish-docs:
name: Publish
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4

- name: Setup Erlang & Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ inputs.elixir_version }}
otp-version: ${{ inputs.otp_version }}

# Caching
- name: Dependencies cache
uses: actions/cache/restore@v4
with:
path: deps
key: ${{ runner.os }}-deps-${{ hashFiles('mix.lock') }}

- name: Build cache
uses: actions/cache/restore@v4
with:
path: _build
key: ${{ runner.os }}-build-dev-${{inputs.elixir_version}}-${{inputs.otp_version}}-${{ hashFiles('mix.lock') }}

# Run
- name: Generate docs
run: mix docs

- name: Push docs
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: doc
28 changes: 28 additions & 0 deletions .github/workflows/publish-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# from https://github.com/elixir-mint/castore/blob/22d0a4efd41b97f55aab48e170f2520c338341b9/.github/workflows/publish.sh
# by Eric Meadows-Jönsson (@ericmj)

# Assume package name == repository name
package="${GITHUB_REPOSITORY#*/}"

git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

if [ $(git tag -l "v$(cat VERSION)") ]; then
echo "NOT TAGGING"
else
echo "TAGGING"
git tag "v$(cat VERSION)"
git push --tags
fi

if mix hex.info $package "$(cat VERSION)"; then
echo "NOT PUBLISHING"
else
echo "PUBLISHING"

# Assumes HEX_API_KEY is set in GitHub Actions secrets
# https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions
mix hex.publish --yes
fi
47 changes: 47 additions & 0 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# based on https://github.com/elixir-mint/castore/blob/22d0a4efd41b97f55aab48e170f2520c338341b9/.github/workflows/publish.yml
# by Eric Meadows-Jönsson (@ericmj)

on:
workflow_call:
inputs:
elixir_version:
required: true
type: string
otp_version:
required: true
type: string

jobs:
publish-release:
name: Publish
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Erlang & Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ inputs.elixir_version }}
otp-version: ${{ inputs.otp_version }}

# Caching
- name: Dependencies cache
uses: actions/cache/restore@v4
with:
path: deps
key: ${{ runner.os }}-deps-${{ hashFiles('mix.lock') }}

- name: Build cache
uses: actions/cache/restore@v4
with:
path: _build
key: ${{ runner.os }}-build-dev-${{inputs.elixir_version}}-${{inputs.otp_version}}-${{ hashFiles('mix.lock') }}

# Run
- name: Publish release
run: .github/workflows/publish-release.sh
env:
HEX_API_KEY: ${{ secrets.HEX_API_KEY }}
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
11 changes: 8 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ defmodule Refactory.MixProject do
use Mix.Project

@source_url "https://github.com/elixir-dx/refactory"
@version "0.1.0"

def project do
[
app: :refactory,
version: @version,
version: version(),
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
Expand Down Expand Up @@ -62,7 +61,7 @@ defmodule Refactory.MixProject do
[
main: "Refactory",
source_url: @source_url,
source_ref: "v#{@version}"
source_ref: "v#{version()}"
]
end

Expand All @@ -73,4 +72,10 @@ defmodule Refactory.MixProject do
test: ["ecto.reset", "test"]
]
end

defp version do
"VERSION"
|> File.read!()
|> String.trim()
end
end

0 comments on commit 88c855c

Please sign in to comment.