Skip to content

Commit 7332417

Browse files
authored
Merge pull request #1 from dohernandez/patch/github-action
patch: Github Actions and Makefile functionalities
2 parents 9f8df5e + 802e1bb commit 7332417

18 files changed

Lines changed: 618 additions & 87 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Check Branch
2+
3+
This action gets the head ref or source branch of the pull request in a workflow to run a checks on the branch name. The check verify that the branch name starts with a placeholder `types` and followed by another placeholder `separators`.
4+
5+
The input `types` default values are:
6+
7+
- major
8+
- release
9+
- minor
10+
- feature
11+
- feat
12+
- patch
13+
- issue
14+
- hotfix
15+
- dependabot
16+
- whitesource/
17+
18+
The input `separators` default value are `-` and `\`.
19+
20+
## Usage
21+
22+
See [action.yml](action.yml).
23+
24+
## Examples
25+
26+
```yaml
27+
---
28+
name: check branch name
29+
30+
on:
31+
pull_request:
32+
33+
jobs:
34+
bump:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v2
38+
39+
- name: Check branch
40+
uses: ./.github/actions/check-branch/
41+
```
42+
43+
## How does it work
44+
45+
This simple action runs a [check-branch script](check-branch.sh)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: 'Check Branch'
2+
description: 'Check branch name'
3+
4+
inputs:
5+
types:
6+
description: "Provide custom types if you don't want the default ones from major|release|minor|feature|patch|issue|hotfix|dependabot|whitesource/"
7+
required: false
8+
separators:
9+
description: "Provide custom separators if you don't want the default ones from -|/"
10+
required: false
11+
12+
runs:
13+
using: "composite"
14+
steps:
15+
- run: ${{ github.action_path }}/check-branch.sh "${{ inputs.types }}" "${{ inputs.separators }}"
16+
shell: bash
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env sh
2+
3+
# This code is provided by github.com/dohernandez/dev.
4+
5+
FLAT_TYPES_DEFAULT="major release minor feature feat patch issue hotfix dependabot whitesource/"
6+
FLAT_SEPARATORS_DEFAULT="- /"
7+
8+
types="${1}"
9+
10+
FLAT_TYPES=""
11+
12+
for target in $types
13+
do
14+
FLAT_TYPES="$FLAT_TYPES $target"
15+
done
16+
17+
# removing the first space added when FLAT_TYPES is empty
18+
FLAT_TYPES=$(echo "${FLAT_TYPES}" | sed 's/^ //g')
19+
20+
# If FLAT_TYPES is empty, use the value of FLAT_TYPES_DEFAULT instead
21+
FLAT_TYPES=${FLAT_TYPES:-$FLAT_TYPES_DEFAULT}
22+
23+
REGEX_FLAT_TYPES=$(echo "${FLAT_TYPES}" | sed 's/ /|/g')
24+
25+
26+
separators="${2}"
27+
28+
FLAT_SEPARATORS=""
29+
30+
for target in $separators
31+
do
32+
FLAT_SEPARATORS="$FLAT_SEPARATORS $target"
33+
done
34+
35+
# If FLAT_SEPARATORS is empty, use the value of FLAT_TYPES_DEFAULT instead
36+
FLAT_SEPARATORS=${FLAT_SEPARATORS:-$FLAT_SEPARATORS_DEFAULT}
37+
38+
REGEX_FLAT_SEPARATORS=$(echo "${FLAT_SEPARATORS}" | sed 's/ /|/g')
39+
40+
41+
42+
# ^minor([-\/]+.+)?$
43+
if ! (echo "${GITHUB_HEAD_REF}" | grep -i -E "^($REGEX_FLAT_TYPES)([$REGEX_FLAT_SEPARATORS]+.+)?$"); then
44+
VALID_FLAT_TYPES=$(echo "${FLAT_TYPES}" | sed 's/ /, /g')
45+
VALID_FLAT_SEPARATORS=$(echo "${FLAT_SEPARATORS}" | sed 's/ /, /g')
46+
echo "Invalid branch name \"${GITHUB_HEAD_REF}\". Valid branch prefixes: ${VALID_FLAT_TYPES} and separators: ${VALID_FLAT_SEPARATORS}"
47+
exit 1
48+
fi
49+
echo "Valid branch name"

.github/dependabot.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gomod"
4+
directory: "/" # Location go.mod
5+
schedule:
6+
interval: "daily"
7+
open-pull-requests-limit: 1
8+
groups:
9+
dependencies:
10+
patterns:
11+
- "*" # Update all dependencies
12+
update-types:
13+
- "minor"
14+
- "patch"

.github/workflows/check.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: check
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
types: [opened, edited, synchronize, reopened]
8+
9+
jobs:
10+
branch-name:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Check branch name
16+
uses: ./.github/actions/check-branch/
17+
with:
18+
types: ${{env.types}}
19+
separators: ${{env.separators}}

.github/workflows/cloc.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: cloc
2+
on:
3+
pull_request:
4+
5+
# Cancel the workflow in progress in newer build is about to start.
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
8+
cancel-in-progress: true
9+
10+
jobs:
11+
cloc:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
with:
17+
path: pr
18+
- name: Checkout base code
19+
uses: actions/checkout@v3
20+
with:
21+
ref: ${{ github.event.pull_request.base.sha }}
22+
path: base
23+
- name: Count Lines Of Code
24+
id: loc
25+
run: |
26+
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
27+
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
28+
{
29+
echo "diff<<EOF"
30+
echo "$OUTPUT"
31+
echo "EOF"
32+
} >> $GITHUB_OUTPUT
33+
- name: Comment Code Lines
34+
uses: marocchino/sticky-pull-request-comment@v2
35+
with:
36+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
37+
header: LOC
38+
message: |
39+
### Lines Of Code
40+
${{ steps.loc.outputs.diff }}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- main
8+
pull_request:
9+
10+
# Cancel the workflow in progress in newer build is about to start.
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
golangci:
17+
name: golangci-lint
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/setup-go@v3
21+
with:
22+
go-version: 1.23.x
23+
- uses: actions/checkout@v3
24+
- name: golangci-lint
25+
uses: golangci/golangci-lint-action@v6.1.0
26+
with:
27+
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
28+
version: v1.61.0
29+
30+
# Optional: working directory, useful for monorepos
31+
# working-directory: somedir
32+
33+
# Optional: golangci-lint command line arguments.
34+
# args: --issues-exit-code=0
35+
36+
# Optional: show only new issues if it's a pull request. The default value is `false`.
37+
# only-new-issues: true
38+
39+
# Optional: if set to true then the action will use pre-installed Go.
40+
# skip-go-installation: true
41+
42+
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
43+
# skip-pkg-cache: true
44+
45+
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
46+
# skip-build-cache: true

.github/workflows/gorelease.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: gorelease
2+
on:
3+
pull_request:
4+
5+
# Cancel the workflow in progress in newer build is about to start.
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
8+
cancel-in-progress: true
9+
10+
env:
11+
GO_VERSION: 1.23.x
12+
jobs:
13+
gorelease:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Install Go stable
17+
if: env.GO_VERSION != 'tip'
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: ${{ env.GO_VERSION }}
21+
- name: Install Go tip
22+
if: env.GO_VERSION == 'tip'
23+
run: |
24+
curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz
25+
ls -lah gotip.tar.gz
26+
mkdir -p ~/sdk/gotip
27+
tar -C ~/sdk/gotip -xzf gotip.tar.gz
28+
~/sdk/gotip/bin/go version
29+
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
30+
- name: Checkout code
31+
uses: actions/checkout@v3
32+
- name: Gorelease cache
33+
uses: actions/cache@v3
34+
with:
35+
path: |
36+
~/go/bin/gorelease
37+
key: ${{ runner.os }}-gorelease-generic
38+
- name: Gorelease
39+
id: gorelease
40+
run: |
41+
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
42+
OUTPUT=$(gorelease 2>&1 || exit 0)
43+
echo "${OUTPUT}"
44+
echo "report<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
45+
- name: Comment report
46+
continue-on-error: true
47+
uses: marocchino/sticky-pull-request-comment@v2
48+
with:
49+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
50+
header: gorelease
51+
message: |
52+
### Go API Changes
53+
54+
<pre>
55+
${{ steps.gorelease.outputs.report }}
56+
</pre>

.github/workflows/release.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: release
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
branches:
8+
- main
9+
10+
jobs:
11+
create-release:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
18+
- name: Set up Go
19+
uses: actions/setup-go@v2
20+
with:
21+
go-version: 1.23.x
22+
23+
- name: Get Merged PR Branch
24+
id: get-branch
25+
run: |
26+
PR_BRANCH=$(jq -r '.pull_request.head.ref' "$GITHUB_EVENT_PATH")
27+
echo "Merged PR Branch: $PR_BRANCH"
28+
echo "PR_BRANCH=$PR_BRANCH" >> $GITHUB_ENV
29+
30+
- name: Determine level
31+
id: determine-level
32+
run: |
33+
case "${{ env.PR_BRANCH }}" in
34+
patch* | issue* | hotfix* | dependabot* | whitesource/*)
35+
LEVEL="patch";;
36+
minor* | feature* | feat*)
37+
LEVEL="minor";;
38+
major* | release*)
39+
LEVEL="major";;
40+
*)
41+
LEVEL="unknown";;
42+
esac
43+
echo "Determined level: $LEVEL"
44+
echo "LEVEL=$LEVEL" >> $GITHUB_ENV
45+
46+
- name: Get latest tag
47+
id: latest_tag
48+
uses: actions-ecosystem/action-get-latest-tag@v1
49+
50+
- name: Bump release version
51+
id: bump_version
52+
uses: actions-ecosystem/action-bump-semver@v1
53+
with:
54+
current_version: ${{ steps.latest_tag.outputs.tag }}
55+
level: ${{ env.LEVEL }}
56+
57+
- name: Get Merge Commit Message
58+
id: get_merge_commit_message
59+
run: |
60+
MERGE_COMMIT_MESSAGE=$(git log --merges --format=%B -n 1)
61+
echo "Merge Commit Message: $MERGE_COMMIT_MESSAGE"
62+
echo "$MERGE_COMMIT_MESSAGE" > ${{ github.workspace }}-CHANGELOG.txt
63+
64+
- name: Create GitHub Release
65+
uses: softprops/action-gh-release@v1
66+
with:
67+
body_path: ${{ github.workspace }}-CHANGELOG.txt
68+
tag_name: ${{ steps.bump_version.outputs.new_version }}
69+
token: ${{ secrets.GH_TOKEN }}

0 commit comments

Comments
 (0)