Use appropriate conditional statements in CI workflows to ensure steps execute under the right circumstances and handle different environments gracefully.
Key practices:
if: ${{ always() }} for cleanup/finalization steps that must run regardless of previous step failuresif: ${{ runner.environment != 'self-hosted' }} for steps that should only run on certain runner typesExample from the discussions:
- name: Publish artifact marking this job as done
uses: actions/upload-artifact@v4
if: ${{ always() }} # Ensures cleanup runs even if job fails
with:
# artifact configuration
- name: Setup Python
if: ${{ runner.environment != 'self-hosted' }} # Only on GitHub-hosted
uses: ./.github/actions/setup-python
- name: Change Mirror Priorities
if: ${{ runner.environment != 'self-hosted' }} # Skip on self-hosted
uses: ./.github/actions/apt-mirrors
This approach prevents workflow failures due to missing infrastructure, ensures proper cleanup, and optimizes resource usage by skipping unnecessary steps on different runner types.