Merge pull request #66 from ekline-io/dj/EK-666-Daily-Job-Review #148
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: release | |
on: | |
push: | |
branches: | |
- main | |
tags: | |
- "v*.*.*" | |
pull_request: | |
types: | |
- labeled | |
repository_dispatch: | |
types: | |
- ekline-cli-release | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ekline-ci-cd | |
jobs: | |
release: | |
if: github.event_name != 'repository_dispatch' && github.event.action != 'labeled' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
# Bump version on merging Pull Requests with specific labels. | |
# (bump:major,bump:minor,bump:patch) | |
- id: bumpr | |
if: "!startsWith(github.ref, 'refs/tags/')" | |
uses: haya14busa/action-bumpr@v1 | |
# Get tag name. | |
- id: tag | |
uses: haya14busa/action-cond@v1 | |
with: | |
cond: "${{ startsWith(github.ref, 'refs/tags/') }}" | |
if_true: ${{ github.ref }} | |
if_false: ${{ steps.bumpr.outputs.next_version }} | |
# Create release | |
- if: "steps.tag.outputs.value != ''" | |
env: | |
TAG_NAME: ${{ steps.tag.outputs.value }} | |
BODY: ${{ steps.bumpr.outputs.message }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh release create "${TAG_NAME}" -t "Release ${TAG_NAME/refs\/tags\//}" --notes "${BODY}" | |
- if: "steps.tag.outputs.value != ''" | |
name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/ekline-io/${{ env.IMAGE_NAME }} | |
tags: | | |
type=semver,pattern=v{{version}},value=${{ steps.tag.outputs.value }} | |
type=semver,pattern=v{{major}}.{{minor}},value=${{ steps.tag.outputs.value }} | |
type=semver,pattern=v{{major}},value=${{ steps.tag.outputs.value }} | |
type=raw,value=latest | |
- if: "steps.tag.outputs.value != ''" | |
name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- if: "steps.tag.outputs.value != ''" | |
name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
# Update corresponding major and minor tag. | |
# e.g. Update v1 and v1.2 when releasing v1.2.3 | |
- uses: haya14busa/action-update-semver@v1 | |
if: "!steps.bumpr.outputs.skip" | |
with: | |
tag: ${{ steps.bumpr.outputs.next_version }} | |
- id: "tag_create" | |
if: "!steps.bumpr.outputs.skip" | |
uses: rickstaa/action-create-tag@v1 | |
with: | |
tag: "latest" | |
force_push_tag: true | |
message: "Latest release" | |
check-version: | |
if: github.event_name == 'repository_dispatch' | |
runs-on: ubuntu-latest | |
outputs: | |
versions_match: ${{ steps.compare_versions.outputs.match }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Ensure all history and tags are fetched | |
# Step 1: Identify Release Type and Version from Dispatch Event | |
- name: Identify Release Type and Version | |
id: identify_release | |
run: | | |
RELEASE_TYPE="${{ github.event.client_payload.releaseType }}" | |
RELEASE_VERSION="${{ github.event.client_payload.releaseVersion }}" | |
if [ -z "$RELEASE_TYPE" ]; then | |
RELEASE_TYPE="patch" | |
fi | |
echo "releaseType=$RELEASE_TYPE" >> $GITHUB_OUTPUT | |
echo "releaseVersion=$RELEASE_VERSION" >> $GITHUB_OUTPUT | |
echo "Determined release type: $RELEASE_TYPE" | |
echo "Release version: $RELEASE_VERSION" | |
# Step 2: Extract Major Version from Release Version | |
- name: Extract Major Version from Release Version | |
id: extract_release_version | |
run: | | |
RELEASE_VERSION="${{ steps.identify_release.outputs.releaseVersion }}" | |
RELEASE_MAJOR_VERSION=$(echo "$RELEASE_VERSION" | cut -d'.' -f1) | |
echo "release_major_version=$RELEASE_MAJOR_VERSION" >> $GITHUB_OUTPUT | |
echo "Release major version: $RELEASE_MAJOR_VERSION" | |
# Step 3: Extract Major Version from Dockerfile | |
- name: Extract Major Version from Dockerfile | |
id: extract_dockerfile_version | |
run: | | |
DOCKERFILE_PATH="Dockerfile" | |
DOCKERFILE_MAJOR_VERSION=$(grep '^FROM ghcr.io/ekline-io/ekline-cli:' "$DOCKERFILE_PATH" | sed 's/.*:\([0-9]\+\).*/\1/') | |
echo "dockerfile_major_version=$DOCKERFILE_MAJOR_VERSION" >> $GITHUB_OUTPUT | |
echo "Dockerfile major version: $DOCKERFILE_MAJOR_VERSION" | |
# Step 4: Compare Major Versions | |
- name: Compare Major Versions | |
id: compare_versions | |
run: | | |
if [ "${{ steps.extract_dockerfile_version.outputs.dockerfile_major_version }}" != "${{ steps.extract_release_version.outputs.release_major_version }}" ]; then | |
echo "Major versions do not match. Skipping dispatch release." | |
echo "match=false" >> $GITHUB_OUTPUT | |
else | |
echo "Major versions match." | |
echo "match=true" >> $GITHUB_OUTPUT | |
fi | |
dispatch-release: | |
needs: [check-version] | |
if: github.event_name == 'repository_dispatch' && needs.check-version.outputs.versions_match == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Ensure all history and tags are fetched | |
# Step 1: Identify Release Type from Dispatch Event | |
- name: Identify Release Type | |
id: identify_release | |
run: | | |
RELEASE_TYPE="${{ github.event.client_payload.releaseType }}" | |
if [ -z "$RELEASE_TYPE" ]; then | |
RELEASE_TYPE="patch" | |
fi | |
echo "releaseType=$RELEASE_TYPE" >> $GITHUB_OUTPUT | |
echo "Determined release type: $RELEASE_TYPE" | |
- name: Get Latest Tag | |
id: get_latest_tag | |
run: | | |
# Get the latest release tag | |
LATEST_TAG=$(gh release view --json tagName --jq '.tagName') | |
echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT | |
echo "Latest tag: $LATEST_TAG" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Step 3: Bump Version Based on Release Type | |
- name: Bump Version | |
id: bump_version | |
run: | | |
LATEST_TAG="${{ steps.get_latest_tag.outputs.latest_tag }}" | |
RELEASE_TYPE="${{ steps.identify_release.outputs.releaseType }}" | |
# Ensure the latest tag starts with 'v' | |
if [[ "$LATEST_TAG" != v* ]]; then | |
echo "Latest tag does not start with 'v'. Cannot proceed." | |
exit 1 | |
fi | |
# Extract version numbers | |
VERSION=${LATEST_TAG#v} | |
# Split version into major, minor, patch | |
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" | |
# Bump the version based on releaseType | |
if [[ "$RELEASE_TYPE" == "major" ]]; then | |
MAJOR=$((MAJOR + 1)) | |
MINOR=0 | |
PATCH=0 | |
elif [[ "$RELEASE_TYPE" == "minor" ]]; then | |
MINOR=$((MINOR + 1)) | |
PATCH=0 | |
elif [[ "$RELEASE_TYPE" == "patch" ]]; then | |
PATCH=$((PATCH + 1)) | |
else | |
echo "Invalid releaseType: $RELEASE_TYPE" | |
exit 1 | |
fi | |
# Construct new version and tag | |
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}" | |
NEW_TAG="v${NEW_VERSION}" | |
echo "New version: $NEW_VERSION" | |
echo "New tag: $NEW_TAG" | |
# Create the new tag pointing to the current commit | |
git tag "$NEW_TAG" | |
git push origin "$NEW_TAG" | |
# Set the new tag as output | |
echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT | |
# Set the next version as output | |
echo "next_version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
# Step 4: Create GitHub Release | |
- name: Create GitHub Release | |
if: steps.bump_version.outcome == 'success' | |
run: | | |
gh release create ${{ steps.bump_version.outputs.new_tag }} \ | |
--title "Release ${{ steps.bump_version.outputs.new_tag }}" \ | |
--notes "Updated EkLine CLI version to ${{ github.event.client_payload.releaseVersion }}" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/ekline-io/${{ env.IMAGE_NAME }} | |
tags: | | |
type=semver,pattern=v{{version}},value=${{ steps.bump_version.outputs.new_tag }} | |
type=semver,pattern=v{{major}}.{{minor}},value=${{ steps.bump_version.outputs.new_tag }} | |
type=semver,pattern=v{{major}},value=${{ steps.bump_version.outputs.new_tag }} | |
type=raw,value=latest | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
# Update corresponding major and minor tag. | |
# e.g. Update v1 and v1.2 when releasing v1.2.3 | |
- uses: haya14busa/action-update-semver@v1 | |
if: "!steps.bumpr.outputs.skip" | |
with: | |
tag: ${{ steps.bump_version.outputs.next_version }} | |
- id: "tag_create" | |
if: "!steps.bumpr.outputs.skip" | |
uses: rickstaa/action-create-tag@v1 | |
with: | |
tag: "latest" | |
force_push_tag: true | |
message: "Latest release" | |
release-check: | |
if: github.event.action == 'labeled' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Post bumpr status comment | |
uses: haya14busa/action-bumpr@v1 |