Skip to content

Commit

Permalink
Add github actions workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
at055612 committed Sep 25, 2024
1 parent 11d09bd commit fdb40ca
Show file tree
Hide file tree
Showing 9 changed files with 759 additions and 1 deletion.
80 changes: 80 additions & 0 deletions .github/workflows/build_and_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Full CI Build (and Release)

on:
push:
branches:
- '**'
pull_request:

jobs:
build-project:
runs-on: ubuntu-24.04
env:
# Static env vars

# Github runners limited to 2 cores, 7Gb RAM
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
MAX_WORKERS: 3
# Fixed ssh-agent socket so multiple steps can use the same agent
# if needs be. Used by update_gh_pages.sh
SSH_AUTH_SOCK: "/tmp/ssh-agent-stroom.sock"
steps:

- name: Install dependencies
id: install_dependencies
run: |
# libxml2-utils needed for xmllint
sudo apt-get update
sudo apt-get install -y libxml2-utils
- name: Checkout code
id: checkout_code
uses: actions/checkout@v3
with:
# Set this so it gets the annotated commit, not the commit being tagged.
# Which means we can get the release msg
# See https://github.com/actions/runner/issues/712
ref: ${{ github.ref }}

- name: Setup Java
id: setup_java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'

- name: Set Environment Variables
id: set_env_var
env:
# Needed for authenticated api calls to avoid rate limit
# Github provides this temporary token automatically
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
${GITHUB_WORKSPACE}/.github/workflows/scripts/setup_env_vars.sh
# Separate step to show what is visible across steps
- name: Build Environment Info
id: build_info
run: |
${ACTIONS_SCRIPTS_DIR}/dump_env_vars.sh
- name: Run full build
id: run_build
env:
# Needed for authenticated api calls to avoid rate limit
# Github provides this temporary token automatically
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pushd "${BUILD_DIR}" > /dev/null
echo -e "${GREEN}Running ${BLUE}ci_build.sh${NC}"
./ci_build.sh
echo -e "${GREEN}Finished running build script${NC}"
- name: Release to GitHub
id: create_release
if: ${{ env.BUILD_IS_SCHEMA_RELEASE == 'true' }}
env:
# Github provided secret
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: "${ACTIONS_SCRIPTS_DIR}/create_github_release.sh"

97 changes: 97 additions & 0 deletions .github/workflows/scripts/create_github_release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash
set -eo pipefail
IFS=$'\n\t'

setup_echo_colours() {
# Exit the script on any error
set -e

# shellcheck disable=SC2034
if [ "${MONOCHROME}" = true ]; then
RED=''
GREEN=''
YELLOW=''
BLUE=''
BLUE2=''
DGREY=''
NC='' # No Colour
else
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
BLUE2='\033[1;34m'
DGREY='\e[90m'
NC='\033[0m' # No Colour
fi
}

debug_value() {
local name="$1"; shift
local value="$1"; shift

if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${name}: ${value}${NC}"
fi
}

debug() {
local str="$1"; shift

if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${str}${NC}"
fi
}

main() {
IS_DEBUG=false
#SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

setup_echo_colours

echo -e "${GREEN}Finding asset files for release${NC}"
local asset_files=()
for asset_file in "${BUILD_DIR}/release_artefacts/"*; do
echo -e "${GREEN}Found asset file: ${BLUE}${asset_file}${NC}"
asset_files+=("${asset_file}")
done

local args=()
if [[ ${GITHUB_REF} =~ .*(beta|alpha).* ]]; then
echo -e "${GREEN}Release is a pre-release${NC}"
args+=("--prerelease")
fi

git tag \
--list "${BUILD_TAG}" \
--format='%(subject)%0a%0a%(contents:body)'

local message
message="$( \
git tag \
--list "${BUILD_TAG}" \
--format='%(subject)%0a%0a%(contents:body)')"

echo -e "${GREEN}Creating release for tag ${BLUE}${BUILD_TAG}${GREEN} with message:${NC}"
echo -e "${DGREY}------------------------------------------------------------------------${NC}"
echo -e "${DGREY}${message}${NC}"
echo -e "${DGREY}------------------------------------------------------------------------${NC}"


# Create the release on GitHub using the annotated tag that triggered
# this build
# See https://cli.github.com/manual/gh_release_create
gh release create \
--title "${BUILD_TAG}" \
--notes "${message}" \
--verify-tag \
"${args[@]}" \
"${BUILD_TAG}" \
"${asset_files[@]}"

echo "${GREEN}Release created for tag ${BLUE}${BUILD_TAG}${NC}"
}

main "$@"

# vim: set tabstop=2 shiftwidth=2 expandtab:
26 changes: 26 additions & 0 deletions .github/workflows/scripts/dump_env_vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -eo pipefail

if [[ -z "${ACTIONS_SCRIPTS_DIR}" ]]; then
echo "ACTIONS_SCRIPTS_DIR not set"
exit 1
fi

"${ACTIONS_SCRIPTS_DIR}/echo_variables.sh" \
"git version" "$(git --version)" \
"GITHUB_EVENT_NAME" "$GITHUB_EVENT_NAME" \
"GITHUB_REF" "$GITHUB_REF" \
"GITHUB_SHA" "$GITHUB_SHA" \
"GITHUB_WORKSPACE" "$GITHUB_WORKSPACE" \
"BUILD_BRANCH" "$BUILD_BRANCH" \
"BUILD_COMMIT" "$BUILD_COMMIT" \
"BUILD_DIR" "$BUILD_DIR" \
"BUILD_NUMBER" "$BUILD_NUMBER" \
"BUILD_TAG" "$BUILD_TAG" \
"BUILD_IS_PULL_REQUEST" "$BUILD_IS_PULL_REQUEST" \
"BUILD_IS_SCHEMA_RELEASE" "$BUILD_IS_SCHEMA_RELEASE" \
"ACTIONS_SCRIPTS_DIR" "$ACTIONS_SCRIPTS_DIR" \
"PWD" "$PWD" \
"HOME" "$HOME"

72 changes: 72 additions & 0 deletions .github/workflows/scripts/echo_variables.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash

# Simple script to echo pairs of variable names and their values,
# e.g. ./echo_variables.sh HOME $HOME "Docker version" "$(docker --version)"
# results in:
# HOME: [/home/dev]
# Docker version: [Docker version 20.10.6, build 370c289]

set -eo pipefail
IFS=$'\n\t'

setup_echo_colours() {
# Exit the script on any error
set -e

# shellcheck disable=SC2034
if [ "${MONOCHROME}" = true ]; then
RED=''
GREEN=''
YELLOW=''
BLUE=''
BLUE2=''
DGREY=''
NC='' # No Colour
else
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
BLUE2='\033[1;34m'
DGREY='\e[90m'
NC='\033[0m' # No Colour
fi
}

debug_value() {
local name="$1"; shift
local value="$1"; shift

if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${name}: ${value}${NC}"
fi
}

debug() {
local str="$1"; shift

if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${str}${NC}"
fi
}

main() {
IS_DEBUG=false

setup_echo_colours

local max_padding_str=' '

while (( "$#" >= 2 )); do
local name="${1}"
local value="${2}"
shift 2
printf \
"${GREEN}%s${NC}: %s [${BLUE}%s${NC}]\n" \
"${name}" \
"${max_padding_str:${#name}}" \
"${value}"
done
}

main "$@"
57 changes: 57 additions & 0 deletions .github/workflows/scripts/setup_env_vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

set -eo pipefail

# Set variables in github's special env file which are then automatically
# read into env vars in each subsequent step

is_schema_release=false

echo -e "${GREEN}GITHUB_REF: ${BLUE}${GITHUB_REF}${NC}"

if [[ ${GITHUB_REF} =~ ^refs/tags/ ]]; then
# This is build is for a git tag
# strip off the 'refs/tags/' bit
tag_name="${GITHUB_REF#refs/tags/}"
if [[ "${tag_name}" =~ ^v ]]; then
# e.g. 'v4.0.2'
is_schema_release=true
echo "tag_name=${tag_name}"
fi
elif [[ ${GITHUB_REF} =~ ^refs/heads/ ]]; then
# This build is just a commit on a branch
# strip off the 'ref/heads/' bit
build_branch="${GITHUB_REF#refs/heads/}"
fi

# Brace block means all echo stdout get appended to GITHUB_ENV
{
# Map the GITHUB env vars to our own
echo "BUILD_DIR=${GITHUB_WORKSPACE}"
echo "BUILD_COMMIT=${GITHUB_SHA}"

build_number="${GITHUB_RUN_NUMBER}"
echo "BUILD_NUMBER=${build_number}"

echo "ACTIONS_SCRIPTS_DIR=${GITHUB_WORKSPACE}/.github/workflows/scripts"

if [[ ${GITHUB_REF} =~ ^refs/heads/ ]]; then
echo "BUILD_BRANCH=${build_branch}"
fi

# Only do a release based on our schedule, e.g. nightly
# Skip release if we have same commit as last time
if [[ "${is_schema_release}" = "true" ]]; then
echo "BUILD_IS_SCHEMA_RELEASE=true"
echo "BUILD_TAG=${GITHUB_REF#refs/tags/}"
else
echo "BUILD_IS_SCHEMA_RELEASE=false"
fi

if [[ ${GITHUB_REF} =~ ^refs/pulls/ ]]; then
echo "BUILD_IS_PULL_REQUEST=true"
else
echo "BUILD_IS_PULL_REQUEST=false"
fi

} >> "${GITHUB_ENV}"
Loading

0 comments on commit fdb40ca

Please sign in to comment.