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

Update build workflow bicep #45

Merged
merged 16 commits into from
Jul 2, 2024
149 changes: 149 additions & 0 deletions .github/scripts/get_release_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# ------------------------------------------------------------
# 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)

# We set the environment variable CHART_VERSION based on the kind of build. This is used for
# versioning our helm chart ONLY.
#
# CHART_VERSION is:
#
# '0.42.42-dev' for most builds
# '0.42.42-pr-<pull request #>' for PR builds
# '1.0.0-rc1' (the full version): for a tagged prerelease
# '1.0.0' (major.minor.patch): for a tagged release
#
# note: we always install the helm chart using the tilde-range syntax to match our behavior
# based on channels. For example '--version ~0.9.0' will install the latest 0.9.X build
# which matches how we do other versioning/installation operations.

# REL_VERSION is used to stamp versions into binaries
# REL_CHANNEL is used to upload assets to different paths
# CHART_VERSION is used to set the version of the helm chart

# 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:
sk593 marked this conversation as resolved.
Show resolved Hide resolved
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")

chart = "CHART_VERSION=0.42.42-dev"
print("Setting: {}".format(chart))
githubEnv.write(chart + "\n")

channel = "REL_CHANNEL=edge"
print("Setting: {}".format(channel))
githubEnv.write(channel + "\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")

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

channel = "REL_CHANNEL=edge"
print("Setting: {}".format(channel))
githubEnv.write(channel + "\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")

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

channel = "REL_CHANNEL={}.{}".format(match.group("major"), match.group("minor"))
print("Setting: {}".format(channel))
githubEnv.write(channel + "\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")

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

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

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

chart = "CHART_VERSION=0.42.42-dev"
print("Setting: {}".format(chart))
githubEnv.write(chart + "\n")

channel = "REL_CHANNEL=edge"
print("Setting: {}".format(channel))
githubEnv.write(channel + "\n")
7 changes: 6 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: 'Setup Node'
uses: actions/setup-node@v3
with:
node-version: 14.x
node-version: 18.x
- name: 'Checkout'
uses: actions/checkout@v1
- name: 'Build aws-type-downloader'
Expand All @@ -29,6 +29,11 @@ jobs:
with:
version: latest
working-directory: 'src/aws-type-downloader'
- run: git submodule update --init --recursive
- run: npm ci
working-directory: 'bicep-types/src/bicep-types'
- run: npm run build
working-directory: 'bicep-types/src/bicep-types'
- run: npm ci
working-directory: 'src/aws-type-generator'
- run: npm run build
Expand Down
79 changes: 72 additions & 7 deletions .github/workflows/publish-bicep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,79 @@

name: Update extensibility provider types
on:
push:
branches: [ main ]
pull_request:
paths:
- "artifacts/bicep/**"
branches: [ main ]
workflow_dispatch:
inputs: {}
env:
# bicep-types ACR url for uploading AWS Bicep types
BICEP_TYPES_REGISTRY: 'biceptypes.azurecr.io'
AWS_REGION: us-west-2

jobs:
build-and-push-bicep-types:
name: Publish Radius bicep types to ACR
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Parse release version and set environment variables
run: python ./.github/scripts/get_release_version.py
- name: Set up Go ${{ env.GOVER }}
uses: actions/setup-go@v5
with:
go-version: ${{ env.GOVER }}
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: 'Build aws-type-downloader'
env:
GOPROXY: "https://proxy.golang.org"
working-directory: 'src/aws-type-downloader'
run: go build .
- name: Download AWS specs from CloudControl
run: |
cd src/aws-type-downloader && go run main.go --output ../../artifacts/types --clean
- name: 'Initialize submodule'
run: |
git submodule update --init --recursive
npm --prefix bicep-types/src/bicep-types ci && npm --prefix bicep-types/src/bicep-types run build; \
- name: Generate Bicep extensibility types for AWS
run: |
npm --prefix ./src/aws-type-generator install
npm run --prefix ./src/aws-type-generator start -- --input ../../artifacts/types --output ../../artifacts/bicep --release-version ${{ env.REL_VERSION == 'edge' && 'latest' || env.REL_VERSION }}
- name: Upload AWS Bicep types artifacts
uses: actions/upload-artifact@v4
with:
name: aws-bicep-types
path: ./artifacts/bicep
if-no-files-found: error
- name: 'Login via Azure CLI'
if: github.repository == 'radius-project/bicep-types-aws' && ((startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'))
uses: azure/login@v1
with:
creds: ${{ secrets.BICEP_TYPES_AZURE_CREDENTIALS }}
- name: Setup and verify bicep CLI
if: github.repository == 'radius-project/bicep-types-aws' && ((startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'))
run: |
curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
chmod +x ./bicep
bicep --version
- name: Publish bicep types
if: github.repository == 'radius-project/bicep-types-aws' && ((startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'))
run: |
bicep publish-provider ./artifacts/bicep/index.json --target br:${{ env.BICEP_TYPES_REGISTRY }}/aws:${{ env.REL_VERSION == 'edge' && 'latest' || env.REL_VERSION }} --force
update-types:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not part of this PR but i'm seeing that 'set-output' on line 103, 104 is deprecated.
https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
maybe we can update it in this or another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, this will eventually be deleted but I updated it for now so the workflow runs fine

if: github.event_name == 'pull_request' && github.base_ref == 'main'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated so this only runs on pulls from main. We don't want this running on pushes

name: Update Bicep extensibility provider types
runs-on: ubuntu-latest
timeout-minutes: 10
Expand All @@ -36,9 +101,9 @@ jobs:
- id: get_commit_hash
name: Get commit hash (radius-project/bicep-types-aws)
run: |
echo "::set-output name=commit_hash_short::$(git rev-parse HEAD --short)"
echo "::set-output name=gh_uri::https://github.com/radius-project/bicep-types-aws/tree/$(git rev-parse HEAD)/artifacts/bicep"

echo "commit_hash_short=$(git rev-parse HEAD --short)" >> $GITHUB_OUTPUT
echo "gh_uri=https://github.com/radius-project/bicep-types-aws/tree/$(git rev-parse HEAD)/artifacts/bicep" >> $GITHUB_OUTPUT
- name: Clone https://github.com/radius-project/bicep
uses: actions/checkout@v3
with:
Expand All @@ -53,7 +118,7 @@ jobs:
cp -R artifacts/bicep/* workflow-temp/bicep/src/Bicep.Types.Aws/generated

- name: Create Pull Request in radius-project/bicep repo
uses: peter-evans/create-pull-request@v4
uses: peter-evans/create-pull-request@v6
with:
path: workflow-temp/bicep
token: ${{ secrets.GH_RAD_CI_BOT_PAT }}
Expand All @@ -73,4 +138,4 @@ jobs:
autogenerated
github_actions
aws-types
draft: false
draft: false
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Once you have the prerequisites installed, you can build the repository with bel

1. Clone this repository
2. [Configure the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
3. Run `go run ./src/aws-type-downloader/main.go --output ./artifacts/types --clean`
4. Run `npm run --prefix ./src/aws-type-generator start -- --input ../../artifacts/types --output ../../artifacts/bicep`
3. Run `go run main.go --output ../../artifacts/types --clean` in the `src/aws-type-downloader` folder
4. Run `npm run --prefix ./src/aws-type-generator start -- --input ../../artifacts/types --output ../../artifacts/bicep` in the root folder

Note: `npm run --prefix` does not preserve the current working directory, so the extra `../..` is needed.
2 changes: 1 addition & 1 deletion src/aws-type-generator/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async function buildTypeIndex(logger: ILogger, baseDir: string, version: string)
types: readTypesJson(content),
});
}
const indexContent = await buildIndex(typeFiles, log => logOut(logger, log), {name: "AWS", version: (version == "edge" ? "latest" : version), isSingleton: false} as TypeSettings);
const indexContent = await buildIndex(typeFiles, log => logOut(logger, log), {name: "AWS", version: version, isSingleton: false} as TypeSettings);

await writeFile(`${baseDir}/index.json`, writeIndexJson(indexContent));
await writeFile(`${baseDir}/index.md`, writeIndexMarkdown(indexContent));
Expand Down