From 88c855c7dc26f946ee71520952a3558c77137d1e Mon Sep 17 00:00:00 2001 From: Arno Dirlam Date: Sun, 19 May 2024 12:50:29 +0200 Subject: [PATCH] CI: Automate publishing releases to hex.pm --- .github/workflows/ci.yml | 29 ++++++++++++-- .github/workflows/docs.yml | 55 --------------------------- .github/workflows/publish-docs.yml | 49 ++++++++++++++++++++++++ .github/workflows/publish-release.sh | 28 ++++++++++++++ .github/workflows/publish-release.yml | 47 +++++++++++++++++++++++ VERSION | 1 + mix.exs | 11 ++++-- 7 files changed, 159 insertions(+), 61 deletions(-) delete mode 100644 .github/workflows/docs.yml create mode 100644 .github/workflows/publish-docs.yml create mode 100755 .github/workflows/publish-release.sh create mode 100644 .github/workflows/publish-release.yml create mode 100644 VERSION diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66e01d5..b1bae12 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,8 @@ on: push +permissions: + contents: write + jobs: checks: name: Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}} @@ -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 @@ -35,7 +38,7 @@ jobs: # Caching - name: Dependencies cache - uses: actions/cache@v1 + uses: actions/cache@v4 with: path: deps key: ${{ runner.os }}-deps-${{ hashFiles('mix.lock') }} @@ -43,7 +46,7 @@ jobs: ${{ 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') }} @@ -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 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml deleted file mode 100644 index 8236de9..0000000 --- a/.github/workflows/docs.yml +++ /dev/null @@ -1,55 +0,0 @@ -on: - push: - branches: - - main - - 'docs/**' - -jobs: - publish-docs: - name: Publish docs - runs-on: ubuntu-20.04 - - strategy: - matrix: - elixir: [1.13.4] - otp: [24.1.7] - - steps: - - uses: actions/checkout@v2 - - - name: Setup Erlang & Elixir - uses: erlef/setup-beam@v1 - with: - elixir-version: ${{ matrix.elixir }} - otp-version: ${{ matrix.otp }} - - # Caching - - name: Dependencies cache - uses: actions/cache@v1 - 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 - with: - path: _build - key: ${{ runner.os }}-build-dev-${{matrix.elixir}}-${{matrix.otp}}-${{ hashFiles('mix.lock') }} - restore-keys: | - ${{ runner.os }}-build-dev-${{matrix.elixir}}-${{matrix.otp}}- - - # Setup - - name: Install dependencies - run: mix deps.get - - # Run - - name: Generate docs - run: mix docs - - - name: Push docs - uses: JamesIves/github-pages-deploy-action@v4.2.5 - with: - branch: gh-pages - folder: doc \ No newline at end of file diff --git a/.github/workflows/publish-docs.yml b/.github/workflows/publish-docs.yml new file mode 100644 index 0000000..86c46e7 --- /dev/null +++ b/.github/workflows/publish-docs.yml @@ -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 diff --git a/.github/workflows/publish-release.sh b/.github/workflows/publish-release.sh new file mode 100755 index 0000000..a944b3f --- /dev/null +++ b/.github/workflows/publish-release.sh @@ -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 diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 0000000..aa5a664 --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -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 }} diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..6c6aa7c --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.0 \ No newline at end of file diff --git a/mix.exs b/mix.exs index 43d4e53..9453316 100644 --- a/mix.exs +++ b/mix.exs @@ -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()), @@ -62,7 +61,7 @@ defmodule Refactory.MixProject do [ main: "Refactory", source_url: @source_url, - source_ref: "v#{@version}" + source_ref: "v#{version()}" ] end @@ -73,4 +72,10 @@ defmodule Refactory.MixProject do test: ["ecto.reset", "test"] ] end + + defp version do + "VERSION" + |> File.read!() + |> String.trim() + end end