Skip to content

test: Refactor test structure and remove mock dependencies #74

test: Refactor test structure and remove mock dependencies

test: Refactor test structure and remove mock dependencies #74

Workflow file for this run

name: CI/CD Pipeline
on:
push:
branches:
- main
paths-ignore:
- README.md
pull_request:
branches:
- main
paths-ignore:
- README.md
schedule:
- cron: "0 0 * * 0" # Every Sunday at midnight
workflow_dispatch:
inputs:
force:
description: "Force run the workflow"
required: false
default: false
type: boolean
skip_build:
description: "Skip build and use local image (for local testing with act)"
required: false
default: false
type: boolean
jobs:
# ============================================
# Stage 1: Validation (runs in parallel)
# ============================================
script-validation:
name: Script Validation
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install shellcheck
run: |
sudo apt-get update
sudo apt-get install -y shellcheck
- name: Run shellcheck on scripts
run: find scripts/ -name "*.sh" -exec shellcheck {} \;
- name: Validate script syntax
run: find scripts/ -name "*.sh" -exec bash -n {} \;
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install bats-core
run: |
git clone https://github.com/bats-core/bats-core.git /tmp/bats-core
cd /tmp/bats-core
sudo ./install.sh /usr/local
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y curl jq
- name: Make scripts executable
run: chmod +x scripts/*.sh
- name: Run unit tests
run: bats tests/unit/
# ============================================
# Stage 2: Build Test Image (after validation)
# ============================================
build-test-image:
name: Build Test Image
runs-on: ubuntu-latest
needs: [unit-tests, script-validation]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up environment variables
run: |
chmod +x ./scripts/get-net-pwsh-versions.sh
./scripts/get-net-pwsh-versions.sh
echo "BUILD_ARGS<<EOF" >> $GITHUB_ENV
cat /tmp/env_vars >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Build args prepared:"
cat /tmp/env_vars
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Docker image (no push)
uses: docker/build-push-action@v6
with:
context: .
push: false
load: true
tags: jmcombs/powershell:test
platforms: linux/amd64
build-args: ${{ env.BUILD_ARGS }}
- name: Save Docker image as artifact
run: docker save jmcombs/powershell:test -o /tmp/test-image.tar
- name: Upload Docker image artifact
uses: actions/upload-artifact@v4
with:
name: test-image
path: /tmp/test-image.tar
retention-days: 1
# ============================================
# Stage 3: Integration Tests (using test image)
# ============================================
integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
needs: build-test-image
if: ${{ !inputs.skip_build }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download Docker image artifact
uses: actions/download-artifact@v4
with:
name: test-image
path: /tmp
- name: Load Docker image
run: |
docker load -i /tmp/test-image.tar
# Tag as :latest so integration tests can find it
docker tag jmcombs/powershell:test jmcombs/powershell:latest
echo "Available Docker images:"
docker images | grep powershell
- name: Install bats-core
run: |
git clone https://github.com/bats-core/bats-core.git /tmp/bats-core
cd /tmp/bats-core
sudo ./install.sh /usr/local
- name: Run integration tests
run: bats tests/integration/
integration-tests-local:
name: Integration Tests (Local Image)
runs-on: ubuntu-latest
if: ${{ inputs.skip_build }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Verify Docker image exists
run: |
echo "Checking for locally-built Docker image..."
if docker images | grep -q "jmcombs/powershell.*latest"; then
echo "✅ Found jmcombs/powershell:latest"
docker images | grep powershell
else
echo "❌ Error: jmcombs/powershell:latest not found"
echo ""
echo "Please build the image first:"
echo " ./scripts/build-local-arm64.sh"
exit 1
fi
- name: Install bats-core
run: |
git clone https://github.com/bats-core/bats-core.git /tmp/bats-core
cd /tmp/bats-core
sudo ./install.sh /usr/local
- name: Run integration tests
run: bats tests/integration/
- name: Test summary
if: always()
run: |
echo ""
echo "================================================"
echo "Integration Tests Complete"
echo "================================================"
echo ""
echo "Image tested: jmcombs/powershell:latest"
docker images | grep powershell || true
# ============================================
# Stage 4: Publish (only on main, after tests)
# ============================================
publish:
name: Build and Publish
runs-on: ubuntu-latest
needs: [integration-tests, integration-tests-local]
if: |
always() &&
github.ref == 'refs/heads/main' &&
github.event_name != 'pull_request' &&
!inputs.skip_build &&
(needs.integration-tests.result == 'success' || needs.integration-tests.result == 'skipped') &&
(needs.integration-tests-local.result == 'success' || needs.integration-tests-local.result == 'skipped')
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up environment variables
run: |
chmod +x ./scripts/get-net-pwsh-versions.sh
./scripts/get-net-pwsh-versions.sh
echo "BUILD_ARGS<<EOF" >> $GITHUB_ENV
cat /tmp/env_vars >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ github.repository }}
tags: |
type=raw,value=latest
labels: |
org.opencontainers.image.created=$GITHUB_RUN_DATE
org.opencontainers.image.authors=${{ env.AUTHOR }}
org.opencontainers.image.url=https://hub.docker.com/r/${{ github.repository }}
org.opencontainers.image.source=$GITHUB_SERVER_URL/${{ github.repository }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
sbom: true
push: true
tags: ${{ steps.meta.outputs.tags }}
# Note: Removed linux/arm/v7 as PowerShell module installation crashes
# under QEMU emulation on 32-bit ARM (exit code 134 / SIGABRT)
platforms: linux/amd64,linux/arm64
build-args: ${{ env.BUILD_ARGS }}
- name: Update README with versions
run: |
NET_RUNTIME_LTS_VERSION=$(grep 'NET_RUNTIME_LTS_VERSION' /tmp/env_vars | cut -d '=' -f 2)
PWSH_LTS_VERSION=$(grep 'PWSH_LTS_VERSION' /tmp/env_vars | cut -d '=' -f 2)
echo "NET_RUNTIME_LTS_VERSION=$NET_RUNTIME_LTS_VERSION" >> $GITHUB_ENV
echo "PWSH_LTS_VERSION=$PWSH_LTS_VERSION" >> $GITHUB_ENV
sed -i "s/| .NET Core Runtime | .*/| .NET Core Runtime | $NET_RUNTIME_LTS_VERSION |/" README.md
sed -i "s/| PowerShell Core | .*/| PowerShell Core | $PWSH_LTS_VERSION |/" README.md
- name: Commit version updates
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Update README.md with latest .NET Core Runtime and PowerShell Core versions"
branch: main
file_pattern: README.md
- name: Update README on Docker Hub
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
repository: ${{ github.repository }}