Skip to content

Commit

Permalink
ci: add github workflows
Browse files Browse the repository at this point in the history
- auto_prerelease.yaml
- auto_tag_and_release.yaml
  • Loading branch information
black-desk committed Sep 20, 2024
1 parent d2f6ff0 commit 35682c8
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/auto_prerelease.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Automatic pre-release

on:
push:
tags: ["v*-*"]

jobs:
auto_pre_release:
name: Automatic pre-release
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# NOTE:
# Some automatic release action
# might need history for generate change log.
fetch-depth: 0

- name: Run generate-release-assets.sh
id: generate_release_assets
run: |
# NOTE:
# The pitchfork layout holds extra scripts in tools directory.
# > https://blog.black-desk.cn/pages/pintchfork-layout.html
# But the "Standard Go Project Layout"
# holds extra scripts in scripts directory.
# > https://github.com/golang-standards/project-layout#scripts
generate_release_assets=tools/generate-release-assets.sh
if [ ! -f "$generate_release_assets" ]; then
generate_release_assets=scripts/generate-release-assets.sh
fi
if [ ! -f "$generate_release_assets" ]; then
echo "generate-release-assets script not found" >&2
exit -1
fi
ASSETS="$("${generate_release_assets}")"
echo assets="$ASSETS" >> $GITHUB_OUTPUT
- name: Run marvinpinto/action-automatic-releases
uses: marvinpinto/action-automatic-releases@latest
if: needs.auto_tag.outputs.new_tag
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
automatic_release_tag: ${{ needs.auto_tag.outputs.new_tag }}
prerelease: true
files: ${{ steps.generate_release_assets.outputs.assets }}
108 changes: 108 additions & 0 deletions .github/workflows/auto_tag_and_release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Automatic create tag and release.
on:
push:
branches: [master]

jobs:
auto_tag:
permissions:
contents: write
name: Automatic create new tag from tools/get_project_version.sh
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# NOTE:
# Step `check_tag` need history.
fetch-depth: 0

- name: Run get-project-version.sh
id: get_project_version
run: |
# NOTE:
# The pitchfork layout holds extra scripts in tools directory.
# > https://blog.black-desk.cn/pages/pintchfork-layout.html
# But the "Standard Go Project Layout"
# holds extra scripts in scripts directory.
# > https://github.com/golang-standards/project-layout#scripts
get_project_version=tools/get-project-version.sh
if [ ! -f "$get_project_version" ]; then
get_project_version=scripts/get-project-version.sh
fi
if [ ! -f "$get_project_version" ]; then
echo "get-project-version script not found" >&2
exit -1
fi
version="$("${get_project_version}")"
echo version="$version" >> $GITHUB_OUTPUT
- name: Check if tag already exists
id: check_tag
run: |
if git rev-parse "${{ steps.get_project_version.outputs.version }}" &>/dev/null; then
echo existed=true >> $GITHUB_OUTPUT
else
echo existed=false >> $GITHUB_OUTPUT
fi
- name: Run anothrNick/github-tag-action
id: create_tag
if: steps.check_tag.outputs.existed == 'false'
uses: anothrNick/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN}}
CUSTOM_TAG: ${{ steps.get_project_version.outputs.version }}

outputs:
new_tag: ${{ steps.create_tag.outputs.new_tag }}

auto_release:
permissions:
contents: write
name: Automatic release for new tag
runs-on: ubuntu-latest
needs:
- auto_tag
steps:
- name: Checkout repository
if: needs.auto_tag.outputs.new_tag
uses: actions/checkout@v4
with:
# NOTE:
# Some automatic release action
# might need history for generate change log.
fetch-depth: 0

- name: Run tools/generate-release-assets.sh
id: generate_release_assets
if: needs.auto_tag.outputs.new_tag
run: |
# NOTE:
# The pitchfork layout holds extra scripts in tools directory.
# > https://blog.black-desk.cn/pages/pintchfork-layout.html
# But the "Standard Go Project Layout"
# holds extra scripts in scripts directory.
# > https://github.com/golang-standards/project-layout#scripts
generate_release_assets=tools/generate-release-assets.sh
if [ ! -f "$generate_release_assets" ]; then
generate_release_assets=scripts/generate-release-assets.sh
fi
if [ ! -f "$generate_release_assets" ]; then
echo "generate-release-assets script not found" >&2
exit -1
fi
ASSETS="$("${generate_release_assets}")"
echo assets="$ASSETS" >> $GITHUB_OUTPUT
- name: Run marvinpinto/action-automatic-releases
uses: marvinpinto/action-automatic-releases@latest
if: needs.auto_tag.outputs.new_tag
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
automatic_release_tag: ${{ needs.auto_tag.outputs.new_tag }}
prerelease: false
files: ${{ steps.generate_release_assets.outputs.assets }}
101 changes: 101 additions & 0 deletions tools/generate-release-assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash
# NOTE:
# Use /usr/bin/env to find shell interpreter for better portability.
# Reference: https://en.wikipedia.org/wiki/Shebang_%28Unix%29#Portability

# NOTE:
# Exit immediately if any commands (even in pipeline)
# exits with a non-zero status.
set -e
set -o pipefail

# WARNING:
# This is not reliable when using POSIX sh
# and current script file is sourced by `source` or `.`
CURRENT_SOURCE_FILE_PATH="${BASH_SOURCE[0]:-$0}"
CURRENT_SOURCE_FILE_NAME="$(basename -- "$CURRENT_SOURCE_FILE_PATH")"

# This function log messages to stderr works like printf
# with a prefix of the current script name.
# Arguments:
# $1 - The format string.
# $@ - Arguments to the format string, just like printf.
function log() {
local format="$1"
shift
# shellcheck disable=SC2059
printf "$CURRENT_SOURCE_FILE_NAME: $format\n" "$@" >&2 || true
}

# shellcheck disable=SC2016
USAGE="$CURRENT_SOURCE_FILE_NAME"'
This script generate release assets
then print paths to the generated assets to STDOUT.
You can use this script in github action like this:
```yaml
name: Example workflow
on:
push:
tags: *
jobs:
auto_release:
name: Automatic release
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run tools/'"$CURRENT_SOURCE_FILE_NAME"'
id: generate_release_assets
run: |
ASSETS=$(tools/'"$CURRENT_SOURCE_FILE_NAME"')
echo assets="$ASSETS" >> $GITHUB_OUTPUT
- name: Automatic release
uses: marvinpinto/action-automatic-releases@latest
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
prerelease: false
files: ${{ steps.generate_release_assets.outputs.assets }}
```
'"
Usage:
$CURRENT_SOURCE_FILE_NAME -h
$CURRENT_SOURCE_FILE_NAME
Options:
-h Show this screen."

while getopts ':h' option; do
case "$option" in
h)
echo "$USAGE"
exit
;;
\?)
log "[ERROR] Unknown option: -%s" "$OPTARG"
exit 1
;;
esac
done
shift $((OPTIND - 1))

CURRENT_SOURCE_FILE_DIR="$(dirname -- "$CURRENT_SOURCE_FILE_PATH")"
cd -- "$CURRENT_SOURCE_FILE_DIR"

source ./utils.sh

# ------------------------------------------------------------------------------

check_ci

BUILD_DIR="../build_generate-release-assets"

configure_cmake_project .. $BUILD_DIR -DCMAKE_BUILD_TYPE=Release >&2

realpath "$BUILD_DIR"/errors.hpp
77 changes: 77 additions & 0 deletions tools/get-project-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env bash
# NOTE:
# Use /usr/bin/env to find shell interpreter for better portability.
# Reference: https://en.wikipedia.org/wiki/Shebang_%28Unix%29#Portability

# NOTE:
# Exit immediately if any commands (even in pipeline)
# exits with a non-zero status.
set -e
set -o pipefail

# WARNING:
# This is not reliable when using POSIX sh
# and current script file is sourced by `source` or `.`
CURRENT_SOURCE_FILE_PATH="${BASH_SOURCE[0]:-$0}"
CURRENT_SOURCE_FILE_NAME="$(basename -- "$CURRENT_SOURCE_FILE_PATH")"

# This function log messages to stderr works like printf
# with a prefix of the current script name.
# Arguments:
# $1 - The format string.
# $@ - Arguments to the format string, just like printf.
function log() {
local format="$1"
shift
# shellcheck disable=SC2059
printf "$CURRENT_SOURCE_FILE_NAME: $format\n" "$@" >&2 || true
}

# shellcheck disable=SC2016
USAGE="$CURRENT_SOURCE_FILE_NAME"'
This script prints the project version to STDOUT
for automatically creating tag in CI.
'"
Usage:
$CURRENT_SOURCE_FILE_NAME -h
$CURRENT_SOURCE_FILE_NAME
Options:
-h Show this screen."

CURRENT_SOURCE_FILE_DIR="$(dirname -- "$CURRENT_SOURCE_FILE_PATH")"
cd -- "$CURRENT_SOURCE_FILE_DIR"

function main() {
while getopts ':h' option; do
case "$option" in
h)
echo "$USAGE"
exit
;;
\?)
log "[ERROR] Unknown option: -%s" "$OPTARG"
exit 1
;;
esac
done
shift $((OPTIND - 1))

source ./utils.sh

check_ci

BUILD_DIR=../build_get-project-version

configure_cmake_project .. $BUILD_DIR >&2
cd $BUILD_DIR
version=$(cmake --system-information | awk -F= '$1~/CMAKE_PROJECT_VERSION:STATIC/{print$2}')
if [ -z "$version" ]; then
false
fi
echo "v$version"
}

main "$@"
Loading

0 comments on commit 35682c8

Please sign in to comment.