Skip to content

Commit 12381d1

Browse files
committed
nat64_appliance image workflow
Add a workflow to build the nat64_appliance on a weekely schedule and create a uniq release and update the "latest" tag. Depends-On: openstack-k8s-operators/ci-framework#3550 Assisted-By: Claude Code/claude-4.5-sonnet Signed-off-by: Harald Jensås <[email protected]>
1 parent 77556a4 commit 12381d1

File tree

1 file changed

+261
-0
lines changed

1 file changed

+261
-0
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
name: Build NAT64 Appliance Image
2+
3+
on:
4+
# Allows you to run this workflow manually from the Actions tab
5+
workflow_dispatch:
6+
inputs:
7+
release_tag:
8+
description: 'Release tag (e.g., v1.0.0)'
9+
required: true
10+
ci_framework_repo:
11+
description: 'ci-framework repository (for testing forks)'
12+
required: false
13+
default: 'https://github.com/openstack-k8s-operators/ci-framework.git'
14+
ci_framework_ref:
15+
description: 'ci-framework branch/tag/PR (e.g., main, my-branch, refs/pull/123/head)'
16+
required: false
17+
default: 'main'
18+
19+
# Run weekly on Mondays at 00:00 UTC
20+
schedule:
21+
- cron: '0 0 * * 1'
22+
23+
jobs:
24+
build-image:
25+
runs-on: ubuntu-22.04
26+
27+
# Permission needed to create a release and upload assets
28+
permissions:
29+
contents: write
30+
31+
env:
32+
WORK_DIR: ${{ github.workspace }}/ci-framework-data
33+
34+
steps:
35+
- name: 1. Checkout Repository
36+
uses: actions/checkout@v4
37+
38+
- name: 2. Clone ci-framework Repository
39+
run: |
40+
CI_REPO="${{ inputs.ci_framework_repo || 'https://github.com/openstack-k8s-operators/ci-framework.git' }}"
41+
CI_REF="${{ inputs.ci_framework_ref || 'main' }}"
42+
43+
echo "Cloning ci-framework..."
44+
echo " Repository: $CI_REPO"
45+
echo " Reference: $CI_REF"
46+
47+
# Check if this is a special ref (like refs/pull/123/head)
48+
if [[ "$CI_REF" == refs/* ]]; then
49+
echo "Detected special ref, using fetch method..."
50+
git clone "$CI_REPO" ${{ github.workspace }}/ci-framework
51+
cd ${{ github.workspace }}/ci-framework
52+
git fetch origin "$CI_REF"
53+
git checkout FETCH_HEAD
54+
else
55+
echo "Detected branch/tag, using direct clone with shallow history..."
56+
git clone --depth 1 --branch "$CI_REF" "$CI_REPO" \
57+
${{ github.workspace }}/ci-framework
58+
fi
59+
60+
echo "ci-framework cloned successfully"
61+
cd ${{ github.workspace }}/ci-framework
62+
echo "Current commit: $(git rev-parse HEAD)"
63+
echo "Current branch/ref: $(git describe --all)"
64+
65+
- name: 3. Install System Dependencies
66+
run: |
67+
echo "Installing system dependencies for diskimage-builder..."
68+
sudo apt-get update
69+
sudo apt-get install -y \
70+
python3-pip \
71+
python3-venv \
72+
qemu-utils \
73+
dosfstools \
74+
xfsprogs \
75+
kpartx \
76+
debootstrap \
77+
gdisk \
78+
git
79+
echo "System dependencies installed"
80+
81+
- name: 4. Setup Python Virtual Environment
82+
run: |
83+
echo "Creating Python virtual environment at ~/nat64_venv..."
84+
python3 -m venv ~/nat64_venv
85+
source ~/nat64_venv/bin/activate
86+
87+
pip install --upgrade pip setuptools wheel
88+
89+
echo "Installing Ansible and diskimage-builder..."
90+
pip install ansible-core
91+
pip install diskimage-builder
92+
93+
echo "Environment setup complete"
94+
95+
96+
- name: 5. Create Ansible Playbook for Building NAT64 Image
97+
run: |
98+
cat > ${{ github.workspace }}/build-nat64.yml << 'EOF'
99+
---
100+
- name: Build nat64-appliance image
101+
hosts: localhost
102+
connection: local
103+
gather_facts: true
104+
vars:
105+
cifmw_basedir: "${{ env.WORK_DIR }}"
106+
cifmw_nat64_appliance_run_dib_as_root: false
107+
cifmw_nat64_appliance_use_ci_script: false
108+
cifmw_nat64_appliance_venv_dir: "{{ ansible_env.HOME }}/nat64_venv"
109+
roles:
110+
- nat64_appliance
111+
EOF
112+
113+
echo "Ansible playbook created"
114+
cat ${{ github.workspace }}/build-nat64.yml
115+
116+
- name: 6. Run Ansible Playbook to Build Image
117+
run: |
118+
echo "Building NAT64 appliance image using Ansible..."
119+
echo "Using ci-framework role defaults for DIB configuration"
120+
echo "Skipping package installation (already done in step 3)"
121+
122+
cd ${{ github.workspace }}/ci-framework
123+
124+
# Activate the venv and run the playbook
125+
source ~/nat64_venv/bin/activate
126+
127+
# Skip 'packages' tag since we already installed dependencies
128+
# Use our venv at ~/nat64_venv instead of letting role create one
129+
ANSIBLE_ROLES_PATH=${{ github.workspace }}/ci-framework/roles \
130+
ansible-playbook -v \
131+
--skip-tags packages \
132+
${{ github.workspace }}/build-nat64.yml
133+
134+
echo "Image build completed successfully"
135+
136+
- name: 7. Verify Built Image
137+
run: |
138+
IMAGE_PATH="${{ env.WORK_DIR }}/nat64_appliance/nat64-appliance.qcow2"
139+
140+
if [ -f "$IMAGE_PATH" ]; then
141+
echo "Built image details:"
142+
ls -lh "$IMAGE_PATH"
143+
qemu-img info "$IMAGE_PATH"
144+
else
145+
echo "ERROR: Image file not found at $IMAGE_PATH"
146+
echo "Checking work directory contents:"
147+
ls -laR "${{ env.WORK_DIR }}"
148+
exit 1
149+
fi
150+
151+
- name: 8. Create Unique Release Tag and Rename Image
152+
id: release_tag
153+
run: |
154+
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
155+
# Manual run: use provided tag
156+
RELEASE_TAG="${{ inputs.release_tag }}"
157+
else
158+
# Scheduled run: generate timestamp-based tag
159+
RELEASE_TAG="build-$(date +%Y%m%d-%H%M%S)"
160+
fi
161+
162+
echo "release_tag=$RELEASE_TAG" >> $GITHUB_OUTPUT
163+
echo "Release tag: $RELEASE_TAG"
164+
165+
# Rename image to include the release tag
166+
TAGGED_IMAGE="nat64-appliance-${RELEASE_TAG}.qcow2"
167+
mv ${{ env.WORK_DIR }}/nat64_appliance/nat64-appliance.qcow2 \
168+
${{ github.workspace }}/${TAGGED_IMAGE}
169+
170+
echo "tagged_image=$TAGGED_IMAGE" >> $GITHUB_OUTPUT
171+
echo "Image renamed to: $TAGGED_IMAGE"
172+
ls -lh ${{ github.workspace }}/${TAGGED_IMAGE}
173+
174+
- name: 9. Create Versioned GitHub Release
175+
uses: softprops/action-gh-release@v2
176+
with:
177+
tag_name: ${{ steps.release_tag.outputs.release_tag }}
178+
name: "NAT64 Appliance ${{ steps.release_tag.outputs.release_tag }}"
179+
body: |
180+
NAT64 Appliance image built by GitHub Actions.
181+
182+
## Build Configuration
183+
184+
- **ci-framework repository**: `${{ inputs.ci_framework_repo || 'https://github.com/openstack-k8s-operators/ci-framework.git' }}`
185+
- **ci-framework reference**: `${{ inputs.ci_framework_ref || 'main' }}`
186+
- **Build type**: ${{ github.event_name == 'schedule' && 'Scheduled (weekly)' || 'Manual dispatch' }}
187+
188+
DIB configuration uses defaults from the ci-framework nat64_appliance role.
189+
190+
## Usage
191+
192+
For usage instructions and deployment examples, see the
193+
[nat64_appliance README](https://github.com/openstack-k8s-operators/ci-framework/blob/main/roles/nat64_appliance/README.md).
194+
files: ${{ steps.release_tag.outputs.tagged_image }}
195+
196+
- name: 10. Update 'latest' Release Tag
197+
uses: softprops/action-gh-release@v2
198+
with:
199+
tag_name: latest
200+
name: "NAT64 Appliance (Latest)"
201+
prerelease: true
202+
body: |
203+
This is the latest NAT64 Appliance build. This release is automatically updated.
204+
205+
**Latest Build**: ${{ steps.release_tag.outputs.release_tag }}
206+
207+
## Download
208+
209+
Use the static URL for the latest build:
210+
```bash
211+
curl -L -O https://github.com/openstack-k8s-operators/openstack-k8s-operators-ci/releases/download/latest/nat64-appliance-latest.qcow2
212+
```
213+
214+
Or download a specific version from the [releases page](https://github.com/openstack-k8s-operators/openstack-k8s-operators-ci/releases)
215+
to pin your CI to a known-good build.
216+
217+
## Usage
218+
219+
For usage instructions and deployment examples, see the
220+
[nat64_appliance README](https://github.com/openstack-k8s-operators/ci-framework/blob/main/roles/nat64_appliance/README.md).
221+
files: ${{ steps.release_tag.outputs.tagged_image }}
222+
223+
- name: 11. Upload Image with Static Filename to Latest Release
224+
run: |
225+
echo "Creating static filename copy for easy downloading..."
226+
227+
# Copy the image with a static name
228+
cp ${{ github.workspace }}/${{ steps.release_tag.outputs.tagged_image }} \
229+
${{ github.workspace }}/nat64-appliance-latest.qcow2
230+
231+
echo "Uploading to latest release with static filename..."
232+
# Upload to latest release, replacing any existing file with the same name
233+
gh release upload latest nat64-appliance-latest.qcow2 --clobber
234+
235+
echo "Static URL available at:"
236+
echo "https://github.com/${{ github.repository }}/releases/download/latest/nat64-appliance-latest.qcow2"
237+
env:
238+
GH_TOKEN: ${{ github.token }}
239+
240+
- name: 12. Cleanup Old Releases (Keep Last 4)
241+
if: github.event_name == 'schedule'
242+
run: |
243+
echo "Cleaning up old scheduled builds, keeping last 4..."
244+
245+
# Get all releases with 'build-' prefix, sort by creation date (newest first)
246+
# Skip the first 4 (keep them), delete the rest
247+
OLD_RELEASES=$(gh release list --limit 100 --json tagName,createdAt \
248+
| jq -r '.[] | select(.tagName | startswith("build-")) | .tagName' \
249+
| sort -r \
250+
| tail -n +5)
251+
252+
if [ -n "$OLD_RELEASES" ]; then
253+
echo "Deleting old releases:"
254+
echo "$OLD_RELEASES"
255+
echo "$OLD_RELEASES" | xargs -I {} gh release delete {} --yes --cleanup-tag
256+
echo "Cleanup complete"
257+
else
258+
echo "No old releases to delete (fewer than 5 total)"
259+
fi
260+
env:
261+
GH_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)