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

Push versioned dashboard images on release #51

Merged
merged 2 commits into from
Feb 22, 2024
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
137 changes: 137 additions & 0 deletions .github/scripts/get_release_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# ------------------------------------------------------------
# Copyright 2023 The Radius Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------

# This script parses release version from Git tag and set the parsed version to
# environment variable, REL_VERSION.

# We set the environment variable REL_CHANNEL based on the kind of build. This is used for
# versioning of our assets.
#
# REL_CHANNEL is:
# 'edge': for most builds
# 'edge': for PR builds
# '1.0.0-rc1' (the full version): for a tagged prerelease
# '1.0' (major.minor): for a tagged release

# We set the environment variable UPDATE_RELEASE if it's a full release (tagged and not prerelease)

# REL_VERSION is used to stamp versions into binaries
# REL_CHANNEL is used to upload assets to different paths

# This way a 1.0 user can download 1.0.1, etc.

import os
import re
import sys

gitRef = os.getenv("GITHUB_REF")

# From https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
# Group 'version' returns the whole version
# other named groups return the components
tagRefRegex = r"^refs/tags/v(?P<version>0|(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$"
pullRefRegex = r"^refs/pull/(.*)/(.*)$"

with open(os.getenv("GITHUB_ENV"), "a") as githubEnv:
if gitRef is None:
print("This is not running in github, GITHUB_REF is null. Assuming a local build...")

version = "REL_VERSION=edge"
print("Setting: {}".format(version))
githubEnv.write(version + "\n")

channel = "REL_CHANNEL=edge"
print("Setting: {}".format(channel))
githubEnv.write(channel + "\n")

tag = "REL_TAG=local"
print("Setting: {}".format(tag))
githubEnv.write(tag + "\n")

sys.exit(0)

match = re.search(pullRefRegex, gitRef)
if match is not None:
print("This is pull request {}...".format(match.group(1)))

version = "REL_VERSION=pr-{}".format(match.group(1))
print("Setting: {}".format(version))
githubEnv.write(version + "\n")

channel = "REL_CHANNEL=edge"
print("Setting: {}".format(channel))
githubEnv.write(channel + "\n")

tag = "REL_TAG=pr-{}".format(match.group(1))
print("Setting: {}".format(tag))
githubEnv.write(tag + "\n")

sys.exit(0)

match = re.search(tagRefRegex, gitRef)
if match is not None:
print("This is tagged as {}...".format(match.group("version")))

if match.group("prerelease") is None:
print("This is a full release...")

version = "REL_VERSION={}".format(match.group("version"))
print("Setting: {}".format(version))
githubEnv.write(version + "\n")

channel = "REL_CHANNEL={}.{}".format(match.group("major"), match.group("minor"))
print("Setting: {}".format(channel))
githubEnv.write(channel + "\n")

tag = "REL_TAG={}".format(match.group("version"))
print("Setting: {}".format(tag))
githubEnv.write(tag + "\n")

print("Setting: UPDATE_RELEASE=true")
githubEnv.write("UPDATE_RELEASE=true" + "\n")

sys.exit(0)

else:
print("This is a prerelease...")

version = "REL_VERSION={}".format(match.group("version"))
print("Setting: {}".format(version))
githubEnv.write(version + "\n")

channel = "REL_CHANNEL={}".format(match.group("version"))
print("Setting: {}".format(channel))
githubEnv.write(channel + "\n")

tag = "REL_TAG={}".format(match.group("version"))
print("Setting: {}".format(tag))
githubEnv.write(tag + "\n")

sys.exit(0)

print("This is a normal build")
version = "REL_VERSION=edge"
print("Setting: {}".format(version))
githubEnv.write(version + "\n")

channel = "REL_CHANNEL=edge"
print("Setting: {}".format(channel))
githubEnv.write(channel + "\n")

tag = "REL_TAG=latest"
print("Setting: {}".format(tag))
githubEnv.write(tag + "\n")

27 changes: 18 additions & 9 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
push:
branches:
- main
tags:
- v*
pull_request:
branches:
- main
Expand All @@ -14,15 +16,17 @@ permissions:
env:
CI_LINT: ${{ github.event_name == 'pull_request' }}
CI_TEST: ${{ github.event_name == 'pull_request' }}
CI_PUBLISH_RELASE: false ## TODO: enable me later.
CI_PUBLISH_PRERELEASE: ${{ github.repository == 'radius-project/dashboard' && github.ref == 'refs/heads/main' && github.event_name == 'push' }}
CI_PUBLISH_RELEASE: ${{ github.repository == 'radius-project/dashboard' && startsWith(github.ref, 'refs/tags/v') && github.event_name == 'push' }}
CI_PUBLISH_LATEST: ${{ github.repository == 'radius-project/dashboard' && github.ref == 'refs/heads/main' && github.event_name == 'push' }}
jobs:
build:
name: Build Packages
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Parse release version and set environment variables
run: python ./.github/scripts/get_release_version.py
- name: Enable corepack
run: corepack enable
- name: Install Node.js 21 # Must be after corepack is enabled.
Expand Down Expand Up @@ -77,13 +81,18 @@ jobs:
uses: ./.github/actions/analyze-image
with:
image: ghcr.io/radius-project/dashboard:latest
- name: Login (to ghcr.io on push to main)
if: ${{ env.CI_PUBLISH_PRERELEASE == 'true' || env.CI_PUBLISH_RELASE == 'true' }}
- name: Login to ghcr.io
if: ${{ env.CI_PUBLISH_LATEST == 'true' || env.CI_PUBLISH_RELEASE == 'true' }}
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}
- name: Push Image (to ghcr.io on push to main)
if: ${{ env.CI_PUBLISH_PRERELEASE == 'true' }}
run: docker push ghcr.io/radius-project/dashboard:latest
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push Image to ghcr.io (push to main)
if: ${{ env.CI_PUBLISH_LATEST == 'true' }}
run: |
docker push ghcr.io/radius-project/dashboard:latest
- name: Push Image to ghcr.io (push to tag)
if: ${{ env.CI_PUBLISH_RELEASE == 'true' }}
run: |
docker push ghcr.io/radius-project/dashboard:${{ env.REL_TAG }}