Skip to content

Build Image

Build Image #42

Workflow file for this run

name: Build Image
on:
push:
branches:
- '*'
repository_dispatch:
types: [octopi_release]
workflow_dispatch:
inputs:
octopi_version:
description: "OctoPi version (leave empty to use latest stable release)"
required: false
default: ''
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: "⬇ Checkout"
uses: actions/checkout@v2
- name: "🔎 Determine OctoPi version"
uses: actions/github-script@v4
with:
script: |
let release = null;
let version = '';
if (context.eventName === "respository_dispatch" && context.action === "octopi_release") {
version = context.payload.client_payload.version;
console.log(`Version from repository dispatch: ${version}`);
} else if (context.eventName === "workflow_dispatch") {
version = context.payload.inputs.octopi_version;
console.log(`Version from workflow dispatch: ${version}`);
}
if (version) {
console.log(`Fetching release of ${version}`);
const query = `query {
repository(owner: "guysoft", name: "Octopi") {
release(tagName:"${version}") {
tagName
releaseAssets(first:5) {
nodes {
name
downloadUrl
}
}
}
}
}`;
const result = await github.graphql(query);
console.log({result});
release = result.repository.release;
} else {
console.log("Fetching latest release");
const query = `query {
repository(owner:"guysoft", name:"OctoPi") {
latestRelease {
tagName
releaseAssets(first:5) {
nodes {
name
downloadUrl
}
}
}
}
}`;
const result = await github.graphql(query);
console.log({result});
release = result.repository.latestRelease;
}
if (!release || !release.tagName || !release.releaseAssets || !release.releaseAssets.nodes) core.setFailed("Could not find OctoPi release");
const octopiVersion = release.tagName;
let octopiUrl = null;
for (const asset of release.releaseAssets.nodes) {
if (asset.name.startsWith("octopi-") && asset.name.endsWith(".zip")) {
octopiUrl = asset.downloadUrl;
break;
}
}
if (!octopiUrl) core.setFailed("Could not find OctoPi download URL");
console.log(`OctoPi version: ${octopiVersion}`)
console.log(`OctoPi download URL: ${octopiUrl}`)
core.exportVariable("OCTOPI_VERSION", octopiVersion)
core.exportVariable("OCTOPI_URL", octopiUrl)
- name: "⬇ Download OctoPi"
id: octopi_download
run: |
mkdir build
cd build
wget ${{ env.OCTOPI_URL }} -O octopi.zip
unzip octopi.zip
rm octopi.zip
IMAGE=$(ls *.img | head -n 1)
mv $IMAGE input.img
- name: "🏗 Run CustoPiZer"
uses: OctoPrint/CustoPiZer@main
with:
workspace: "${{ github.workspace }}/build"
scripts: "${{ github.workspace }}/src/scripts"
config: "${{ github.workspace }}/src/config"
- name: "✏ Rename image"
run: |
OCTOPI_VERSION="${{ env.OCTOPI_VERSION }}"
NOW="$(date +"%Y%m%d%H%M%S")"
IMAGE="octopi-$OCTOPI_VERSION-klipper-$NOW"
echo "IMAGE=$IMAGE" >> $GITHUB_ENV
echo "NOW=$NOW" >> $GITHUB_ENV
cd build
mv output.img $IMAGE.img
- name: "📝 Prepare release"
if: github.event_name == 'repository_dispatch' || github.event_name == 'workflow_dispatch'
run: |
RELEASE_NAME="OctoPi ${{ env.OCTOPI_VERSION }} with latest Klipper"
echo "RELEASE_NAME=$RELEASE_NAME" >> $GITHUB_ENV
RELEASE_TAG="${{ env.OCTOPI_VERSION }}.klipper.${{ env.NOW }}"
echo "RELEASE_TAG=$RELEASE_TAG" >> $GITHUB_ENV
RELEASE_BODY=""
(git rev-parse $RELEASE_TAG >/dev/null 2>&1) || RELEASE_BODY="Created with [CustoPiZer](https://github.com/OctoPrint/CustoPiZer)"
echo "RELEASE_BODY=$RELEASE_BODY" >> $GITHUB_ENV
- name: "📦 Package the image"
if: github.event_name == 'repository_dispatch' || github.event_name == 'workflow_dispatch'
id: package-image
uses: OctoPrint/actions/package-rpi-image@main
with:
image_path: "build/${{ env.IMAGE }}.img"
- name: "🆕 Generate rpi-imager.json snippet"
if: github.event_name == 'repository_dispatch' || github.event_name == 'workflow_dispatch'
uses: OctoPrint/actions/rpi-imager-snippet@main
with:
name: "${{ env.RELEASE_NAME }}"
description: "A Raspberry Pi distribution for 3d printers. Ships OctoPrint & Klipper out-of-the-box."
icon: "https://github.com/guysoft/OctoPi-Klipper/raw/main/media/rpi-imager-OctoPi-Klipper.png"
url: "https://github.com/guysoft/OctoPi-Klipper-CustoPiZer/releases/download/${{ env.RELEASE_TAG }}/${{ steps.package-image.outputs.zip_name }}"
output: "build/rpi-imager.json"
image_sha256: ${{ steps.package-image.outputs.image_sha256 }}
image_size: ${{ steps.package-image.outputs.image_size }}
zip_sha256: ${{ steps.package-image.outputs.zip_sha256 }}
zip_size: ${{ steps.package-image.outputs.zip_size }}
- name: "🔖 Create release & attach assets"
if: github.event_name == 'repository_dispatch' || github.event_name == 'workflow_dispatch'
uses: softprops/action-gh-release@v1
with:
name: "${{ env.RELEASE_NAME }}"
tag_name: "${{ env.RELEASE_TAG }}"
body: "${{ env.RELEASE_BODY }}"
prerelease: ${{ contains(env.OCTOPI_VERSION, 'rc') }}
fail_on_unmatched_files: true
files: |
build/${{ steps.package-image.outputs.zip_name }}
build/${{ steps.package-image.outputs.zip_name }}.md5
build/${{ steps.package-image.outputs.zip_name }}.sha256
build/rpi-imager.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: "⬆ Upload build artifact"
uses: actions/upload-artifact@v2
if: github.event_name == 'push'
with:
name: ${{ env.IMAGE }}
path: build/${{ env.IMAGE }}.img
- name: "🔖 Generate rpi-imager json"
uses: OctoPrint/actions/rpi-imager-json@main
with:
token: "${{ secrets.GITHUB_TOKEN }}"
owner: guysoft
repo: OctoPi-Klipper
output: ./rpi-imager.json
nameStable: "OctoPi-Klipper (stable)"
namePrerelease: "OctoPi-Klipper (prerelease)"
- name: "⬆ Publish Files To rpi-imager"
if: github.event_name == 'repository_dispatch' || github.event_name == 'workflow_dispatch'
id: upload
uses: Creepios/[email protected]
with:
host: ${{ secrets.FTP_HOST }}
port: 22
username: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
localPath: './rpi-imager.json'
remotePath: '/rpi-imager/rpi-imager-octopi-klipper.json'