-
Notifications
You must be signed in to change notification settings - Fork 361
Labeling Cloud Foundry API resources with a git SHA
Recently the Cloud Foundry API added the ability to add labels to a given resource. One interesting use case is to tag your app and related resources with a git commit SHA. This allows you to track which version of your code is running on Cloud Foundry.
When you push an app to Cloud Foundry, the files you push are stored in a resource called a package. Packages are an input into the staging process, along with buildpacks, the output of which is a droplet.
Tracking the version of your code through these various resources can be difficult, so it may help to label them.
The first step is to get the git commit SHA:
git rev-parse --short HEAD
Then, we need to find the GUIDs of the resource we want to label. Here, we'll label the app, package, and droplet.
To get the app's guid:
cf app appName --guid
The Cloud Foundry CLI does not yet support labels, so we'll have to access the Cloud Foundry API directly.
Luckily, the Cloud Foundry CLI does include a curl
command that takes care of authentication and targeting the appropriate API endpoint.
To get the guid of the current droplet associated with the app:
cf curl "/v3/apps/${APP_GUID}/droplets/current" | jq -r .guid
Droplet are built from packages, so to get the package guid associated with your droplet:
cf curl "/v3/droplets/${DROPLET_GUID}" | jq -r .links.package.href | xargs basename
To add a label to a resource, you must submit a PATCH
request. All of the bodies will be the same for each resource, so we can construct it:
jq -nc --arg commit "${COMMIT_SHA}" '{"metadata": { "labels": { "commit": $commit } } }'
Then, PATCH
against, say, the app:
cf curl /v3/apps/${APP_GUID} -X PATCH -d "${REQUEST_BODY}"
Once you have labeled the resources, you can then query the Cloud Foundry API using the label_selector
query parameter.
Here is an example of finding packages with the commit SHA:
cf curl "/v3/apps?label_selector=commit=${COMMIT_SHA}"
This should return all packages that have been tagged with our commit SHA. You can swap out apps
for packages
and droplets
to see those tagged resources.
Here is a script that, when run from the repository of your app, will tag the app, droplet, and package with the commit SHA:
#!/usr/bin/env bash
# Run this from your app's git repo
set -ex
APP_GUID="$(cf app appName --guid)"
APP_URI="/v3/apps/${APP_GUID}}"
DROPLET_GUID="$(cf curl "/v3/apps/${APP_GUID}/droplets/current" | jq -r .guid)"
DROPLET_URI="/v3/droplets/${DROPLET_GUID}"
PACKAGE_GUID="$(cf curl "/v3/droplets/${DROPLET_GUID}" | jq -r .links.package.href | xargs basename)"
PACKAGE_URI="/v3/packages/${PACKAGE_GUID}"
# Get a short version of your git SHA
COMMIT_SHA="$(git rev-parse --short HEAD)"
REQUEST_BODY="$(jq -nc --arg commit "${COMMIT_SHA}" '{"metadata": { "labels": { "commit": $commit } } }')"
# Label the app, package, and droplet with the code's current commit SHA.
cf curl "${APP_URI}" -X PATCH -d "${REQUEST_BODY}"
cf curl "${PACKAGE_URI}" -X PATCH -d "${REQUEST_BODY}"
cf curl "${DROPLET_URI}" -X PATCH -d "${REQUEST_BODY}"
-
Pipelines
-
Contributing
- Tips and Tricks
- Cloud Controller API v3 Style Guide
- Playbooks
- Development configuration
- Testing
-
Architectural Details
-
CC Resources
- Apps
- Audit Events
- Deployments
- Labels
- Services
- Sidecars
-
Dependencies
-
Troubleshooting
- Ruby Console Script to Find Fields that Cannot Be Decrypted
- Logging database queries in unit tests
- Inspecting blobstore cc resources and cc packages(webdav)
- How to Use USR1 Trap for Diagnostics
- How to Perf: Finding and Fixing Bottlenecks
- How to get access to mysql database
- How To Get a Ruby Heap Dumps & GC Stats from CC
- How to curl v4 internal endpoints with mtls
- How to access Bosh Director console and restore an outdated Cloud Config
- Analyzing Cloud Controller's NGINX logs using the toplogs script
-
k8s
-
Archive