GCP for DevOps — Versioning & Lifecycle

2 minute read

Versioning & Lifecycle

Object versioning keeps a history of every change to an object. Lifecycle management automates moving or deleting objects based on age, version count, or storage class.

Object Versioning

# Enable versioning on a bucket
$ gcloud storage buckets update gs://my-devops-artifacts \
    --versioning

# Upload a file, then overwrite it
$ echo "v1" > config.yaml
$ gcloud storage cp config.yaml gs://my-devops-artifacts/config.yaml
$ echo "v2" > config.yaml
$ gcloud storage cp config.yaml gs://my-devops-artifacts/config.yaml

# List all versions (including non-current)
$ gcloud storage ls -a gs://my-devops-artifacts/config.yaml

gs://my-devops-artifacts/config.yaml#1711800000000000  (current)
gs://my-devops-artifacts/config.yaml#1711799000000000  (non-current)

# Download a specific version
$ gcloud storage cp gs://my-devops-artifacts/config.yaml#1711799000000000 ./config-v1.yaml

# Restore a previous version (copy old version to current)
$ gcloud storage cp gs://my-devops-artifacts/config.yaml#1711799000000000 \
    gs://my-devops-artifacts/config.yaml

Lifecycle Management

Lifecycle rules automate storage class transitions and object deletion.

# Set lifecycle rules using a JSON file
$ cat lifecycle.json
{
  "lifecycle": {
    "rule": [
      {
        "action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
        "condition": {"age": 30, "matchesStorageClass": ["STANDARD"]}
      },
      {
        "action": {"type": "SetStorageClass", "storageClass": "COLDLINE"},
        "condition": {"age": 90, "matchesStorageClass": ["NEARLINE"]}
      },
      {
        "action": {"type": "SetStorageClass", "storageClass": "ARCHIVE"},
        "condition": {"age": 365, "matchesStorageClass": ["COLDLINE"]}
      },
      {
        "action": {"type": "Delete"},
        "condition": {"age": 730}
      },
      {
        "action": {"type": "Delete"},
        "condition": {"isLive": false, "numNewerVersions": 3}
      }
    ]
  }
}
# Apply lifecycle rules
$ gcloud storage buckets update gs://my-devops-artifacts \
    --lifecycle-file=lifecycle.json

# View current lifecycle rules
$ gcloud storage buckets describe gs://my-devops-artifacts \
    --format="json(lifecycle)"

Common Lifecycle Patterns

Build Artifacts — Keep 30 Days, Delete

{
  "lifecycle": {
    "rule": [
      {
        "action": {"type": "Delete"},
        "condition": {"age": 30}
      }
    ]
  }
}

Terraform State — Version Control

{
  "lifecycle": {
    "rule": [
      {
        "action": {"type": "Delete"},
        "condition": {"isLive": false, "numNewerVersions": 10}
      }
    ]
  }
}

Logs — Tier Down Then Delete

{
  "lifecycle": {
    "rule": [
      {
        "action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
        "condition": {"age": 30, "matchesStorageClass": ["STANDARD"]}
      },
      {
        "action": {"type": "SetStorageClass", "storageClass": "COLDLINE"},
        "condition": {"age": 90, "matchesStorageClass": ["NEARLINE"]}
      },
      {
        "action": {"type": "Delete"},
        "condition": {"age": 365}
      }
    ]
  }
}

Retention Policies

Prevent objects from being deleted or overwritten for a specified period (compliance).

# Set a retention period of 90 days
$ gcloud storage buckets update gs://my-audit-logs \
    --retention-period=90d

# Lock the retention policy (IRREVERSIBLE)
$ gcloud storage buckets update gs://my-audit-logs \
    --lock-retention-period

Common Mistakes

Mistake Why It’s Wrong Fix
No versioning on state buckets Can’t recover from bad state Enable versioning on Terraform state buckets
No lifecycle rules Old objects accumulate cost Always set cleanup rules
Keeping all versions forever Storage costs grow indefinitely Limit with numNewerVersions
Locking retention without testing Irreversible — can’t delete objects Test with unlocked retention first
Manual cleanup Error-prone and inconsistent Use lifecycle rules

Quick Reference

Command Description
gcloud storage buckets update gs://B --versioning Enable versioning
gcloud storage ls -a gs://B/OBJ List all versions
gcloud storage buckets update gs://B --lifecycle-file=F Set lifecycle rules
gcloud storage buckets describe gs://B --format=json(lifecycle) View lifecycle
gcloud storage buckets update gs://B --retention-period=Nd Set retention

Next → Storage for DevOps

Updated: