docs: https://docs.harness.io/article/tm0w6rruqv-harness-api
fn_run_query () {
#
# env config
# - HARNESS_API_KEY
#. - HARNESS_ACCOUNT_ID
#
curl -s \
-H 'x-api-key: '$HARNESS_API_KEY \
-X POST \
-H 'Content-Type: application/json' \
--data @- \
'https://app.harness.io/gateway/api/graphql?accountId='$HARNESS_ACCOUNT_ID
}
get git connectors
cat <<_EOF_ | fn_run_query
{"query":"
query GetGits {
connectors(limit: 5, filters: [{connectorType: {operator: EQUALS, values: [GIT]}}]) {
nodes {
id
name
}
}
}
"}
_EOF_
be sure to escape all $
characters within the query
env:
- APP_NAME
- APP_DESC
export APP_NAME="myapp"
export APP_DESC="my first app"
cat <<_EOF_ | fn_run_query
{"query":"
mutation(\$app: CreateApplicationInput!) {
createApplication(input: \$app) {
clientMutationId
application {
name
id
}
}
}",
"variables":{
"app": {
"name": "$APP_NAME",
"description": "$APP_DESC"
}
}
}
_EOF_
useful for resources that are not supported in the graphql api
docs: https://docs.harness.io/article/bbmhnoqrzw-config-as-code-using-rest-apis
with this you can create resources using their config-as-code yaml specification
fn_put_file () {
#
# env config
# - HARNESS_API_KEY
#. - HARNESS_ACCOUNT_ID
#
# usage: fn_put_file "harness/file/path" "path/to/loca/file"
#
FILE_PATH=$1
FILE=$2
curl -X POST \
"https://app.harness.io/gateway/api/setup-as-code/yaml/upsert-entity?accountId=$HARNESS_ACCOUNT_ID&yamlFilePath=$FILE_PATH" \
-H 'accept: application/json, text/plain, */*' \
-H "x-api-key: $HARNESS_API_KEY" \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
--form "yamlContent=@\"$FILE\""
}
echo "harnessApiVersion: '1.0'
type: DOCKER
password: gcpkms:dockerhub
skipValidation: false
url: https://index.docker.io/v2/
username: rileysnyderharnessio" > dockerhubapi.yaml
fn_put_file "Setup/Artifact%20Servers/dockerhubapi.yaml" "$(pwd)/dockerhubapi.yaml"
echo "harnessApiVersion: '1.0'
type: ENVIRONMENT
configMapYamlByServiceTemplateName: {}
environmentType: NON_PROD" > dev.yaml
export APP_NAME=singular
export ENVIRONMENT=dev
fn_put_file "Setup/Applications/$APP_NAME/Environments/$ENVIRONMENT/Index.yaml" "$(pwd)/dev.yaml"