-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,10 @@ on: | |
branches: | ||
- main | ||
workflow_dispatch: | ||
env: | ||
IMAGE_NAME: ublue-update | ||
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }} | ||
|
||
jobs: | ||
push-ghcr: | ||
name: Build and push image | ||
name: Build and test image | ||
runs-on: ubuntu-24.04 | ||
permissions: | ||
contents: read | ||
|
@@ -34,47 +31,6 @@ jobs: | |
- name: Checkout Push to Registry action | ||
uses: actions/checkout@v4 | ||
|
||
- name: Generate tags | ||
id: generate-tags | ||
shell: bash | ||
run: | | ||
# Generate a timestamp for creating an image version history | ||
TIMESTAMP="$(date +%Y%m%d)" | ||
MAJOR_VERSION="${{ matrix.major_version }}" | ||
COMMIT_TAGS=() | ||
BUILD_TAGS=() | ||
# Have tags for tracking builds during pull request | ||
SHA_SHORT="${GITHUB_SHA::7}" | ||
COMMIT_TAGS+=("pr-${{ github.event.pull_request.number }}-${MAJOR_VERSION}") | ||
COMMIT_TAGS+=("${SHA_SHORT}-${MAJOR_VERSION}") | ||
if [[ "${{ matrix.is_latest_version }}" == "true" ]] && \ | ||
[[ "${{ matrix.is_stable_version }}" == "true" ]]; then | ||
COMMIT_TAGS+=("pr-${{ github.event.pull_request.number }}") | ||
COMMIT_TAGS+=("${SHA_SHORT}") | ||
fi | ||
BUILD_TAGS=("${MAJOR_VERSION}" "${MAJOR_VERSION}-${TIMESTAMP}") | ||
if [[ "${{ matrix.is_latest_version }}" == "true" ]] && \ | ||
[[ "${{ matrix.is_stable_version }}" == "true" ]]; then | ||
BUILD_TAGS+=("latest") | ||
fi | ||
if [[ "${{ github.event_name }}" == "pull_request" ]]; then | ||
echo "Generated the following commit tags: " | ||
for TAG in "${COMMIT_TAGS[@]}"; do | ||
echo "${TAG}" | ||
done | ||
alias_tags=("${COMMIT_TAGS[@]}") | ||
else | ||
alias_tags=("${BUILD_TAGS[@]}") | ||
fi | ||
echo "Generated the following build tags: " | ||
for TAG in "${BUILD_TAGS[@]}"; do | ||
echo "${TAG}" | ||
done | ||
echo "alias_tags=${alias_tags[*]}" >> $GITHUB_OUTPUT | ||
- name: Install Deps | ||
run: | | ||
sudo apt-get install just podman | ||
|
@@ -90,54 +46,3 @@ jobs: | |
id: test_image | ||
run: | | ||
just container-test | ||
# Workaround bug where capital letters in your GitHub username make it impossible to push to GHCR. | ||
# https://github.com/macbre/push-to-ghcr/issues/12 | ||
- name: Lowercase Registry | ||
id: registry_case | ||
uses: ASzc/change-string-case-action@v6 | ||
with: | ||
string: ${{ env.IMAGE_REGISTRY }} | ||
|
||
# Push the image to GHCR (Image Registry) | ||
- name: Push To GHCR | ||
uses: redhat-actions/push-to-registry@v2 | ||
id: push | ||
if: github.event_name != 'pull_request' | ||
env: | ||
REGISTRY_USER: ${{ github.actor }} | ||
REGISTRY_PASSWORD: ${{ github.token }} | ||
with: | ||
image: ${{ steps.build_image.outputs.image }} | ||
tags: ${{ steps.build_image.outputs.tags }} | ||
registry: ${{ steps.registry_case.outputs.lowercase }} | ||
username: ${{ env.REGISTRY_USER }} | ||
password: ${{ env.REGISTRY_PASSWORD }} | ||
extra-args: | | ||
--disable-content-trust | ||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v3 | ||
if: github.event_name != 'pull_request' | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# Sign container | ||
- uses: sigstore/[email protected] | ||
if: github.event_name != 'pull_request' | ||
|
||
- name: Sign container image | ||
if: github.event_name != 'pull_request' | ||
run: | | ||
cosign sign -y --key env://COSIGN_PRIVATE_KEY ${{ steps.registry_case.outputs.lowercase }}/${{ env.IMAGE_NAME }}@${TAGS} | ||
env: | ||
TAGS: ${{ steps.push.outputs.digest }} | ||
COSIGN_EXPERIMENTAL: false | ||
COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }} | ||
|
||
- name: Echo outputs | ||
if: github.event_name != 'pull_request' | ||
run: | | ||
echo "${{ toJSON(steps.push.outputs) }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import os | ||
import sys | ||
from unittest.mock import patch | ||
|
||
sys.path.insert( | ||
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../src")) | ||
) | ||
|
||
from ublue_update.update_drivers.brew import detect_user, brew_update, BREW_PREFIX, BREW_CELLAR, BREW_REPO | ||
|
||
|
||
@patch('os.path.isdir') | ||
@patch('os.stat') | ||
def test_detect_user_success(mock_stat, mock_isdir): | ||
mock_isdir.return_value = True | ||
mock_stat.return_value.st_uid = 1001 | ||
|
||
assert detect_user() == 1001 | ||
|
||
mock_isdir.assert_called_once_with(BREW_PREFIX) | ||
mock_stat.assert_called_once_with(BREW_PREFIX) | ||
|
||
@patch('os.path.isdir') | ||
def test_detect_user_failure(mock_isdir): | ||
mock_isdir.return_value = False | ||
|
||
assert detect_user() == -1 | ||
|
||
mock_isdir.assert_called_once_with(BREW_PREFIX) | ||
|
||
@patch('ublue_update.update_drivers.brew.run_uid') | ||
@patch('os.environ', {'PATH': '/usr/bin'}) | ||
@patch('os.path.isdir') | ||
@patch('os.stat') | ||
@patch('ublue_update.update_drivers.brew.log') | ||
def test_brew_update(mock_log, mock_stat, mock_isdir, mock_run_uid): | ||
# Setup | ||
mock_isdir.return_value = True | ||
mock_stat.return_value.st_uid = 1001 | ||
mock_run_uid.return_value.returncode = 0 # Simulate a successful command | ||
|
||
brew_update(True) | ||
|
||
# Test that brew_update returns early when dry_run is True | ||
mock_run_uid.assert_not_called() | ||
mock_log.info.assert_not_called() | ||
|
||
brew_update(False) | ||
env = [ | ||
f"--E=HOMEBREW_PREFIX='{BREW_PREFIX}'", | ||
f"--E=HOMEBREW_CELLAR='{BREW_CELLAR}'", | ||
f"--E=HOMEBREW_REPOSITORY='{BREW_REPO}'", | ||
f"--E=PATH='/usr/bin:{BREW_PREFIX}/bin:{BREW_PREFIX}/sbin'", | ||
] | ||
|
||
mock_run_uid.assert_any_call(1001, env + [ | ||
"brew", "update" | ||
]) | ||
mock_run_uid.assert_any_call(1001, env + [ | ||
"brew", "upgrade" | ||
]) | ||
|
||
@patch('ublue_update.update_drivers.brew.run_uid') | ||
@patch('os.environ', {'PATH': '/usr/local/bin'}) | ||
@patch('os.path.isdir') | ||
@patch('os.stat') | ||
@patch('ublue_update.update_drivers.brew.log') | ||
def test_brew_update_failure(mock_log, mock_stat, mock_isdir, mock_run_uid): | ||
mock_isdir.return_value = True | ||
mock_stat.return_value.st_uid = 1001 | ||
mock_run_uid.return_value.returncode = 1 # Simulate a failure in the `brew update` command | ||
mock_run_uid.return_value.stdout = b"Error" | ||
|
||
brew_update(False) | ||
|
||
mock_log.error.assert_any_call("Error") | ||
|
||
@patch('ublue_update.update_drivers.brew.run_uid') | ||
@patch('os.environ', {'PATH': '/usr/local/bin'}) | ||
@patch('os.path.isdir') | ||
@patch('os.stat') | ||
@patch('ublue_update.update_drivers.brew.log') | ||
def test_brew_update_upgrade_failure(mock_log, mock_stat, mock_isdir, mock_run_uid): | ||
mock_isdir.return_value = True | ||
mock_stat.return_value.st_uid = 1001 | ||
mock_run_uid.return_value.returncode = 0 # Simulate a successful `brew update` | ||
mock_run_uid.return_value.stdout = b"Update complete" | ||
|
||
mock_run_uid.return_value.returncode = 1 # Simulate a failure during `brew upgrade` | ||
mock_run_uid.return_value.stdout = b'Upgrade error' | ||
|
||
brew_update(False) | ||
|
||
mock_log.error.assert_any_call('Upgrade error') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.