├── Makefile ├── git-info.sh ├── .github └── workflows │ ├── test.yml │ └── test-nightly.yml └── README.md /Makefile: -------------------------------------------------------------------------------- 1 | ifneq (,) 2 | .error This Makefile requires GNU Make. 3 | endif 4 | 5 | .PHONY: test 6 | 7 | # -------------------------------------------------------------------------------------------------- 8 | # VARIABLES 9 | # -------------------------------------------------------------------------------------------------- 10 | 11 | 12 | # -------------------------------------------------------------------------------------------------- 13 | # DEFAULT TARGET 14 | # -------------------------------------------------------------------------------------------------- 15 | help: 16 | @echo "test Test scripts" 17 | 18 | 19 | # -------------------------------------------------------------------------------------------------- 20 | # TEST TARGETS 21 | # -------------------------------------------------------------------------------------------------- 22 | test: 23 | ./git-info.sh 24 | -------------------------------------------------------------------------------- /git-info.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | set -u 5 | 6 | # Commit, Branch or Tag 7 | HEAD="$( git branch --no-color | grep '^\*' | sed 's|)||g' | xargs -n1 | tail -1 )" 8 | 9 | # Commit Hash 10 | HASH="$( git rev-parse HEAD | grep -Eo '^[0-9a-zA-Z]{7}' )" 11 | 12 | # Hash is found in what Branch? 13 | FROM="$( git branch --contains "${HASH}" | head -2 | tac | head -1 | sed 's|^*[[:space:]]*||g' | sed 's|)||g' | xargs -n1 | tail -1 | xargs )" 14 | 15 | # How many commits behind FROM branch 16 | BEHIND="$( git rev-list --count "${HASH}..${FROM}" )" 17 | 18 | 19 | ### 20 | ### TAG 21 | ### 22 | if git name-rev "${HEAD}" | grep -E "^${HEAD} tags/${HEAD}" > /dev/null; then 23 | echo "GIT_NAME=${HEAD}" 24 | echo "GIT_TYPE=TAG" 25 | echo "GIT_HEAD=${HASH}" 26 | echo 27 | echo "DETACHED_FROM=${FROM}" 28 | echo "BEHIND=${BEHIND}" 29 | ### 30 | ### Branch or Detached Commit 31 | ### 32 | else 33 | # Detached Commit (not latest in branch) 34 | if [ "${BEHIND}" -gt "0" ]; then 35 | echo "GIT_NAME=${HEAD}" 36 | echo "GIT_TYPE=COMMIT" 37 | echo "GIT_HEAD=${HASH}" 38 | echo 39 | echo "DETACHED_FROM=${FROM}" 40 | echo "BEHIND=${BEHIND}" 41 | else 42 | if [ "$( git branch | grep '^\*' | sed 's|^*[[:space:]]*||g' | xargs )" = "${FROM}" ]; then 43 | DETACHED= 44 | else 45 | DETACHED="${FROM}" 46 | fi 47 | echo "GIT_NAME=${FROM}" 48 | echo "GIT_TYPE=BRANCH" 49 | echo "GIT_HEAD=${HASH}" 50 | echo 51 | echo "DETACHED_FROM=${DETACHED}" 52 | echo "BEHIND=${BEHIND}" 53 | fi 54 | fi 55 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | # ------------------------------------------------------------------------------------------------- 4 | # Job Name 5 | # ------------------------------------------------------------------------------------------------- 6 | name: Test 7 | 8 | 9 | # ------------------------------------------------------------------------------------------------- 10 | # When to run 11 | # ------------------------------------------------------------------------------------------------- 12 | on: 13 | # Runs on Pull Requests 14 | pull_request: 15 | 16 | # Runs on master Branch and Tags 17 | push: 18 | 19 | 20 | # ------------------------------------------------------------------------------------------------- 21 | # What to run 22 | # ------------------------------------------------------------------------------------------------- 23 | jobs: 24 | test: 25 | name: "[ target: ${{ matrix.target }} ]" 26 | runs-on: ubuntu-latest 27 | strategy: 28 | fail-fast: False 29 | matrix: 30 | target: 31 | - test 32 | steps: 33 | 34 | # ------------------------------------------------------------ 35 | # Checkout repository 36 | # ------------------------------------------------------------ 37 | - name: Checkout repository 38 | uses: actions/checkout@v2 39 | with: 40 | fetch-depth: 0 41 | 42 | - name: "git-info.sh" 43 | run: | 44 | 45 | # Retrieve git info (tags, etc) 46 | git fetch --all 47 | make ${TARGET} 48 | env: 49 | TARGET: ${{ matrix.target }} 50 | -------------------------------------------------------------------------------- /.github/workflows/test-nightly.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | # ------------------------------------------------------------------------------------------------- 4 | # Job Name 5 | # ------------------------------------------------------------------------------------------------- 6 | name: Nightly 7 | 8 | 9 | # ------------------------------------------------------------------------------------------------- 10 | # When to run 11 | # ------------------------------------------------------------------------------------------------- 12 | on: 13 | # Runs every 5min 14 | schedule: 15 | - cron: '*/5 * * * *' 16 | 17 | 18 | # ------------------------------------------------------------------------------------------------- 19 | # What to run 20 | # ------------------------------------------------------------------------------------------------- 21 | jobs: 22 | test: 23 | name: "[ ref: ${{ matrix.refs }} target: ${{ matrix.target }} ]" 24 | runs-on: ubuntu-latest 25 | strategy: 26 | fail-fast: False 27 | matrix: 28 | target: 29 | - test 30 | refs: 31 | - 'master' 32 | - '0.3' 33 | steps: 34 | 35 | # ------------------------------------------------------------ 36 | # Checkout repository 37 | # ------------------------------------------------------------ 38 | - name: Checkout repository 39 | uses: actions/checkout@v2 40 | with: 41 | fetch-depth: 0 42 | ref: ${{ matrix.refs }} 43 | 44 | - name: "git-info.sh" 45 | run: | 46 | 47 | # Retrieve git info (tags, etc) 48 | git fetch --all 49 | make ${TARGET} 50 | env: 51 | TARGET: ${{ matrix.target }} 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Git Tools 2 | 3 | 4 | ## git-info.sh 5 | 6 | ### Motivation 7 | 8 | This is mainly used for GitHub Actions to figure out if a current build (independently of PR, push or cronjob) is on a specific branch or tag or something else. 9 | 10 | ### GitHub Actions 11 | 12 | ```yaml 13 | steps: 14 | - name: Checkout repository 15 | uses: actions/checkout@v2 16 | with: 17 | fetch-depth: 0 18 | 19 | - name: Set variables 20 | id: vars 21 | run: | 22 | # BRANCH, TAG or COMMIT 23 | GIT_TYPE="$( \ 24 | curl -sS https://raw.githubusercontent.com/cytopia/git-tools/master/git-info.sh \ 25 | | sh \ 26 | | grep '^GIT_TYPE' \ 27 | | sed 's|.*=||g' \ 28 | )" 29 | # Branch name, Tag name or Commit Hash 30 | GIT_SLUG="$( \ 31 | curl -sS https://raw.githubusercontent.com/cytopia/git-tools/master/git-info.sh \ 32 | | sh \ 33 | | grep '^GIT_NAME' \ 34 | | sed 's|.*=||g' \ 35 | )" 36 | # Export variable for later stage 37 | # https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files 38 | echo "GIT_TYPE=${GIT_TYPE}" >> $GITHUB_ENV 39 | echo "GIT_SLUG=${GIT_SLUG}" >> $GITHUB_ENV 40 | 41 | - name: Publish docker image 42 | run: | 43 | # Use git tag name or git branch name as docker tag 44 | docker push image:${GIT_SLUG} 45 | ``` 46 | 47 | ### See usage in action 48 | 49 | Will output the correct git name and type: 50 | 51 | Branch: `release-0.24` 52 | ```bash 53 | $ git-info.sh 54 | GIT_NAME=release-0.24 55 | GIT_TYPE=BRANCH 56 | GIT_HEAD=892f8fc 57 | 58 | DETACHED_FROM= 59 | BEHIND=0 60 | ``` 61 | 62 | HEAD detached at `a78bccfd` (latest commit in `master` branch) 63 | ```bash 64 | $ git-info.sh 65 | GIT_NAME=master 66 | GIT_TYPE=BRANCH 67 | GIT_HEAD=a78bccf 68 | 69 | DETACHED_FROM=master 70 | BEHIND=0 71 | ``` 72 | 73 | Git tag `0.9` (tag in `master` branch) 74 | ```bash 75 | $ git-info.sh 76 | GIT_NAME=0.9 77 | GIT_TYPE=TAG 78 | GIT_HEAD=a7bdd40 79 | 80 | DETACHED_FROM=master 81 | BEHIND=0 82 | ``` 83 | 84 | HEAD detached at `05faee2` (two commits behind `master` branch) 85 | ```bash 86 | $ git-info.sh 87 | GIT_NAME=05faee2 88 | GIT_TYPE=COMMIT 89 | GIT_HEAD=05faee2 90 | 91 | DETACHED_FROM=master 92 | BEHIND=2 93 | ``` 94 | --------------------------------------------------------------------------------