Skip to content

Commit a053b5e

Browse files
committed
Add files to publish oci artifact
Signed-off-by: Manuel Morejon <manuel@mmorejon.io>
1 parent 60dd81a commit a053b5e

7 files changed

Lines changed: 177 additions & 0 deletions

File tree

.github/workflows/oci-publish.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Publicar Artefacto OCI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
# tags: [ 'v*' ] # Descomentar si quieres que se ejecute también al crear tags
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
11+
jobs:
12+
publish-artifact:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
packages: write
17+
18+
steps:
19+
# 1. Download the repository code
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
# 2. Configure ORAS CLI (Using the official action)
24+
# https://github.com/marketplace/actions/setup-oras
25+
- name: Set up ORAS
26+
uses: oras-project/setup-oras@v1
27+
with:
28+
version: 1.2.4
29+
30+
# 3. Install JQ (Required to manipulate the JSON in the script)
31+
- name: Install JQ (JSON Processor)
32+
run: sudo apt-get update && sudo apt-get install -y jq
33+
34+
# 4. Login to GitHub Container Registry
35+
- name: Log into GHCR
36+
run: echo "${{ secrets.GITHUB_TOKEN }}" | oras login ${{ env.REGISTRY }} -u ${{ github.actor }} --password-stdin
37+
38+
# 5. Execute the publish script
39+
# The script will automatically read GITHUB_REPOSITORY, GITHUB_SHA, etc.
40+
- name: Execute Publish Script
41+
run: |
42+
chmod +x publish.sh
43+
./publish.sh

files/example-1.txt

173 Bytes
Binary file not shown.

files/example-2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ORAS was used to create the OCI artifact.

publish.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# --- CONFIGURATION ---
5+
# Detect the repository name (user/repo)
6+
FULL_REPO="${GITHUB_REPOSITORY:-mmorejon/erase-una-vez-5}"
7+
REPO_URL="https://github.com/${FULL_REPO}"
8+
IMAGE="ghcr.io/$FULL_REPO"
9+
TAG="${GITHUB_REF_NAME:-v1}"
10+
SRC_DIR="./files"
11+
# ---------------------
12+
13+
log_info() { echo -e "\033[1;34m[INFO]\033[0m $1"; }
14+
log_step() { echo -e "\033[1;33m[STEP $1]\033[0m $2"; }
15+
log_success() { echo -e "\033[1;32m[SUCCESS]\033[0m $1"; }
16+
17+
log_info "Initializing OCI artifact publication sequence."
18+
19+
# 0. Metadata Discovery
20+
log_step "0/5" "Gathering metadata from environment..."
21+
22+
# 1. Prepare the data layer
23+
log_step "1/5" "Packaging filesystem layer from: $SRC_DIR"
24+
tar -czf layer.tar.gz -C "$SRC_DIR" .
25+
26+
# Calculate the DiffID
27+
log_step "2/5" "Calculating data integrity checksums (DiffID)..."
28+
DIFF_ID=$(gzip -d -c layer.tar.gz | (sha256sum 2>/dev/null || shasum -a 256) | awk '{print $1}')
29+
30+
# Upload the layer
31+
log_step "3/5" "Uploading raw data blob..."
32+
LAYER_DESC=$(oras blob push "$IMAGE" layer.tar.gz --descriptor)
33+
34+
# --- BUILD FUNCTION ---
35+
build_arch() {
36+
local ARCH=$1
37+
echo " [INFO] Processing architecture: $ARCH" >&2
38+
39+
# A. Config Blob
40+
jq --arg arch "$ARCH" --arg diff "sha256:$DIFF_ID" \
41+
'.architecture = $arch | .rootfs.diff_ids[0] = $diff' \
42+
templates/config.json > "config-$ARCH.json"
43+
44+
local CFG_DESC=$(oras blob push "$IMAGE" "config-$ARCH.json" --descriptor)
45+
46+
# B. Manifest
47+
jq --argjson cfg "$CFG_DESC" \
48+
--argjson layer "$LAYER_DESC" \
49+
--arg src "$REPO_URL" \
50+
'.config.digest = $cfg.digest | .config.size = $cfg.size |
51+
.layers[0].digest = $layer.digest | .layers[0].size = $layer.size |
52+
.annotations["org.opencontainers.image.source"] = $src' \
53+
templates/manifest.json > "manifest-$ARCH.json"
54+
55+
oras manifest push "$IMAGE" "manifest-$ARCH.json" --descriptor
56+
}
57+
58+
# --- EXECUTION ---
59+
60+
log_step "4/5" "Building manifests..."
61+
AMD_DESC=$(build_arch "amd64")
62+
ARM_DESC=$(build_arch "arm64")
63+
64+
# Create Index
65+
log_step "5/5" "Constructing OCI Image Index..."
66+
jq --argjson amd "$AMD_DESC" \
67+
--argjson arm "$ARM_DESC" \
68+
--arg src "$REPO_URL" \
69+
'.manifests[0].digest = $amd.digest | .manifests[0].size = $amd.size |
70+
.manifests[1].digest = $arm.digest | .manifests[1].size = $arm.size |
71+
.annotations["org.opencontainers.image.source"] = $src' \
72+
templates/index.json > index_final.json
73+
74+
log_info "Publishing final tag: $IMAGE:$TAG"
75+
oras manifest push "$IMAGE:$TAG" index_final.json > /dev/null
76+
77+
# Cleanup
78+
rm layer.tar.gz config-*.json manifest-*.json index_final.json
79+
80+
echo ""
81+
log_success "Artifact published successfully."

templates/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"architecture": "__ARCH__",
3+
"os": "linux",
4+
"rootfs": {
5+
"type": "layers",
6+
"diff_ids": ["sha256:__DIFF_ID__"]
7+
}
8+
}

templates/index.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"schemaVersion": 2,
3+
"mediaType": "application/vnd.oci.image.index.v1+json",
4+
"manifests": [
5+
{
6+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
7+
"digest": "",
8+
"size": 0,
9+
"platform": {
10+
"architecture": "amd64",
11+
"os": "linux"
12+
}
13+
},
14+
{
15+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
16+
"digest": "",
17+
"size": 0,
18+
"platform": {
19+
"architecture": "arm64",
20+
"os": "linux"
21+
}
22+
}
23+
],
24+
"annotations": {
25+
"org.opencontainers.image.source": ""
26+
}
27+
}

templates/manifest.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"schemaVersion": 2,
3+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
4+
"config": {
5+
"mediaType": "application/vnd.oci.image.config.v1+json",
6+
"digest": "", "size": 0
7+
},
8+
"layers": [
9+
{
10+
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
11+
"digest": "", "size": 0
12+
}
13+
],
14+
"annotations": {
15+
"org.opencontainers.image.source": ""
16+
}
17+
}

0 commit comments

Comments
 (0)