Skip to content

How to Create an App Using V3 of the CC API

braa braa braa edited this page Sep 13, 2019 · 42 revisions
  1. Find the GUID of your space and set an APP_NAME:

    CF_API_ENDPOINT=$(cf api | grep -i "api endpoint" | awk '{print $3}')
    SPACE_GUID=$(cf space `cf target | tail -n 1 | awk '{print $2}'` --guid)
    APP_NAME=[your-app-name]
    
  2. Create an empty app (docs):

    Here is the most common way:

    APP_GUID=$(cf curl /v3/apps -X POST -d "$(printf '{"name": "%s", "relationships": {"space": {"data": {"guid": "%s"}}}}' "$APP_NAME" "$SPACE_GUID")" | tee /dev/tty | jq -r .guid)

    To also provide environment variables (especially if you're building a go app):

    APP_GUID=$(cf curl /v3/apps -X POST -d "$(printf '{"name": "%s", "relationships": {"space": {"data": {"guid": "%s"}}}, "environment_variables": { "GOPACKAGENAME": "go-online"} }' "$APP_NAME" "$SPACE_GUID")" | tee /dev/tty | jq -r .guid)

  3. Create an empty package for the app (docs):

    PACKAGE_GUID=$(cf curl /v3/packages -X POST -d "$(printf '{"type":"bits", "relationships": {"app": {"data": {"guid": "%s"}}}}' "$APP_GUID")" | tee /dev/tty | jq -r .guid)

    Note: Other package types are also supported. See documentation for Create a Package.

  4. If your package is type buildpack, create a ZIP file of your application:

    zip -r my-app.zip *

    Note: The zip file should not have a folder as the top-level item (e.g. create the zip file from within your app’s directory)

  5. If your package is type buildpack, upload your bits to your new package (docs):

    curl -k "$CF_API_ENDPOINT/v3/packages/$PACKAGE_GUID/upload" -F bits=@"my-app.zip" -H "Authorization: $(cf oauth-token | grep bearer)"

  6. Stage your package and create a build (docs):

    BUILD_GUID=$(cf curl /v3/builds -X POST -d "$(printf '{ "package": { "guid": "%s" }}' "$PACKAGE_GUID")" | tee /dev/tty | jq -r .guid)

    Note: The output of this command includes your new build's GUID

    To specify specific buildpacks to use in your app, use a command like this:

    BUILD_GUID=$(cf curl /v3/builds -X POST -d "$(printf '{ "package": { "guid": "%s" }, "lifecycle": { "type": "buildpack", "data": { "buildpacks": ["ruby_buildpack", "go_buildpack"] } } }' "$PACKAGE_GUID")" | tee /dev/tty | jq -r .guid)

    Alternative: to push the go URL buildpack instead of the admin buildpack:

    BUILD_GUID=$(cf curl /v3/builds -X POST -d "$(printf '{ "package": { "guid": "%s" }, "lifecycle": { "type": "buildpack", "data": { "buildpacks": ["ruby_buildpack", "https://github.com/cloudfoundry/go-buildpack.git"] } } }' "$PACKAGE_GUID")" | tee /dev/tty | jq -r .guid)

  7. Wait for the state of the new build to reach STAGED:

    watch cf curl /v3/builds/$BUILD_GUID

    If staging fails and you're trying to push a go app, go back to the part where you're POSTing the app and see the part on environment variables.

  8. Get the droplet corresponding to the staged build:

    DROPLET_GUID=$(cf curl /v3/builds/$BUILD_GUID | jq -r '.droplet.guid')

  9. Assign the droplet to the app:

    cf curl /v3/apps/$APP_GUID/relationships/current_droplet -X PATCH -d "$(printf '{"data": {"guid": "%s"}}' "$DROPLET_GUID")"

  10. Create a route:

    CF_TRACE=true cf create-route [space-name] [domain-name] -n [host-name]
    ROUTE_GUID=[copy route's GUID under metadata->guid of the last response]
    
  11. Map the route to your app (docs):

    DESTINATION=$(printf '{"destinations" : [ {"app": {"guid": "%s"}  }  ]  } ' "$APP_GUID")
    

cf curl /v3/routes/$ROUTE_GUID/destinations -X POST -d "$DESTINATION" ```

  1. Start your app (docs):

    cf curl /v3/apps/$APP_GUID/actions/start -X POST

  2. Visit your app at the new route to confirm that it is up. You might need to wait a minute or two for it to be running after the previous start command.

Clone this wiki locally