Skip to content

Labeling Cloud Foundry API resources with a git SHA

Andy Brown edited this page Mar 6, 2019 · 7 revisions

Introduction

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.

Prerequisites

Steps

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}"
Clone this wiki locally