Skip to content

Instantly share code, notes, and snippets.

@danielcrenna
Forked from fredeil/README.md
Last active July 23, 2020 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcrenna/2aadaa5c0ba01a1e755dcd23296c6b0c to your computer and use it in GitHub Desktop.
Save danielcrenna/2aadaa5c0ba01a1e755dcd23296c6b0c to your computer and use it in GitHub Desktop.
GitHub action for versioning your repository using NBGV

Simple versioning of your repository

Using NBGV to tag your master branch on push.

name: tag-master-branch
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v1
- uses: aarnott/nbgv@master
with:
setAllVars: true
- run: echo "VERSION= $NBGV_Version"
- name: Chmod shell script
run: chmod +x ./version.sh
shell: bash
continue-on-error: true
- name: Version
run: ./version.sh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
{
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/tags/v\\d+\\.\\d+"
]
}
#!/bin/bash
# Get the latest tag
tag=$(git describe --tags `git rev-list --tags --max-count=1`)
tag_commit=$(git rev-list -n 1 $tag)
# Get current commit hash for tag
commit=$(git rev-parse HEAD)
if [ "$tag_commit" == "$commit" ]; then
echo "No new commits since previous tag.."
exit 1
fi
if [ ! -f "version.json" ]; then
echo "Could not find version.json file.."
exit 1
fi
# Prefix release with 'v'
new="v$NBGV_Version"
echo ::set-output name=new_tag::$new
# Push new tag ref to GitHub
dt=$(date '+%Y-%m-%dT%H:%M:%SZ')
full_name=$GITHUB_REPOSITORY
git_refs_url=$(jq .repository.git_refs_url $GITHUB_EVENT_PATH | tr -d '"' | sed 's/{\/sha}//g')
echo "$dt: pushing tag $new to repo $full_name"
echo $git_refs_url
cat $GITHUB_EVENT_PATH | jq '.'
curl -s -X POST $git_refs_url \
-H "Authorization: token $GITHUB_TOKEN" \
-d @- << EOF
{
"ref": "refs/tags/$new",
"sha": "$commit"
}
EOF
@danielcrenna
Copy link
Author

@fredeil I needed to add an additional env statement in my fork, to pass through to the shell script, otherwise the token was replaced with an empty string. Maybe a recent change to GitHub Actions.

@fredeil
Copy link

fredeil commented Jul 23, 2020

Cool thanks @danielcrenna. Will update it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment