Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active December 6, 2021 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/9434e6fa5977f9626b470b46a0d02149 to your computer and use it in GitHub Desktop.
Save Integralist/9434e6fa5977f9626b470b46a0d02149 to your computer and use it in GitHub Desktop.
[Github Action Workflow Example] #github #git #actions #workflows

See https://github.com/Integralist/actions-testing for working examples.

DOCS: Syntax and Expressions.

The following example demonstrates a few things...

  1. Events using on.
  2. Generating multiple jobs using strategy.matrix.
  3. Overriding specific variables using include.
  4. Setting environment variables using env.
  5. Defining a multiline script (for readability) using yaml | operator.
# .github/workflows/release.yaml 

name: Main

on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  build:
    strategy:
      matrix:
        rust-toolchain: [stable]
        os: [ubuntu-latest, macos-latest, windows-latest]
        arch: [amd64, arm64]
        include:
          - os: ubuntu-latest
            name: linux
          - os: macos-latest
            name: darwin
            rust-target: x86_64-apple-darwin
          - os: windows-latest
            name: windows
            extension: .exe
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: true

      - name: Install latest Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust-toolchain }}
          target: ${{ matrix.rust-target }}
          default: true
          override: true

      - name: Extract tag name
        uses: olegtarasov/get-tag@v2.1
        id: tagName
        
    - name: Test
      run: make test
      shell: bash
      env:
        TEST_COMPUTE_INIT: true
        TEST_COMPUTE_BUILD: true
        TEST_COMPUTE_DEPLOY: true

      - name: Build
        run: |
          cargo build --all --release --locked
          cd target/release

      - name: Package
        run: |
          strip viceroy${{ matrix.extension }}
          tar czf viceroy_${{ steps.tagName.outputs.tag }}_${{ matrix.name }}-${{ martrix.arch }}.tar.gz viceroy${{ matrix.extension }}

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            target/release/viceroy_${{ steps.tagName.outputs.tag }}_${{ matrix.name }}-${{ martrix.arch }}.tar.gz
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# .github/workflows/release.yaml
name: Main
on:
push:
tags:
- "v*.*.*"
jobs:
build:
strategy:
matrix:
rust-toolchain: [stable]
os: [ubuntu-latest, macos-11, windows-latest]
arch: [amd64, arm64]
exclude:
- os: windows-latest
arch: arm64
include:
- os: ubuntu-latest
name: linux
rust_abi: unknown-linux-gnu
- os: macos-11
name: darwin
rust_abi: apple-darwin
- os: windows-latest
name: windows
rust_abi: pc-windows-msvc
extension: .exe
- arch: arm64
rust_arch: aarch64
- arch: amd64
rust_arch: x86_64
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: true
- name: Install latest Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
target: ${{ matrix.rust_arch }}-${{ matrix.rust_abi }}
default: true
override: true
- name: Install C cross-compilation toolchain
if: ${{ matrix.name == 'linux' && matrix.arch != 'amd64' }}
run: |
sudo apt install -f -y gcc-${{ matrix.rust_arch }}-linux-gnu
echo CC=${{ matrix.rust_arch }}-linux-gnu-gcc >> $GITHUB_ENV
echo RUSTFLAGS='-C linker=${{ matrix.rust_arch }}-linux-gnu-gcc' >> $GITHUB_ENV
- name: Extract tag name
uses: olegtarasov/get-tag@v2.1
id: tagName
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --release --all --locked --target=${{ matrix.rust_arch }}-${{ matrix.rust_abi }}
- name: Strip symbols (linux)
if: ${{ matrix.name == 'linux' }}
run: |
${{ matrix.rust_arch }}-linux-gnu-strip target/${{ matrix.rust_arch }}-${{ matrix.rust_abi }}/release/viceroy${{ matrix.extension }}
- name: Strip symbols (non-linux)
if: ${{ matrix.name != 'linux' }}
run: |
strip target/${{ matrix.rust_arch }}-${{ matrix.rust_abi }}/release/viceroy${{ matrix.extension }}
- name: Package
run: |
cd target/${{ matrix.rust_arch }}-${{ matrix.rust_abi }}/release
tar czf viceroy_${{ steps.tagName.outputs.tag }}_${{ matrix.name }}-${{ matrix.arch }}.tar.gz viceroy${{ matrix.extension }}
- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
target/${{ matrix.rust_arch }}-${{ matrix.rust_abi }}/release/viceroy_${{ steps.tagName.outputs.tag }}_${{ matrix.name }}-${{ matrix.arch }}.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment