Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Auto tagging and auto releasing #6

Merged
merged 6 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .chglog/CHANGELOG.tpl.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{ if .Versions -}}
<a name="unreleased"></a>
## [Unreleased]

{{ if .Unreleased.CommitGroups -}}
<a name="unreleased"></a>
## [Unreleased]
{{ range .Unreleased.CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
Expand Down Expand Up @@ -47,7 +47,8 @@
{{ end -}}

{{- if .Versions }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD
{{ if .Unreleased.CommitGroups }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD{{ end }}
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
- name: Generate files for release
uses: ./
with:
entrypoint: "/generate-readme.sh"
entrypoint: "/pre-release.sh"
13 changes: 13 additions & 0 deletions .github/workflows/release-draft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ on:
- master

jobs:
readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: master
- name: Should generate README.md and CHANGELOG for this REPO
uses: ./
with:
entrypoint: "/generate-readme.sh"
tf_docs_git_push: 'true'

update_release_draft:
needs: ['readme']
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Release Tasks
on:
release:
types: [published]
jobs:
tag:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Should generate README.md for this REPO
uses: ./
with:
entrypoint: "/release.sh"
tf_docs_git_push: 'true'
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM derekrada/terraform-docs:v1.0.1
COPY ./src/common.sh /common.sh
COPY ./src/docker-entrypoint.sh /docker-entrypoint.sh
COPY ./src/generate-readme.sh /generate-readme.sh
COPY ./src/pre-release.sh /pre-release.sh
COPY ./src/release.sh /release.sh
COPY ./src/default.tpl /default_template.tpl

ENTRYPOINT ["/docker-entrypoint.sh"]
35 changes: 33 additions & 2 deletions src/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ git_changed () {
echo "::set-output name=num_changed::${GIT_FILES_CHANGED}"
}

git_commit () {
git_setup () {
git config --global user.name ${GITHUB_ACTOR}
git config --global user.email ${GITHUB_ACTOR}@users.noreply.github.com
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
}

git_commit () {
git_changed
if [ "${GIT_FILES_CHANGED}" -eq 0 ]; then
echo "::debug file=common.sh,line=20,col=1 No files changed, skipping commit"
else
git commit -m "${INPUT_TF_DOCS_GIT_COMMIT_MESSAGE}"
git push
fi
}

Expand Down Expand Up @@ -68,3 +71,31 @@ update_doc () {
echo "${MY_DOC}"
fi
}

update_meta () {
NEW_VERSION="${1}"
MAJOR_VERSION=`echo "${NEW_VERSION}" | cut -d. -f1`
echo "major_version: ${MAJOR_VERSION}" > /tmp/version.yml
echo "version: ${NEW_VERSION}" >> /tmp/version.yml
gomplate -d meta=.github/meta.yml -d newmeta=/tmp/version.yml -i '{{ ds "meta" | coll.Merge (ds "newmeta") | data.ToYAML }}' -o /tmp/meta.yml
cat /tmp/meta.yml > .github/meta.yml
git_add_doc ".github/meta.yml"
}

update_readme () {
# generate README.md with version
gomplate -d action=action.yml -d meta=.github/meta.yml -f .github/templates/README.tpl -o README.md
git_add_doc "./README.md"
}

overwrite_docker_tag () {
NEW_TAG="${1:-"latest"}"
# update the dockerfile to be locked down at this specific version
sed -i "s|FROM derekrada/terraform-docs:.*|FROM derekrada/terraform-docs:${NEW_TAG}|" ./Dockerfile
git_add_doc "./Dockerfile"
}

generate_change_log () {
git-chglog --tag-filter-pattern '^v[0-9]+.+' -o CHANGELOG.md
git_add_doc "./CHANGELOG.md"
}
2 changes: 2 additions & 0 deletions src/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fi

# go to github repo
cd $GITHUB_WORKSPACE
git_setup

if [ -f "${GITHUB_WORKSPACE}/${INPUT_TF_DOCS_ATLANTIS_FILE}" ]; then

Expand Down Expand Up @@ -41,6 +42,7 @@ fi

if [ "${INPUT_TF_DOCS_GIT_PUSH}" = "true" ]; then
git_commit
git push
else
git_changed
fi
Expand Down
50 changes: 5 additions & 45 deletions src/generate-readme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,12 @@ set -e

. /common.sh

export INPUT_TF_DOCS_GIT_COMMIT_MESSAGE="chore: automated release process"
export INPUT_TF_DOCS_GIT_COMMIT_MESSAGE="chore: automated generate readme"
cd $GITHUB_WORKSPACE
git_setup

TAG_PREFIX="v"
RELEASE_BRANCH_VERSION=`git rev-parse --abbrev-ref HEAD | sed "s|release/||" | sed "s/${TAG_PREFIX}//"`
RELEASE_BRANCH_SHA=`git rev-parse HEAD`

set +e
git describe --contains "${RELEASE_BRANCH_SHA}" 2>/dev/null
IS_TAGGED=$?
set -e

if [ "${IS_TAGGED}" -eq 0 ]; then
echo "Release is already tagged"
exit
fi

MAJOR_VERSION=`echo $RELEASE_BRANCH_VERSION | cut -d. -f1`
MINOR_VERSION=`echo $RELEASE_BRANCH_VERSION | cut -d. -f2`
PATCH_VERSION=`echo $RELEASE_BRANCH_VERSION | cut -d. -f3`

if [ -z "$MAJOR_VERSION" ] || [ -z "$MINOR_VERSION" ] || [ -z "$PATCH_VERSION" ]; then
echo "Invalid release branch name"
exit 1
fi

NEW_VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}"

# update meta.yml
echo "major_version: ${MAJOR_VERSION}" > .github/meta.yml
echo "version: ${NEW_VERSION}" >> .github/meta.yml
git_add_doc ".github/meta.yml"

# generate README.md with version
gomplate -d action=action.yml -d meta=.github/meta.yml -f .github/templates/README.tpl -o README.md
git_add_doc "./README.md"

# generate CHANGELOG.md
git-chglog --tag-filter-pattern '^v[0-9]+.+' --next-tag "${TAG_PREFIX}${NEW_VERSION}" -o CHANGELOG.md
git_add_doc "./CHANGELOG.md"

# update the dockerfile to be locked down at this specific version
sed -i "s|FROM derekrada/terraform-docs:latest|FROM derekrada/terraform-docs:${TAG_PREFIX}${NEW_VERSION}|" ./Dockerfile
git_add_doc "./Dockerfile"
update_readme
generate_change_log

git_commit

git tag -f "${TAG_PREFIX}${RELEASE_BRANCH_VERSION}"
git push --tags
git push
73 changes: 73 additions & 0 deletions src/pre-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
set -e

. /common.sh

export INPUT_TF_DOCS_GIT_COMMIT_MESSAGE="chore: automated release process"
cd $GITHUB_WORKSPACE
git_setup

export TAG_PREFIX="v"
export MAJOR_VERSION=0
export MINOR_VERSION=0
export PATCH_VERSION=1
export NEW_VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}"

parse_version () {
VERSION="${1}"
MAJOR_VERSION=`echo $VERSION | cut -d. -f1`
MINOR_VERSION=`echo $VERSION | cut -d. -f2`
PATCH_VERSION=`echo $VERSION | cut -d. -f3`

if [ -z "$MAJOR_VERSION" ] || [ -z "$MINOR_VERSION" ] || [ -z "$PATCH_VERSION" ]; then
echo "failed to parse version=${VERSION}"
exit 1
fi
NEW_VERSION=${VERSION}
}

is_sha_tagged () {
set +e
git describe --contains "${SHA}" 2>/dev/null
IS_TAGGED=$?
set -e

if [ "${IS_TAGGED}" -eq 0 ]; then
echo "Release is already tagged"
exit
fi
}

create_release () {

# update the meta.yml only done for v1.x.x
update_meta "${NEW_VERSION}"

# update the readme unaffected by major_version changes
update_readme

# replace the module Dockerfile only done for v1.x.x
overwrite_docker_tag "${TAG_PREFIX}${NEW_VERSION}"

# generate changelog.md for the next tag
git-chglog --tag-filter-pattern '^v[0-9]+.+' \
--next-tag "${TAG_PREFIX}${NEW_VERSION}" \
"${TAG_PREFIX}${NEW_VERSION}" \
-o /tmp/tag_msg.md

git-chglog --tag-filter-pattern '^v[0-9]+.+' --next-tag "${TAG_PREFIX}${NEW_VERSION}" -o CHANGELOG.md
git_add_doc "./CHANGELOG.md"

# commit and push
git_commit
git push
git tag -f "${TAG_PREFIX}${NEW_VERSION}" -a --file /tmp/tag_msg.md
git push --tags
}

RELEASE_BRANCH_VERSION=`git rev-parse --abbrev-ref HEAD | sed "s|release/||" | sed "s/${TAG_PREFIX}//"`
RELEASE_BRANCH_SHA=`git rev-parse HEAD`

parse_version "${RELEASE_BRANCH_VERSION}"
is_sha_tagged "${RELEASE_BRANCH_SHA}"
create_release "${TAG_PREFIX}${NEW_VERSION}"
12 changes: 12 additions & 0 deletions src/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e

. /common.sh

export INPUT_TF_DOCS_GIT_COMMIT_MESSAGE="chore: automated release process"
cd $GITHUB_WORKSPACE
git_setup

VERSION=`gomplate -d meta=.github/meta.yml 'v{{ (ds "meta").major_version }}'`
git tag -f "${VERSION}"
git push --tags