TrainRun.jl/.github/workflows/release.yml

333 lines
14 KiB
YAML

name: create new release
on:
workflow_dispatch:
inputs:
version:
description: "Version to register (semantic versioning schema, e.g. '1.0.1'):"
required: true
jobs:
version:
name: ":triangular_flag_on_post: check submitted version - "
runs-on: ubuntu-latest
outputs:
current: ${{ steps.version_check.outputs.current }}
new: ${{ steps.version_check.outputs.new }}
steps:
- name: "checkout"
uses: actions/checkout@v3
- name: "check version number"
id: version_check
run: |
VERSION=${{ github.event.inputs.version }}
echo "Version to register '$VERSION':" >> $GITHUB_STEP_SUMMARY
## =================================
## check if version follows the semantic version pattern
## ---------------------------------
#remove a trailing 'v' if it exists
if [[ ${VERSION:0:1} -eq "v" ]]; then
VERSION=${VERSION:1}
fi
# check with regular expressions
SEM_VER_REGEX="^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$"
STATUS=0
if [[ ! $VERSION =~ $SEM_VER_REGEX ]]; then
STATUS=1
fi
if [ $STATUS = 1 ]; then
echo "- :no_entry_sign: format does not follow the semantic versioning schema" >> $GITHUB_STEP_SUMMARY
echo ":warning: Please see https://semver.org/ for further information." >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "- format follows the semantic versioning schema :white_check_mark:" >> $GITHUB_STEP_SUMMARY
fi
## =================================
## check if version is an increment
## ---------------------------------
VERSION_MAJOR=$(echo $VERSION | cut -d. -f1 )
VERSION_MINOR=$(echo $VERSION | cut -d. -f2 )
VERSION_PATCH=$(echo $VERSION | cut -d. -f3 )
CURRENT_VERSION=$(grep -n -m 1 "## Version \[*.*.*\]" CHANGELOG.md | cut -d[ -f2 | cut -d] -f1)
CURRENT_VERSION_MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1 )
CURRENT_VERSION_MINOR=$(echo $CURRENT_VERSION | cut -d. -f2 )
CURRENT_VERSION_PATCH=$(echo $CURRENT_VERSION | cut -d. -f3 )
if [[ $VERSION_MAJOR -eq CURRENT_VERSION_MAJOR ]]; then
if [[ $VERSION_MINOR -eq CURRENT_VERSION_MINOR ]]; then
if [[ $(($VERSION_PATCH - 1)) -ne CURRENT_VERSION_PATCH ]]; then
STATUS=1
fi
else
if [[ $(($VERSION_MINOR - 1)) -eq CURRENT_VERSION_MINOR ]]; then
if [[ $VERSION_PATCH -ne 0 ]]; then
STATUS=1
fi
else
STATUS=1
fi
fi
else
if [[ $(($VERSION_MAJOR - 1)) -eq CURRENT_VERSION_MAJOR ]]; then
if [[ $VERSION_MINOR -ne 0 ]]; then
STATUS=1
fi
if [[ $VERSION_PATCH -ne 0 ]]; then
STATUS=1
fi
else
STATUS=1
fi
fi
if [ $STATUS = 1 ]; then
echo "- :no_entry_sign: Version skipped steps from the previous version $CURRENT_VERSION and thus does not follow the semantic versioning schema." >> $GITHUB_STEP_SUMMARY
echo "- :warning: Please see https://semver.org/ for further information." >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "- is an increment from the previous version :white_check_mark:" >> $GITHUB_STEP_SUMMARY
fi
## =================================
## provide variables for subsequent jobs
## ---------------------------------
echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "new=$VERSION" >> $GITHUB_OUTPUT
create_package:
name: "create package"
needs: [version]
runs-on: ubuntu-latest
steps:
- run: echo "Creating package and release note for '${{ needs.version.outputs.new }}':" >> $GITHUB_STEP_SUMMARY
# 1. checkout the repo
- name: "checkout"
uses: actions/checkout@v3
# 2. create release note
- name: "create release note"
run: |
VERSION=${{ needs.version.outputs.new }}
CURRENT_VERSION=${{ needs.version.outputs.current }}
STATUS=0
## =================================
## create release note
## ---------------------------------
TOP=$(grep -n "## \[Unreleased\]" CHANGELOG.md | cut -d: -f1)
BOTTOM=$(grep -n -m 1 "## Version \[$CURRENT_VERSION\]" CHANGELOG.md | cut -d: -f1)
awk "NR>$TOP&&NR<$BOTTOM" CHANGELOG.md > release-note-v$VERSION.md
sed -i -- "s/###/##/g" release-note-v$VERSION.md
## =================================
## check if release note is empty
## ---------------------------------
WORD_COUNT=$(wc -w release-note-v$VERSION.md | awk '{print $1}')
if [[ $WORD_COUNT -lt 4 ]]; then
STATUS=1
fi
if [ $STATUS = 1 ]; then
echo "- :no_entry_sign: 'Unreleased' section in CHANGELOG.md is empty" >> $GITHUB_STEP_SUMMARY
echo ":warning: Please provide a meaningful CHANGELOG.md for the new version $VERSION"." >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "- 'Unreleased' section in CHANGELOG.md has content :white_check_mark:" >> $GITHUB_STEP_SUMMARY
fi
## =================================
## remove empty lines from top and bottom
## ---------------------------------
sed -i -- -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;ba' -e '}' release-note-v$VERSION.md
# 3. create release archive
- name: "create release archive"
uses: papeloto/action-zip@v1
with:
files: docs src test README.md LICENSE Project.toml
recursive: false
dest: TrainRuns.jl-v${{ needs.version.outputs.new }}.zip
- run: echo "- creating ZIP-Archive for the new release :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 4. upload artifact to share it with other jobs
- uses: actions/upload-artifact@v3
with:
path: |
release-note-v${{ needs.version.outputs.new }}.md
TrainRuns.jl-v${{ needs.version.outputs.new }}.zip
if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
- run: echo "- providing ZIP-Archive and release notes for other jobs :white_check_mark:" >> $GITHUB_STEP_SUMMARY
publish_github:
needs: [create_package, version]
name: "publish on github"
runs-on: ubuntu-latest
steps:
- run: echo "Publishing package on Github:" >> $GITHUB_STEP_SUMMARY
# 1. download artifact in folder artifact/
- uses: actions/download-artifact@v3
- run: echo "- getting ZIP-Archive and release notes :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 2. creating a new release
- name: "create release"
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body_path: artifact/release-note-v${{ needs.version.outputs.new }}.md
draft: false
prerelease: false
- run: echo "- drafting new release :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 3. upload package to new release
- name: "upload release asset"
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: artifact/TrainRuns.jl-v${{ needs.version.outputs.new }}.zip
asset_name: TrainRuns.jl-v${{ needs.version.outputs.new }}.zip
asset_content_type: application/zip
- run: echo "- upload release asset :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 4. publish release on github
- name: "publish release"
uses: StuYarrow/publish-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
id: ${{ steps.create_release.outputs.id }}
- run: echo "- publish release at ${{ steps.create_release.outputs.html_url }} :white_check_mark:" >> $GITHUB_STEP_SUMMARY
zenodo:
needs: [create_package, version]
name: "publish on zenodo"
outputs:
doi: ${{ steps.zenodraft.outputs.doi }}
runs-on: ubuntu-latest
steps:
- run: echo "Archiving package on zenodo to get a DOI:" >> $GITHUB_STEP_SUMMARY
# 1. download artifact in folder artifact/ and move it one level up
- uses: actions/download-artifact@v3
- run: |
mv ./artifact/TrainRuns.jl-v${{ needs.version.outputs.new }}.zip ./
echo "- getting ZIP-Archive and release notes :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 2. install zenodraft
- name: "install zenodraft"
env:
URL: "https://zenodo.org/"
run: |
STATUS=0
npm install -g zenodraft
echo "- installing zenodraft :white_check_mark:" >> $GITHUB_STEP_SUMMARY
if ! curl --output /dev/null --silent --head --fail "$URL"; then
STATUS=1
fi
if [ $STATUS = 1 ]; then
echo "- :no_entry_sign: $URL is not reachable" >> $GITHUB_STEP_SUMMARY
echo ":warning: Please re-run action later!" >> $GITHUB_STEP_SUMMARY
echo ":warning: Please remove github release manually!" >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "- $URL is reachable :white_check_mark:" >> $GITHUB_STEP_SUMMARY
fi
# 3. Update zenodo metadata.json
- name: "Update zenodo metadata.json"
run: |
VERSION=${{ needs.version.outputs.new }}
sed -i".backup" -e"s/\"version\": \"%%\[SCRIPT\]\"/\"version\": \"$VERSION\"/g" .github/zenodo/metadata.json
echo "- updating zenodo metadata :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 4. upload new release to zenodo
- name: "uploading to zenodo"
id: zenodraft
env:
ZENODO_ACCESS_TOKEN: ${{ secrets.ZENODO_ACCESS_TOKEN }}
COLLECTION: 6448563
run: |
DOI=$(zenodraft deposition create version $COLLECTION)
zenodraft file add $DOI TrainRuns.jl-v*.zip
zenodraft metadata update $DOI .github/zenodo/metadata.json
zenodraft deposition publish $DOI
## =================================
## provide variables for subsequent jobs
## ---------------------------------
echo "doi=$DOI" >> $GITHUB_OUTPUT
echo "- publish release at zenodo with DOI: $DOI :white_check_mark:" >> $GITHUB_STEP_SUMMARY
update_repository:
needs: [zenodo, version]
name: "updating CITATION.cff and CHANGELOG.md"
runs-on: ubuntu-latest
steps:
- run: echo "Updating CITATION.cff and CHANGELOG.md:" >> $GITHUB_STEP_SUMMARY
# 1. checkout the repo
- name: "checkout"
uses: actions/checkout@v3
# with:
# ref: main
# 2. update CITATION.cff
- name: "update CITATION.cff"
run: |
DATE=$(date "+%Y-%m-%d")
VERSION=${{ needs.version.outputs.new }}
DOI=${{ needs.publish_zenodo.outputs.doi }}
echo "find lines in CITATION.cff"
VERSION_LINE=$(grep -n '^version:' CITATION.cff | cut -d: -f1)
DATE_LINE=$(grep -n 'date-released:' CITATION.cff | cut -d: -f1)
echo "select the second DOI"
DOI_LINE=$(grep -n 'type: doi' CITATION.cff | cut -d: -f1 | awk "NR==2")
DOI_LINE=$(( $DOI_LINE + 1 ))
echo "update CITATION.cff"
sed -i -- "${VERSION_LINE}s|.*|version: $VERSION|" CITATION.cff
sed -i -- "${DATE_LINE}s|.*|date-released: ${DATE}|" CITATION.cff
sed -i -- "${DOI_LINE}s|.*| value: $DOI|" CITATION.cff
echo "- updated CITATION.cff :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 3. update CHANGELOG.md
- name: "update CHANGELOG.md"
run: |
DATE=$(date "+%Y-%m-%d")
VERSION=${{ needs.version.outputs.new }}
URL="https://github.com/railtoolkit/TrainRuns.jl/compare"
CURRENT_VERSION=${{ needs.version.outputs.current }}
echo "increment CHANGELOG.md"
sed -i -- "/## \[Unreleased\]/a\\\n\n## Version [$VERSION] $DATE" CHANGELOG.md
sed -i -- "s|^\[Unreleased\]: .*$|\[Unreleased\]: $URL/v$VERSION...main\n\[$VERSION\]: $URL/v$CURRENT_VERSION...v$VERSION|" CHANGELOG.md
echo "- updated CHANGELOG.md :white_check_mark:" >> $GITHUB_STEP_SUMMARY
# 4. push the change back to main
- name: push
uses: EndBug/add-and-commit@v9
with:
message: "updated to ${{ needs.version.outputs.new }} (via github action)"
add: CITATION.cff CHANGELOG.md
author_name: railtoolkit
author_email: railtoolkit@ownx.net
- run: echo "- updated repository with new CITATION.cff and CHANGELOG.md :white_check_mark:" >> $GITHUB_STEP_SUMMARY
register_julia:
needs: [update_repository]
name: ":checkered_flag: publish in JuliaRegistries"
runs-on: ubuntu-latest
steps:
# 1. register new release at JuliaRegistries
- uses: julia-actions/RegisterAction@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
publish_mastodon:
needs: [zenodo, version, create_package]
name: ":checkered_flag: Send toot about it to railtoolkit@fosstodon.org"
runs-on: ubuntu-latest
steps:
- uses: cbrgm/mastodon-github-action@v1
with:
message: "The new version ${{ needs.version.outputs.new }} of TrainRuns.jl is available! DOI: https://doi.org/${{ needs.publish_zenodo.outputs.doi }} Further information at at ${{ needs.create_release.outputs.html_url }}."
visibility: "public" # default: public
env:
MASTODON_URL: "https://fosstodon.org/"
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }} # access token