Merge pull request #86 from keithhubner/feature-branch #132
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Version Tag Build Publish | |
on: | |
push: | |
branches: [ "main" ] | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY: ghcr.io | |
REPO_NAME: ${{ github.event.repository.name }} | |
# github.repository as <account>/<repo> | |
IMAGE_NAME: keithhubner/devops-exercise | |
IMAGE_TAG: 1.0.${{ github.run_number }} # GITHUB_RUN_NUMBER }} | |
jobs: | |
release-runner: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Get Tags | |
run: | | |
git fetch --tags | |
echo "Tags fetched" | |
- name: Set Origin | |
run: | | |
git remote set-url origin https://github.com/keithhubner/devops-exercise.git | |
- name: Get the latest tag, if available | |
id: last_tag | |
run: | | |
echo "::set-output name=TAG::$(git describe --tags `git rev-list --tags --max-count=1`)" | |
shell: bash | |
- name: Display last tag | |
run: echo "The last tag is ${{ steps.last_tag.outputs.TAG }}" | |
- name: Parse the last tag | |
id: parse-last-tag | |
run: | | |
LAST_TAG="${{ steps.last_tag.outputs.TAG }}" | |
# Remove the 'v' prefix if present | |
TAG=${LAST_TAG#v} | |
IFS='.' read -ra PARTS <<< "$TAG" | |
MAJOR_LAST="${PARTS[0]}" | |
MINOR_LAST="${PARTS[1]}" | |
PATCH_LAST="${PARTS[2]}" | |
echo "Major from Last Tag: $MAJOR_LAST" | |
echo "Minor from Last Tag: $MINOR_LAST" | |
echo "Patch from Last Tag: $PATCH_LAST" | |
echo "::set-output name=major_last::$MAJOR_LAST" | |
echo "::set-output name=minor_last::$MINOR_LAST" | |
echo "::set-output name=patch_last::$PATCH_LAST" | |
- name: Get current version from version.json | |
id: get-version-json | |
run: | | |
CURRENT_VERSION_JSON=$(jq -r .baseVersion version.json) | |
echo "Current Version from baseVersion.json: $CURRENT_VERSION_JSON" | |
echo "::set-output name=current_version_json::$CURRENT_VERSION_JSON" | |
- name: Parse current version from version.json | |
id: parse-version-json | |
run: | | |
CURRENT_VERSION_JSON="${{ steps.get-version-json.outputs.current_version_json }}" | |
IFS='.' read -ra PARTS_JSON <<< "$CURRENT_VERSION_JSON" | |
MAJOR_JSON="${PARTS_JSON[0]}" | |
MINOR_JSON="${PARTS_JSON[1]}" | |
PATCH_JSON="${PARTS_JSON[2]}" | |
echo "Major from version.json: $MAJOR_JSON" | |
echo "Minor from version.json: $MINOR_JSON" | |
echo "Patch from version.json: $PATCH_JSON" | |
echo "::set-output name=major_json::$MAJOR_JSON" | |
echo "::set-output name=minor_json::$MINOR_JSON" | |
echo "::set-output name=patch_json::$PATCH_JSON" | |
- name: Calculate new version | |
id: calculate-new-version | |
run: | | |
MAJOR_LAST="${{ steps.parse-last-tag.outputs.major_last }}" | |
MINOR_LAST="${{ steps.parse-last-tag.outputs.minor_last }}" | |
PATCH_LAST="${{ steps.parse-last-tag.outputs.patch_last }}" | |
MAJOR_JSON="${{ steps.parse-version-json.outputs.major_json }}" | |
MINOR_JSON="${{ steps.parse-version-json.outputs.minor_json }}" | |
PATCH_JSON="${{ steps.parse-version-json.outputs.patch_json }}" | |
if [[ "$MAJOR_LAST" -lt "$MAJOR_JSON" || "$MINOR_LAST" -lt "$MINOR_JSON" ]]; then | |
NEW_MAJOR="$MAJOR_JSON" | |
NEW_MINOR="$MINOR_JSON" | |
NEW_PATCH="0" | |
else | |
NEW_MAJOR="$MAJOR_LAST" | |
NEW_MINOR="$MINOR_LAST" | |
NEW_PATCH=$((PATCH_LAST + 1)) | |
fi | |
NEW_VERSION="$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH" | |
echo "New Version: $NEW_VERSION" | |
echo "::set-output name=new_version::$NEW_VERSION" | |
- name: Create and push new tag | |
id: create-tag | |
if: steps.calculate-new-version.outputs.new_version != '' | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
NEW_VERSION="${{ steps.calculate-new-version.outputs.new_version }}" | |
git config user.name "GitHub Actions" | |
git config user.email "actions@github.com" | |
git tag -a "$NEW_VERSION" -m "Incremented version to $NEW_VERSION" | |
if git push origin "$NEW_VERSION"; then | |
echo "::set-output name=tag_created::true" | |
else | |
echo "::set-output name=tag_created::false" | |
fi | |
- name: Get commit messages since the last tag | |
id: get-commit-messages | |
run: | | |
LAST_TAG="${{ steps.calculate-new-version.outputs.new_version }}" | |
# Remove the 'v' prefix if present | |
TAG=${LAST_TAG#v} | |
COMMIT_MESSAGES=$(git log -1 --pretty=%B) | |
echo "Commit Messages: $COMMIT_MESSAGES" | |
echo "::set-output name=commit_messages::$COMMIT_MESSAGES" | |
- name: Create GitHub Release | |
id: create-release | |
if: steps.create-tag.outputs.tag_created == 'true' | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
NEW_VERSION="${{ steps.calculate-new-version.outputs.new_version }}" | |
COMMIT_MESSAGES="${{ steps.get-commit-messages.outputs.commit_messages }}" | |
RELEASE_TITLE="Release $NEW_VERSION" | |
RELEASE_BODY="Releasing $NEW_VERSION ### Changes ### $COMMIT_MESSAGES" | |
echo "::set-output name=release_body::$RELEASE_BODY" | |
echo $RELEASE_BODY | |
echo $RELEASE_TITLE | |
- name: Create Release | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
id: publish-release | |
if: steps.create-release.outputs.release_body != '' | |
run: | | |
NEW_VERSION="${{ steps.calculate-new-version.outputs.new_version }}" | |
RELEASE_BODY="${{ steps.create-release.outputs.release_body }}" | |
RELEASE_TITLE="${{ steps.create-release.outputs.release_title }}" | |
gh release create "$NEW_VERSION" -t "$RELEASE_TITLE" -n "$RELEASE_BODY" | |
- name: Build and push Docker image | |
env: | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
TAG: ${{ steps.calculate-new-version.outputs.new_version }} | |
run: | | |
echo $password | docker login ghcr.io -u $username --password-stdin | |
docker build --build-arg SET_VERSION="3.1.2" -t ghcr.io/keithhubner/$REPO_NAME:$TAG . | |
docker push ghcr.io/keithhubner/$REPO_NAME:$TAG | |
- name: Update 'latest' tag | |
env: | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
TAG: ${{ steps.calculate-new-version.outputs.new_version }} | |
run: | | |
echo $password | docker login ghcr.io -u $username --password-stdin | |
docker pull ghcr.io/keithhubner/$REPO_NAME:$TAG # Pull the specified version | |
docker tag ghcr.io/keithhubner/$REPO_NAME:$TAG ghcr.io/keithhubner/$REPO_NAME:latest # Tag 'latest' | |
docker push ghcr.io/keithhubner/$REPO_NAME:latest # Push 'latest' to the registry | |