Skip to content

Commit

Permalink
Improve workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hung3a8 committed Jan 18, 2024
1 parent 4886217 commit 715a605
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 25 deletions.
52 changes: 34 additions & 18 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ on:
workflow_dispatch:
inputs:
generate_iso:
description: 'Generate ISO image'
required: true
type: boolean
default: false
description: 'Choose generated ISO file storage location'
required: false
type: choice
options:
- 'server'
- 'release'
- 'none'

env:
image_name: "VNOI Ubuntu ${{ github.ref }}"
Expand All @@ -33,51 +36,64 @@ jobs:
run: echo $CONTENT | base64 -d > $FILENAME
env:
CONTENT: ${{ secrets.AUTHORIZED_KEYS }}
FILENAME: authorized_keys
FILENAME: src/misc/authorized_keys

- name: Write local_config.sh
- name: Write config.local.sh
run: echo $CONTENT | base64 -d > $FILENAME
env:
CONTENT: ${{ secrets.LOCAL_CONFIG }}
FILENAME: local_config.sh
CONTENT: ${{ secrets.CONFIG_LOCAL_SH }}
FILENAME: config.local.sh

- name: Write config.local.sh
- name: Write src/config.local.sh
run: echo $CONTENT | base64 -d > $FILENAME
env:
CONTENT: ${{ secrets.CONFIG_LOCAL_SH }}
CONTENT: ${{ secrets.SRC_CONFIG_LOCAL_SH }}
FILENAME: src/config.local.sh

- name: Write config.sh
- name: Write src/config.sh
run: echo $CONTENT | base64 -d > $FILENAME
env:
CONTENT: ${{ secrets.CONFIG_SH }}
CONTENT: ${{ secrets.SRC_CONFIG_SH }}
FILENAME: src/config.sh

- name: Build image
run: sudo ./build.sh icpc_build

# ----------------------- #
# Push to Github Releases #
# ----------------------- #
- name: Create Release
uses: actions/create-release@v1
if: ${{ github.event.inputs.generate_iso }}
if: ${{ github.event.inputs.generate_iso == 'release' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GENERATE_ISO: ${{ github.event.inputs.generate_iso }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ env.image_name }}
draft: false
prerelease: false

- name: Split ISO
if: ${{ github.event.inputs.generate_iso }}
if: ${{ github.event.inputs.generate_iso == 'release' }}
run: then mkdir iso-parts; split -b 2000MB live-build/contestant.iso iso-parts/contestant
env:
GENERATE_ISO: ${{ github.event.inputs.generate_iso }}

- name: Upload ISO
if: ${{ github.event.inputs.generate_iso }}
if: ${{ github.event.inputs.generate_iso == 'release' }}
uses: softprops/action-gh-release@v1
with:
files: iso-parts/contestant*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# ----------------------- #
# Push to Server #
# ----------------------- #
- name: Push to Server
if: ${{ github.event.inputs.generate_iso == 'server' }}
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.SCP_HOST }}
username: ${{ secrets.SCP_USERNAME }}
password: ${{ secrets.SCP_PASSWORD }}
source: live-build/contestant.iso
target: /home/${{ secrets.SERVER_USERNAME }}/contestant.iso
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local_config.sh
config.local.sh
live-build/
*.iso
authorized_keys
63 changes: 57 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ fi

SUDO_USER="root"

if [ -f local_config.sh ]; then
source local_config.sh
if [ -f config.local.sh ]; then
source config.local.sh
else
log "local_config.sh not found, running config.sh"
log "config.local.sh not found, running config.sh"
source config.sh
fi

Expand Down Expand Up @@ -129,8 +129,8 @@ icpc_build() {

log "Copy scripts and config to chroot"
cp -R build.sh chroot_install.sh config.sh authorized_keys src/ $CHROOT/root
if [ -f local_config.sh ]; then
cp local_config.sh $CHROOT/root
if [ -f config.local.sh ]; then
cp config.local.sh $CHROOT/root
fi
log "Done"

Expand All @@ -146,7 +146,7 @@ icpc_build() {
log "Done"

log "Cleanup scripts and config from chroot"
rm -f $CHROOT/root{build.sh,chroot_install.sh,config.sh,local_config.sh,authorized_keys,local_config.sh}
rm -f $CHROOT/root{build.sh,chroot_install.sh,config.sh,config.local.sh,authorized_keys}
log "Done"

log "Unmounting /dev and /run from chroot"
Expand Down Expand Up @@ -218,12 +218,60 @@ icpc_image_build() {
log "Build finished. Cleaning up (run clean command for full clean up)."
}

generate_actions_secret() {
if [ "$(uname)" == "Darwin" ]; then
BASE64_ENCODE="base64 -b0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
BASE64_ENCODE="base64 -w0"
fi
# Generate actions secret from config.local.sh
if [ -f config.local.sh ]; then
CONFIG_LOCAL_SH=$($BASE64_ENCODE < config.local.sh)
echo "config.local.sh: $CONFIG_LOCAL_SH"
fi

# from src/config.sh
if [ -f config.sh ]; then
SRC_CONFIG_SH=$($BASE64_ENCODE < config.sh)
echo "src/config.sh: $SRC_CONFIG_SH"
fi

# from src/config.local.sh
if [ -f src/config.local.sh ]; then
SRC_CONFIG_LOCAL_SH=$($BASE64_ENCODE < src/config.local.sh)
echo "src/config.local.sh: $SRC_CONFIG_LOCAL_SH"
fi

# from src/misc/authorized_keys
if [ -f src/misc/authorized_keys ]; then
AUTHORIZED_KEYS=$($BASE64_ENCODE < src/misc/authorized_keys)
echo "src/misc/authorized_keys: $AUTHORIZED_KEYS"
fi

# Ask user if they want to use gh cli to push secret to repo
read -p "Do you want to use gh cli to push secret to repo? (y/n) " -n 1 -r REPLY
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Set ACTIONS_SECRET to repo"
gh secret set CONFIG_LOCAL_SH -b "$CONFIG_LOCAL_SH"
gh secret set SRC_CONFIG_SH -b "$SRC_CONFIG_SH"
gh secret set SRC_CONFIG_LOCAL_SH -b "$SRC_CONFIG_LOCAL_SH"
gh secret set AUTHORIZED_KEYS -b "$AUTHORIZED_KEYS"
else
echo "Skipping"
fi
}

OPTIND=1 # Reset in case getopts has been used previously in the shell.

help() {
echo "Usage: $0 {icpc_build}"
echo
echo " icpc_build <image_file>: Build the ISO image based on the ICPC image"
echo " generate_actions_secret: Generate actions secret from config.local.sh"
echo " clean: Clean up all files generated by this script"
echo " help: Show this help"
}

case $1 in
Expand All @@ -233,6 +281,9 @@ case $1 in
icpc_build)
icpc_build $@
;;
generate_actions_secret)
generate_actions_secret
;;
help)
help
;;
Expand Down

0 comments on commit 715a605

Please sign in to comment.