Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor env variables in release workflow #256

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 99 additions & 62 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,53 @@ on:
required: true
type: string

# Configure constants for this workflow.
# Configure the release workflow by editing these values.
env:
# The base filename of the binary produced by `cargo build`.
BINARY: bevy_quickstart
# The name to use for the packaged application produced by this workflow.
PACKAGE_NAME: bevy_quickstart
# The itch.io page to upload to, in the format: `user-name/project-name`.
# Comment this out to disable.
ITCH_TARGET: the-bevy-flock/bevy-quickstart
# The organization or author that owns the rights to the game.
OWNER: the-bevy-flock
cargo_build_binary_name: bevy_quickstart

# The path to the assets directory.
ASSETS_DIR: assets
# Whether packages produced by this workflow should be uploaded to the Github release.
UPLOAD_PACKAGES_TO_GITHUB_RELEASE: true
assets_path: assets

# Whether to upload the packages produced by this workflow to a GitHub release.
upload_to_github: true

# The itch.io project to upload to in the format `user-name/project-name`.
# There will be no upload to itch.io if this is commented out.
upload_to_itch: the-bevy-flock/bevy-quickstart

############
# ADVANCED #
############

# The ID of the app produced by this workflow.
# Applies to macOS releases.
# Must contain only A-Z, a-z, 0-9, hyphen, and period: https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleidentifier
app_id: the-bevy-flock.bevy-quickstart

# The base filename of the binary in the package produced by this workflow.
# Applies to Windows, macOS, and Linux releases.
# Defaults to `cargo_build_binary_name` if commented out.
#app_binary_name: bevy_quickstart

# The name of the `.zip` or `.dmg` file produced by this workflow.
# Defaults to `app_binary_name` if commented out.
app_package_name: bevy-quickstart

# The display name of the app produced by this workflow.
# Applies to macOS releases.
# Defaults to `app_package_name` if commented out.
app_display_name: Bevy Quickstart

# The short display name of the app produced by this workflow.
# Applies to macOS releases.
# Must be 15 or fewer characters: https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundlename
# Defaults to `app_display_name` if commented out.
#app_short_name: Bevy Quickstart

# Before enabling LFS, please take a look at GitHub's documentation for costs and quota limits:
# https://docs.github.com/en/repositories/working-with-files/managing-large-files/about-storage-and-bandwidth-usage
USE_GIT_LFS: false
git_lfs: false

jobs:
# Determine the version number for this workflow.
Expand All @@ -50,7 +79,7 @@ jobs:
needs:
- get-version
env:
VERSION: ${{ needs.get-version.outputs.version }}
version: ${{ needs.get-version.outputs.version }}
strategy:
matrix:
include:
Expand Down Expand Up @@ -78,12 +107,12 @@ jobs:
- platform: macos
targets: x86_64-apple-darwin aarch64-apple-darwin
profile: release-native
out_dir_suffix: .app/Contents/MacOS
app_suffix: .app/Contents/MacOS
package_ext: .dmg
runner: macos-latest
runs-on: ${{ matrix.runner }}
permissions:
# Required to create a GitHub release (https://docs.github.com/en/rest/releases/releases#create-a-release).
# Required to create a GitHub release: https://docs.github.com/en/rest/releases/releases#create-a-release
contents: write
defaults:
run:
Expand All @@ -92,32 +121,41 @@ jobs:
steps:
- name: Set up environment
run: |
echo 'PACKAGE=${{ env.PACKAGE_NAME }}-${{ matrix.platform }}' >> "${GITHUB_ENV}"
echo 'OUT_DIR=tmp/package/${{ env.PACKAGE_NAME }}${{ matrix.out_dir_suffix }}' >> "${GITHUB_ENV}"
# Default values:
echo "app_binary_name=${app_binary_name:=${{ env.cargo_build_binary_name }}}" >> "${GITHUB_ENV}"
echo "app_package_name=${app_package_name:=${app_binary_name}}" >> "${GITHUB_ENV}"
echo "app_display_name=${app_display_name:=${app_package_name}}" >> "${GITHUB_ENV}"
echo "app_short_name=${app_short_name:=${app_display_name}}" >> "${GITHUB_ENV}"

# File paths:
echo "app=tmp/app/${app_package_name}"'${{ matrix.app_suffix }}' >> "${GITHUB_ENV}"
echo "package=${app_package_name}-"'${{ matrix.platform }}${{ matrix.package_ext }}' >> "${GITHUB_ENV}"

# macOS environment:
if [ '${{ matrix.platform }}' == 'macos' ]; then
echo 'MACOSX_DEPLOYMENT_TARGET=11.0' >> "${GITHUB_ENV}" # MacOS 11.0 Big Sur is the first version to support universal binaries.
echo 'MACOSX_DEPLOYMENT_TARGET=11.0' >> "${GITHUB_ENV}" # macOS 11.0 Big Sur is the first version to support universal binaries.
echo "SDKROOT=$(xcrun --sdk macosx --show-sdk-path)" >> "${GITHUB_ENV}"
fi

- name: Checkout repository
uses: actions/checkout@v4
with:
lfs: ${{ env.USE_GIT_LFS }}
lfs: ${{ env.git_lfs }}

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.targets }}

- name: Populate target directory from cache
- name: Populate cargo cache
uses: Leafwing-Studios/cargo-cache@v2

- name: Install dependencies (Linux)
if: ${{ matrix.platform == 'linux' }}
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev

- name: Prepare output directories
run: rm -rf tmp; mkdir -p tmp/binary '${{ env.OUT_DIR }}'
run: rm -rf tmp; mkdir -p tmp/binary '${{ env.app }}'

- name: Install cargo-binstall (Web)
if: ${{ matrix.platform == 'web' }}
Expand All @@ -127,51 +165,51 @@ jobs:
if: ${{ matrix.platform == 'web' }}
run: |
cargo binstall --no-confirm trunk wasm-bindgen-cli wasm-opt
trunk build --release --dist '${{ env.OUT_DIR }}'
trunk build --release --dist '${{ env.app }}'

- name: Build binaries (non-Web)
if: ${{ matrix.platform != 'web' }}
run: |
for target in ${{ matrix.targets }}; do
cargo build --profile='${{ matrix.profile }}' --target="${target}" --no-default-features --features='${{ matrix.features }}'
mv target/"${target}"/'${{ matrix.profile }}/${{ env.BINARY }}${{ matrix.binary_ext }}' tmp/binary/"${target}"'${{ matrix.binary_ext }}'
mv target/"${target}"/'${{ matrix.profile }}/${{ env.cargo_binary_name }}${{ matrix.binary_ext }}' tmp/binary/"${target}"'${{ matrix.binary_ext }}'
done

- name: Add binaries to package (non-Web)
- name: Add binaries to app (non-Web)
if: ${{ matrix.platform != 'web' }}
run: |
if [ '${{ matrix.platform }}' == 'macos' ]; then
lipo tmp/binary/*'${{ matrix.binary_ext }}' -create -output '${{ env.OUT_DIR }}/${{ env.PACKAGE_NAME }}${{ matrix.binary_ext }}'
lipo tmp/binary/*'${{ matrix.binary_ext }}' -create -output '${{ env.app }}/${{ env.app_binary_name }}${{ matrix.binary_ext }}'
else
mv tmp/binary/*'${{ matrix.binary_ext }}' '${{ env.OUT_DIR }}/${{ env.PACKAGE_NAME }}${{ matrix.binary_ext }}'
mv tmp/binary/*'${{ matrix.binary_ext }}' '${{ env.app }}/${{ env.app_binary_name }}${{ matrix.binary_ext }}'
fi

- name: Add assets to package (non-Web)
- name: Add assets to app (non-Web)
if: ${{ matrix.platform != 'web' }}
run: cp -r '${{ env.ASSETS_DIR }}' '${{ env.OUT_DIR }}' || true # Ignore error if assets folder does not exist
run: cp -r ./'${{ env.assets_path }}' '${{ env.app }}' || true # Ignore error if assets folder does not exist

- name: Add app metadata to package (MacOS)
- name: Add metadata to app (macOS)
if: ${{ matrix.platform == 'macos' }}
run: |
cat > '${{ env.OUT_DIR }}/../Info.plist' << EOF
cat >'${{ env.app }}/../Info.plist' <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>${{ env.PACKAGE_NAME }}</string>
<string>${{ env.app_display_name }}</string>
<key>CFBundleExecutable</key>
<string>${{ env.PACKAGE_NAME }}</string>
<string>${{ env.app_binary_name }}</string>
<key>CFBundleIdentifier</key>
<string>${{ env.OWNER }}.${{ env.PACKAGE_NAME }}</string>
<string>${{ env.app_id }}</string>
<key>CFBundleName</key>
<string>${{ env.PACKAGE_NAME }}</string>
<string>${{ env.app_short_name }}</string>
<key>CFBundleShortVersionString</key>
<string>${{ env.VERSION }}</string>
<string>${{ env.version }}</string>
<key>CFBundleVersion</key>
<string>${{ env.VERSION }}</string>
<string>${{ env.version }}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
Expand All @@ -184,61 +222,60 @@ jobs:
</plist>
EOF

- name: Finish package (non-Windows)
- name: Package app (non-Windows)
if: ${{ matrix.platform != 'windows' }}
working-directory: tmp/package
working-directory: tmp/app
run: |
if [ '${{ matrix.platform }}' == 'macos' ]; then
ln -s /Applications .
hdiutil create -fs HFS+ -volname '${{ env.PACKAGE_NAME }}' -srcfolder . '${{ env.PACKAGE }}${{ matrix.package_ext }}'
hdiutil create -fs HFS+ -volname '${{ env.app_package_name }}' -srcfolder . '${{ env.package }}'
else
zip --recurse-paths '${{ env.PACKAGE }}${{ matrix.package_ext }}' '${{ env.PACKAGE_NAME }}'
zip --recurse-paths '${{ env.package }}' '${{ env.app_package_name }}'
fi

- name: Finish package (Windows)
- name: Package app (Windows)
if: ${{ matrix.platform == 'windows' }}
working-directory: tmp/package
working-directory: tmp/app
shell: pwsh
run: Compress-Archive -Path '${{ env.PACKAGE_NAME }}' -DestinationPath '${{ env.PACKAGE }}${{ matrix.package_ext }}'
run: Compress-Archive -Path '${{ env.app_package_name }}' -DestinationPath '${{ env.package }}'

- name: Upload package to artifacts
- name: Upload package to workflow artifacts
uses: actions/upload-artifact@v4
with:
path: tmp/package/${{ env.PACKAGE }}${{ matrix.package_ext }}
path: tmp/app/${{ env.package }}
name: package-${{ matrix.platform }}
retention-days: 1

- name: Upload package to Github release
if: ${{ env.UPLOAD_PACKAGES_TO_GITHUB_RELEASE == 'true' }}
- name: Upload package to GitHub release
if: ${{ env.upload_to_github == 'true' }}
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: tmp/package/${{ env.PACKAGE }}${{ matrix.package_ext }}
asset_name: ${{ env.PACKAGE }}${{ matrix.package_ext }}
release_name: ${{ env.VERSION }}
tag: ${{ env.VERSION }}
file: tmp/app/${{ env.package }}
asset_name: ${{ env.package }}
release_name: ${{ env.version }}
tag: ${{ env.version }}
overwrite: true

# Get itch.io target from env, because the `env` context can't be used in the `if:` condition of a job.
# See: https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
get-itch-target:
# Check if upload to itch.io is enabled.
# This is needed because the `env` context can't be used in the `if:` condition of a job:
# https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
check-upload-to-itch:
runs-on: ubuntu-latest
steps:
- name: Do nothing
run: 'true'
outputs:
itch-target: ${{ env.ITCH_TARGET }}
target: ${{ env.upload_to_itch }}

# Upload all packages to itch.io.
upload-to-itch:
runs-on: ubuntu-latest
needs:
- get-version
- get-itch-target
- check-upload-to-itch
- build
env:
VERSION: ${{ needs.get-version.outputs.version }}
if: ${{ needs.get-itch-target.outputs.itch-target != '' }}
if: ${{ needs.check-upload-to-itch.outputs.target != '' }}

steps:
- name: Download all packages
Expand All @@ -261,7 +298,7 @@ jobs:
for channel in $(ls tmp); do
./butler push \
--fix-permissions \
--userversion='${{ env.VERSION }}' \
--userversion='${{ needs.get-version.outputs.version }}' \
tmp/"${channel}"/* \
'${{ env.ITCH_TARGET }}':"${channel#package-}"
'${{ env.upload_to_itch }}':"${channel#package-}"
done
64 changes: 46 additions & 18 deletions docs/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,51 @@ The release workflow can be configured by tweaking the environment variables in
<summary>Click here for a list of variables and how they're used.</summary>

```yaml
env:
# The base filename of the binary produced by `cargo build`.
BINARY: bevy_template
# The name to use for the packaged application produced by this workflow.
PACKAGE_NAME: bevy-template
# The itch.io page to upload to, in the format: `user-name/project-name`.
# Comment this out to disable.
ITCH_TARGET: the-bevy-flock/bevy-template
# The organization or author that owns the rights to the game.
OWNER: the-bevy-flock
# The path to the assets directory.
ASSETS_DIR: assets
# Whether packages produced by this workflow should be uploaded to the Github release.
UPLOAD_PACKAGES_TO_GITHUB_RELEASE: true
# Before enabling LFS, please take a look at GitHub's documentation for costs and quota limits:
# https://docs.github.com/en/repositories/working-with-files/managing-large-files/about-storage-and-bandwidth-usage
USE_GIT_LFS: false
# The base filename of the binary produced by `cargo build`.
cargo_build_binary_name: bevy_quickstart

# The path to the assets directory.
assets_path: assets

# Whether to upload the packages produced by this workflow to a GitHub release.
upload_to_github: true

# The itch.io project to upload to in the format `user-name/project-name`.
# There will be no upload to itch.io if this is commented out.
upload_to_itch: the-bevy-flock/bevy-quickstart
benfrankel marked this conversation as resolved.
Show resolved Hide resolved

############
# ADVANCED #
############

# The ID of the app produced by this workflow.
# Applies to macOS releases.
# Must contain only A-Z, a-z, 0-9, hyphens, and periods: https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleidentifier
app_id: the-bevy-flock.bevy-quickstart

# The base filename of the binary in the package produced by this workflow.
# Applies to Windows, macOS, and Linux releases.
# Defaults to `cargo_build_binary_name` if commented out.
app_binary_name: bevy_quickstart

# The name of the `.zip` or `.dmg` file produced by this workflow.
# Defaults to `app_binary_name` if commented out.
app_package_name: bevy_quickstart

# The display name of the app produced by this workflow.
# Applies to macOS releases.
# Defaults to `app_package_name` if commented out.
app_display_name: Bevy Quickstart

# The short display name of the app produced by this workflow.
# Applies to macOS releases.
# Must be 15 or fewer characters: https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundlename
# Defaults to `app_display_name` if commented out.
app_short_name: Bevy Quickstart

# Before enabling LFS, please take a look at GitHub's documentation for costs and quota limits:
# https://docs.github.com/en/repositories/working-with-files/managing-large-files/about-storage-and-bandwidth-usage
git_lfs: false
```
</details>

Expand All @@ -86,7 +114,7 @@ Hit `New repository secret` and enter the following values, then hit `Add secret

#### Create itch.io project

Create a new itch.io project with the same user and project name as in the `ITCH_TARGET` variable in [`.github/workflows/release.yaml`](../.github/workflows/release.yaml).
Create a new itch.io project with the same user and project name as in the `upload_to_itch` variable in [`.github/workflows/release.yaml`](../.github/workflows/release.yaml).
Hit `Save & view page` at the bottom of the page.

[Trigger the release workflow](#cd-releasing) for the first time. Once it's done, go back to itch.io and hit `Edit game` in the top left.
Expand Down