Skip to content

Test

Test #246

Workflow file for this run

name: Test
on:
# Runs automatically for PRs from branches within this repo (maintainers).
# Fork PRs do not have access to secrets; maintainers must trigger those
# via a /test comment (see test-slash-command.yml).
pull_request:
branches: [ main ]
# Triggered by test-slash-command.yml via peter-evans/slash-command-dispatch
# when a maintainer comments /test on a fork PR.
repository_dispatch:
types: [test-command]
# Allows manually triggering a test run against a specific ref from the UI.
workflow_dispatch:
inputs:
ref:
description: "Commit SHA or branch to test"
required: false
default: ""
merge_group:
permissions:
contents: read
jobs:
# A repository_dispatch run (triggered by a /test comment) is not linked to
# the PR, so nothing shows up in the PR's checks. Post a "pending" commit
# status against the fork PR's head SHA so the run is visible on the PR while
# it executes. The final result is reported by the tests-pass job below.
report-pending:
name: Report tests running
if: github.event_name == 'repository_dispatch'
runs-on: ubuntu-latest
permissions:
statuses: write
steps:
- name: Report pending status to PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
"repos/${{ github.repository }}/statuses/${{ github.event.client_payload.pull_request.head.sha }}" \
-f state=pending \
-f context="Tests Pass" \
-f description="Tests triggered via /test are running" \
-f target_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
build:
name: Build
# Skip fork PRs on the pull_request event — they have no secret access.
# Those PRs are tested via repository_dispatch triggered by test-slash-command.yml.
if: >-
github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
with:
ref: ${{ github.event.client_payload.pull_request.head.sha || inputs.ref || github.sha }}
- uses: taiki-e/install-action@just
- uses: actions/setup-go@v6
with:
go-version: stable
cache: false
- run: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@latest
name: Install dependencies
- name: Build package (for testing)
run: just build
- name: Upload build artifacts
uses: actions/upload-artifact@v7
with:
name: dist-packages
path: dist/
test:
name: Test ${{ matrix.job.name }}
needs: [build]
environment:
name: Test Auto
permissions:
# Required to publish system test results to the PR
issues: write
pull-requests: write
contents: read
runs-on: ubuntu-latest
env:
TEST_IMAGE: ${{ matrix.job.image }}
strategy:
fail-fast: false
matrix:
job:
- { name: "debian-systemd", image: "ghcr.io/thin-edge/tedge-demo-main-systemd:latest", test_options: "" }
steps:
- name: Checkout
uses: actions/checkout@v7
with:
# repository_dispatch: use the fork PR's head SHA from the slash command payload
# workflow_dispatch: use the manually specified ref (or default branch)
# pull_request / merge_group: use the default (current SHA)
ref: ${{ github.event.client_payload.pull_request.head.sha || inputs.ref || github.sha }}
- name: Download build artifacts
uses: actions/download-artifact@v8
with:
name: dist-packages
path: dist/
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: create .env file
run: |
touch .env
echo 'C8Y_BASEURL="${{ secrets.C8Y_BASEURL }}"' >> .env
echo 'C8Y_USER="${{ secrets.C8Y_USER }}"' >> .env
echo 'C8Y_PASSWORD="${{ secrets.C8Y_PASSWORD }}"' >> .env
- uses: actions/setup-python@v6
with:
python-version-file: tests/.python-version
cache: 'pip'
cache-dependency-path: |
tests/requirements.txt
- uses: taiki-e/install-action@just
#
# Test
#
- name: Install dependencies
run: |
just venv
- name: Run tests
run: just test ${{ matrix.job.test_options }}
- name: Upload test results
uses: actions/upload-artifact@v7
if: always()
with:
name: reports-${{ matrix.job.name }}
path: output
- name: Send report to commit
if: ${{ always() && (github.event_name == 'pull_request' || github.event_name == 'repository_dispatch') }}
uses: "joonvena/robotframework-reporter-action@v2.5"
with:
report_path: output
show_passed_tests: "false"
gh_access_token: ${{ secrets.GITHUB_TOKEN }}
pull_request_id: ${{ github.event.pull_request.number || github.event.client_payload.pull_request.number }}
sha: ${{ github.event.client_payload.pull_request.head.sha || github.sha }}
tests-pass:
name: Tests Pass
needs: [build, test]
# Run for every trigger EXCEPT fork PRs on the pull_request event: those skip
# build/test (no secret access), so this gate would otherwise report a red
# "Tests Pass" and permanently block the PR. Leaving it unreported keeps the
# required check pending until a maintainer comments /test, at which point the
# repository_dispatch run below reports the result onto the PR head commit.
if: >-
always() &&
(github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-latest
permissions:
# Required to report the gate result back to the fork PR's head commit
# for /test (repository_dispatch) runs.
statuses: write
steps:
- name: Check all matrix jobs passed
id: gate
run: |
if [ "${{ needs.build.result }}" != "success" ]; then
echo "Build job failed: ${{ needs.build.result }}"
exit 1
fi
if [ "${{ needs.test.result }}" != "success" ]; then
echo "One or more matrix jobs failed: ${{ needs.test.result }}"
exit 1
fi
- name: Report result to PR
# For /test runs, publish the gate outcome as a "Tests Pass" commit
# status on the PR head SHA so it shows on the PR and satisfies the
# required branch-protection check (repository_dispatch runs are not
# otherwise linked to the PR).
if: ${{ always() && github.event_name == 'repository_dispatch' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method POST \
"repos/${{ github.repository }}/statuses/${{ github.event.client_payload.pull_request.head.sha }}" \
-f state="${{ steps.gate.outcome == 'success' && 'success' || 'failure' }}" \
-f context="Tests Pass" \
-f description="Tests triggered via /test" \
-f target_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"