Skip to content

Commit

Permalink
Merge pull request #1 from dci-labs/dci_create_component
Browse files Browse the repository at this point in the history
Dci create component
  • Loading branch information
tonyskapunk authored Sep 26, 2022
2 parents 43338e6 + bc4c2d4 commit 4d97432
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 111 deletions.
91 changes: 72 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,104 @@ Some Inputs are required, while others are optional.

- `dciClientId`: Remote CI client ID, this is passed as a secret.
- `dciApiSecret`: Remote CI API secret, this is passed as a secret.
- `dciTopicVersion`: DCI topic version, e.g. `4.10` or `4.11,4.10,4.9` or `all`.
- `componentName`: DCI component name, this is usually the **version** of your component, e.g. `v1.2.3` or `22.04` or `0.1-alpha`.
- `componentCanonicalName`: DCI component canonical name, this is usually the **name** of your component, e.g. `my operator`, `my-app`.
- `componentType`: DCI component type, the type of component to create, e.g. `operator`, `cnf-app`, `git-repo`, `rpm`, `container-image`
- `dciTopics`: A comma separated list of DCI topics example:

```yaml
# Single topic
dciTopics: OCP-4.12

# Multiple topics
dciTopics: 'OCP-4.11,OCP-4.12'

# Multiple topics, multi-line
dciTopics: '
OCP-4.10,
OCP-4.11,
OCP-4.12,
'

# Other topics
dciTopics: RHEL-9.2
```
- `componentName`: DCI component name, e.g. `My Awesome Component` or `FredCo awesome operator` or `acme-component`, etc.
- `componentVersion`: DCI component version, the version of the component to create, e.g. `v1.2.3`, `22.10`, `9.2-rc1`, `v12.28.12-alpha`, etc.
- `componentRelease`: DCI component release tag, must be one of the following: `dev`, `candidate` or `ga`.

**Optional**:

- `dciCsUrl`: Remote CI control server URL, (default: `https://api.distributed-ci.io/`)
- `dciTopic`: DCI topic, the only supported so far is `OCP` (default: `OCP`)
- `dciCsUrl`: Remote CI control server URL, default is `https://api.distributed-ci.io/`.
- `componentTags`: List of DCI component Tags, for example:

```yaml
# Single Tag
componentTags: "arch:arm64"
# Multiple Tags
dciTags: "os:linux,arch:amd64"
```

- `componentUrl`: DCI component URL, requires an http(s) schema, e.g. `https://my-site.com`, `https://github.com/my-org/my-repo`
- `componentData`: DCI component data, a compact (one-liner) json entry, e.g. `{"foo": "bar"}`, `{"uno":null,"dos":["tres",{"sub":{"on":true,"off":false}}]}`

### Workflow

Here few examples on how to use this Action in your workflow as a step

Creating a component on the OCP topic under its version `4.10` with name `v1.2.3` and its canonical name `My container application` of type `image`.
Creating component `My container application` with version `v1.2.3-alpha`, and `dev` release. The component will be created only in `OCP-4.12`.

```YAML
steps:
- name: Create DCI components
uses: tonyskapunk/dci-component@v0.1.0
uses: dci-labs/dci-component@v1.0.0
with:
dciClientId: ${{ secrets.DCI_CLIENT_ID }}
dciApiSecret: ${{ secrets.DCI_API_SECRET }}
dciTopicVersion: "4.10"
componentName: v1.2.3
componentCanonicalName: "My container application"
componentType: image

dciTopics:
- "OCP-4.12"
componentName: "My container application"
componentVersion: v1.2.3
componentRelease: "dev"
```

Creating a component on `all` the OCP topic versions with name `22.04` and its canonical name `my-operator` of type `operator`, with a reference to its repository `https://github.com/myorg/my-operator` and another reference to the image under: `{"imageURL": "quay.io/myorg/my-operator:22.04"}`
Creating a component on multiple OCP topic versions with name `my-operator` and version `22.04`, with a reference to its repository `https://github.com/myorg/my-operator`, with some custom tags, and the location of the image: `{"imageURL": "quay.io/myorg/my-operator:22.04"}`

```YAML
steps:
- name: Create DCI components
uses: tonyskapunk/dci-component@v0.2.0
uses: dci-labs/dci-component@v1.0.0
with:
dciClientId: ${{ secrets.DCI_CLIENT_ID }}
dciApiSecret: ${{ secrets.DCI_API_SECRET }}
dciTopicVersion: all
componentName: 22.04
componentCanonicalName: my-operator
componentType: operator
dciTopics: '
OCP-4.8,
OCP-4.9,
OCP-4.10,
OCP-4.11
'
componentName: my-operator
componentVersion: "22.04"
componentRelease: ga
componentUrl: https://github.com/myorg/my-operator
componentTags: "ansible-operator,os:linux,2022"
componentData: {"imageURL": "quay.io/myorg/my-operator:22.04"}
```

The output of the component created is stored in `components` and can be re-used like this:

```YAML
steps:
- name: Create DCI components
uses: dci-labs/dci-component@v1.0.0
with:
dciClientId: ${{ secrets.DCI_CLIENT_ID }}
dciApiSecret: ${{ secrets.DCI_API_SECRET }}
dciTopics:
- "OCP-4.12"
componentName: "My container application"
componentVersion: v1.2.3
componentRelease: "dev"
id: my_cmp
- run: jq . <<<"${{ steps.my_cmp.outputs.components }}"
```
19 changes: 9 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ inputs:
required: true
default: "https://api.distributed-ci.io/"
# Component details (Required)
dciTopicVersion:
description: "DCI topic version"
dciTopics:
description: "Comma separated list of DCI topics"
required: true
componentName:
description: "DCI component name"
required: true
componentCanonicalName:
description: "DCI component canonical name"
required: true
componentType:
description: "DCI component type"
componentVersion:
description: "DCI component version"
required: true
# Component details (Optional)
dciTopic:
description: "DCI topic"
componentRelease:
description: "DCI component release"
required: true
default: OCP
componentTags:
description: "Comma separated list of DCI component Tags"
required: false
componentUrl:
description: "DCI component URL"
required: false
Expand Down
108 changes: 26 additions & 82 deletions add-component
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ function clean_up() {

trap clean_up EXIT

VALID_DCI_TOPICS=(
OCP
VALID_DCI_RELEASE_TAGS=(
dev
candidate
ga
)

# API Secrets
Expand All @@ -21,73 +23,28 @@ export DCI_API_SECRET
export DCI_CS_URL

# Required Inputs
TOPIC=${INPUT_DCITOPIC^^}
TOPIC_VERSION=${INPUT_DCITOPICVERSION}
TOPICS=(${INPUT_DCITOPICS//,/ })
COMPONENT_NAME=${INPUT_COMPONENTNAME}
COMPONENT_CANONICAL_NAME=${INPUT_COMPONENTCANONICALNAME}
COMPONENT_TYPE=${INPUT_COMPONENTTYPE}
COMPONENT_VERSION=${INPUT_COMPONENTVERSION}
COMPONENT_RELEASE=${INPUT_COMPONENTRELEASE}

# Optional Inputs
COMPONENT_TAGS=${INPUT_COMPONENTTAGS}
COMPONENT_URL=${INPUT_COMPONENTURL}
COMPONENT_DATA=${INPUT_COMPONENTDATA}

# dciTopic
if ! grep -q "${TOPIC}" <<< "${VALID_DCI_TOPICS[@]}"; then
echo "Invalid Topic: ${TOPIC}"
echo "Valid Topics: ${VALID_DCI_TOPICS[@]}"
# componentRelease
if ! grep -q "${COMPONENT_RELEASE,,}" <<< "${VALID_DCI_RELEASE_TAGS[@]}"; then
echo "Invalid Release: ${COMPONENT_RELEASE}"
echo "Valid Releases: ${VALID_DCI_RELEASE_TAGS[@]}"
exit 1
fi

case ${TOPIC} in
"OCP")
product="OpenShift"
;;
esac

# Get Team ID
team_id=$( dcictl \
--format json \
team-list |
jq -r '.teams[]|.id'
)
if [[ -z "${team_id}" ]]; then
echo "Failed to get Team ID, please review your DCI Client ID and API Secret"
exit 1
# componentTags
if [[ -n ${COMPONENT_TAGS} ]]; then
tags="--tags ${COMPONENT_TAGS}"
fi

# Get Product ID
product_id=$( dcictl \
--format json \
product-list \
--where "name:${product}" |
jq -r '.products[].id'
)

# dciTopicVersion
all_versions=( $( dcictl \
--format json \
topic-list \
--where "status:active" \
--where "product_id:${product_id}" \
--where "name:OCP*" |
jq -r '.topics[] | .name | sub("'${TOPIC}'-?";"")')
)
case $TOPIC_VERSION in
"all")
versions=${all_versions[@]}
;;
*)
versions=( ${TOPIC_VERSION//,/ } )
for version in ${versions[@]}; do
if ! grep -q "${version}" <<< "${all_versions[@]}"; then
echo "Invalid Topic Version: ${version}"
echo "Valid Topic Versions: ${all_versions[@]} or 'all'"
exit 1
fi
done
;;
esac

# componentURL
if [[ -n ${COMPONENT_URL} ]]; then
if ! grep -iqP "^https?://" <<< "${COMPONENT_URL}"; then
Expand All @@ -107,34 +64,21 @@ if [[ -n ${COMPONENT_DATA} ]]; then
data="--data $(jq -c . <<<${COMPONENT_DATA})"
fi


for version in ${versions[@]}; do
topic_id=$( dcictl \
--format json \
topic-list \
--where "state:active" \
--where "name:${TOPIC}-${version}" |
jq -r '.topics[] | .id'
)
if [[ -z ${topic_id} ]]; then
echo "No Topic ID was found for ${TOPIC}-${version}"
exit 1
fi
output=$( dcictl \
--format json\
component-create \
--topic-id ${topic_id} \
--team-id ${team_id} \
--name ${COMPONENT_NAME} \
--canonical_project_name "${COMPONENT_CANONICAL_NAME}" \
--type ${COMPONENT_TYPE} \
${url} ${data}
for topic in ${TOPICS[@]}; do
output=$( \
dci-create-component \
--format json \
${topic} \
"${COMPONENT_NAME}" \
${COMPONENT_VERSION} \
${COMPONENT_RELEASE} \
${tags} ${url} ${data}
)
if [[ $(jq .status_code <<< "$output") -ne "null" ]]; then
echo "Failed to create component for ${TOPIC}-${version}"
echo "Failed to create component for ${COMPONENT_NAME}-${COMPONENT_VERSION}"
exit 1
fi
jq . <<< "$output" >> $components_file
jq . <<< "$output" >> ${components_file}
done

echo "::set-output name=components::$(jq -s -c '.' ${components_file})"

0 comments on commit 4d97432

Please sign in to comment.