Skip to content

Commit

Permalink
ci: add github workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
black-desk committed Sep 20, 2024
1 parent 2735822 commit 24fda38
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- directory: /
package-ecosystem: gitsubmodule
schedule:
interval: daily
- directory: /
package-ecosystem: github-actions
schedule:
interval: daily
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 }}
26 changes: 26 additions & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Checks

on: [pull_request]

jobs:
checks:
name: Run black-desk/checks
permissions:
checks: write
contents: read
issues: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: black-desk/checks@master
pass:
name: Pass
if: always()
needs:
- checks
runs-on: ubuntu-latest
steps:
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ pfl_add_library(
COMPILE_FEATURES
INTERFACE
cxx_std_17)

file(READ ${CMAKE_CURRENT_BINARY_DIR}/include/errors/config.hpp
ERRORS_CONFIG_HEADER_FILE)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/include/errors/version.hpp
ERRORS_VERSION_HEADER_FILE)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/include/errors/source_location.hpp
ERRORS_SOURCE_LOCATION_HEADER_FILE)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/include/errors/error.hpp
ERRORS_ERROR_HEADER_FILE)
configure_file(./misc/errors.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/errors.hpp)
5 changes: 4 additions & 1 deletion include/errors/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include <ostream>
#include <string>

#if not defined(ERRORS_SINGLE_HEADER_FILE)
#include "errors/config.hpp"
#include "errors/version.hpp"
#endif

#if defined(ERRORS_ENABLE_SOURCE_LOCATION)
#if defined(ERRORS_ENABLE_SOURCE_LOCATION) and \
not defined(ERRORS_SINGLE_HEADER_FILE)
#include "errors/source_location.hpp"
#endif

Expand Down
2 changes: 2 additions & 0 deletions include/errors/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <string_view>

#if not defined(ERRORS_SINGLE_HEADER_FILE)
#include "errors/config.hpp"
#endif

namespace errors
{
Expand Down
10 changes: 10 additions & 0 deletions misc/errors.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

// The single header file version of the errors library.

#define ERRORS_SINGLE_HEADER_FILE

@ERRORS_CONFIG_HEADER_FILE@
@ERRORS_VERSION_HEADER_FILE@
@ERRORS_SOURCE_LOCATION_HEADER_FILE@
@ERRORS_ERROR_HEADER_FILE@
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
Loading

0 comments on commit 24fda38

Please sign in to comment.