Skip to content

Commit

Permalink
Added local dev and release tooling to changelog tool
Browse files Browse the repository at this point in the history
  • Loading branch information
fheinecke committed Jul 17, 2024
1 parent 5d79bdb commit 161f5f3
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
134 changes: 134 additions & 0 deletions .github/workflows/changelog-cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
name: Changelog generator CD

on:
push:
branches:
- main
paths:
- tools/changelog
tags:
- "v[0-9]+.[0-9]+.[0-9]+**-changelog"
pull_request:
paths:
- tools/changelog/workflows/cd.yaml

env:
TOOL_DIRECTORY: tools/changelog

jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write # Needed to create the release
packages: write # Needed to upload the images to GHCR
steps:
# Setup
- name: Checkout repository
uses: actions/checkout@v4
- name: Create event-specific values
id: setup
run: |
# Determine if the workflow was triggered via a push to main or a tag
# and get the version based off of that
if [[ "${GITHUB_REF}" =~ refs/tags/.* ]]; then
# Transforms tag refs like refs/tags/v1.2.3-changelog into v1.2.3
TAG="${GITHUB_REF#refs/tags/}"
echo "version=${TAG%-changelog}" >> "${GITHUB_OUTPUT}"
# Eventually the parse-version action from the teleport.e repo
# should move into this repo and replace this logic
echo "should-release=true" >> "${GITHUB_OUTPUT}"
if [[ "${GITHUB_REF%-*}" == "${GITHUB_REF}" ]]; then
echo "is-prerelease=true" >> "${GITHUB_OUTPUT}"
fi
fi
# Build the binaries
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: '${{ env.TOOL_DIRECTORY }}/go.mod'

- name: Build the project
working-directory: ${{ env.TOOL_DIRECTORY }}
run: |
make tarball OS=linux ARCH=amd64
make tarball OS=linux ARCH=arm64
make tarball OS=darwin ARCH=amd64
make tarball OS=darwin ARCH=arm64
# Build and push the image
- name: Install docker buildx
uses: docker/[email protected]

- name: Login to GitHub Container Registry
id: login-ghcr
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Prepare container image labels and tags
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ github.repository_owner }}/changelog
flavor: |
latest=false
# Enable sha tag on branch push events and pull requests.
# Enable semver tags on tag push events, but don't overwrite major/minor tags for prereleases.
# Semver tags won't be generated except upon tag events.
tags: |
type=sha,prefix=v0.0.0-{{branch}}-,enable=${{ startsWith(github.ref, 'refs/heads/') }}
type=sha,prefix=v0.0.0-{{base_ref}}-,enable=${{ github.event_name == 'pull_request' }}
type=semver,pattern={{major}},value=${{ steps.setup.outputs.version }},enable=${{ steps.setup.outputs.is-prerelease != 'true' }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.setup.outputs.version }},enable=${{ steps.setup.outputs.is-prerelease != 'true' }}
type=semver,pattern={{version}},value=${{ steps.setup.outputs.version }}
- name: Build the container image and push
uses: docker/build-push-action@v5
with:
context: ${{ env.TOOL_DIRECTORY }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: |
linux/amd64
linux/arm64
provenance: true
sbom: true

# File a new release with the tarballs attached
- name: Create a new GitHub release
if: ${{ steps.setup.outputs.should-release == 'true' }}
working-directory: ${{ env.TOOL_DIRECTORY }}
env:
VERSION: ${{ steps.setup.outputs.version }}
IS_PRERELEASE: ${{ steps.setup.outputs.is-prerelease }}
run: |
if [[ "${IS_PRERELEASE}" == 'true' ]]; then
EXTRA_FLAGS=("--prerelease")
else
EXTRA_FLAGS=("--latest")
fi
readarray -d '' RELEASE_TARBALLS < <(
find . -name '*.tar.gz' -print0
)
echo "Creating a release for ${VERSION} with files:"
for file in "${RELEASE_TARBALLS[@]}"; do
ls -lh "${file}"
done
gh release create --title "changelog ${VERSION}" --verify-tag \
--generate-notes "${EXTRA_FLAGS[@]}" "${GITHUB_REF_NAME}" \
"${RELEASE_TARBALLS[@]}"
52 changes: 52 additions & 0 deletions .github/workflows/changelog-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
name: Changelog generator CI

on:
pull_request:

env:
TOOL_DIRECTORY: tools/changelog

jobs:
test:
name: Run tests
runs-on: ubuntu-latest
steps:
# Determine if tests should run
# This is a monorepo and GH checks cannot be required for only specific
# paths, so this is required instead of a trigger `paths` filter.
- name: Check if relavent files have changed
id: changes
uses: dorny/[email protected]
with:
filters: |
tool:
- 'tools/changelog/**'
# Setup
- name: Checkout repository
if: steps.changes.outputs.tool == 'true'
uses: actions/checkout@v4
- name: Setup Go
if: steps.changes.outputs.tool == 'true'
uses: actions/setup-go@v5
with:
go-version-file: "${{ env.TOOL_DIRECTORY }}/go.mod"

# Linting
- name: Install golangci-lint
if: steps.changes.outputs.tool == 'true'
run: go install github.com/golangci/golangci-lint/cmd/[email protected]
- name: Lint
if: steps.changes.outputs.tool == 'true'
working-directory: ${{ env.TOOL_DIRECTORY }}
run: make lint

# Tests
- name: Install gotestsum
if: steps.changes.outputs.tool == 'true'
run: go install gotest.tools/[email protected]
- name: Run tests
if: steps.changes.outputs.tool == 'true'
working-directory: ${{ env.TOOL_DIRECTORY }}
run: make test
1 change: 1 addition & 0 deletions tools/changelog/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/*
9 changes: 9 additions & 0 deletions tools/changelog/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM gcr.io/distroless/static-debian12

ARG TARGETARCH
ARG TARGETOS
ARG FILE_NAME="changelog"
ARG SOURCE_FILE_PATH="build/${TARGETOS}/${TARGETARCH}/${FILE_NAME}"
COPY "${SOURCE_FILE_PATH}" "/bin/tool"

ENTRYPOINT [ "/bin/tool" ]
32 changes: 32 additions & 0 deletions tools/changelog/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
OS ?= $(shell uname -s | tr '[[:upper:]]' '[[:lower:]]')
ARCH ?= $(shell uname -m)
TOOL_NAME ?= changelog
BUILD_DIR ?= build/$(OS)/$(ARCH)/
VERSION ?= v0.0.0-dev

lint:
@golangci-lint run ./... --out-format colored-line-number

test:
@gotestsum --format github-actions ./... -- -shuffle on -timeout 2m -race

binary:
@echo "Building for $(OS)/$(ARCH) and writing to $(BUILD_DIR)"
@mkdir -p "$(BUILD_DIR)"
@GOOS=$(OS) GOARCH=$(ARCH) go build -o "$(BUILD_DIR)/" -ldflags="-s -w"

tarball: TARBALL_NAME = "$(TOOL_NAME)-$(VERSION)-$(OS)-$(ARCH).tar.gz"
tarball: binary
@tar -C "$(BUILD_DIR)" -czvf "$(BUILD_DIR)/$(TARBALL_NAME)" "$(TOOL_NAME)" > /dev/null

container-image: OS = linux
container-image: binary
@docker buildx build --platform="linux/$(ARCH)" -t "$(TOOL_NAME)" .

clean:
@rm -rf build/
@if [ -n "$(shell docker image ls --quiet --filter "reference=$(TOOL_NAME):$(VERSION)" )" ]; then \
docker image rm -f "$(TOOL_NAME):$(VERSION)"; \
fi

.PHONY: lint test binary tarball container-image clean
1 change: 1 addition & 0 deletions tools/changelog/workflows/cd.yaml
1 change: 1 addition & 0 deletions tools/changelog/workflows/ci.yaml

0 comments on commit 161f5f3

Please sign in to comment.