index : static-web-server.git

ascending towards madness

freebsd_instance:
  image: freebsd-12-2-release-amd64

# Test FreeBSD in a full VM on cirrus-ci.com
# The binary will be also built in 32-bit mode, but will execute on a
# 64-bit kernel and in a 64-bit environment. Our tests don't execute
# any of the system's binaries, so the environment shouldn't matter.
task:
  only_if: $CIRRUS_TAG == ''
  env:
    RUSTFLAGS: -Dwarnings
  matrix:
    - name: freebsd-amd64-test
      setup_script:
        - pkg install -y bash curl
        - curl https://sh.rustup.rs -sSf --output rustup.sh
        - sh rustup.sh -y --profile minimal --default-toolchain stable
        - . $HOME/.cargo/env
        - |
          echo "~~~~ rustc --version ~~~~"
          rustc --version
          rustup show active-toolchain
      test_script:
        - . $HOME/.cargo/env
        - cargo test --all
      build_test_script:
        - . $HOME/.cargo/env
        - |
          cargo build --verbose

    - name: freebsd-i686-test
      setup_script:
        - pkg install -y bash curl
        - curl https://sh.rustup.rs -sSf --output rustup.sh
        - sh rustup.sh -y --profile minimal --default-toolchain stable
        - . $HOME/.cargo/env
        - rustup target add i686-unknown-freebsd
        - |
          echo "~~~~ rustc --version ~~~~"
          rustc --version
          rustup show active-toolchain
      test_script:
        - . $HOME/.cargo/env
        - |
          cargo test --all --target i686-unknown-freebsd
      build_test_script:
        - . $HOME/.cargo/env
        - |
          cargo build --verbose --target i686-unknown-freebsd

task:
  name: freebsd-release
  only_if: $CIRRUS_TAG != ''
  env:
    RUSTFLAGS: -Dwarnings
    GITHUB_TOKEN: ENCRYPTED[d1766ef328d83729917d2ffb875d64c35d1c0177edf8f32e66ec464daf5c1b7b145d65fc6c044a73fffe2235d3b38349]
  setup_script:
    - pkg install -y bash curl jq
    - curl https://sh.rustup.rs -sSf --output rustup.sh
    - sh rustup.sh -y --profile minimal --default-toolchain stable
    - . $HOME/.cargo/env
    - rustup target add i686-unknown-freebsd
    - |
      echo "~~~~ rustc --version ~~~~"
      rustc --version
  build_script: |
    . $HOME/.cargo/env
    cargo build --verbose --release --target=x86_64-unknown-freebsd
    cargo build --verbose --release --target=i686-unknown-freebsd
  archive_x86_64_script: |
    . $HOME/.cargo/env
    staging="static-web-server-$CIRRUS_TAG-x86_64-unknown-freebsd"
    mkdir -p "$staging/"
    cp "target/x86_64-unknown-freebsd/release/static-web-server" "$staging/"
    strip "$staging/static-web-server"
    cp {README.md,LICENSE-APACHE,LICENSE-MIT} "$staging/"
    tar czf "$staging.tar.gz" "$staging"
  archive_i686_script: |
    . $HOME/.cargo/env
    staging="static-web-server-$CIRRUS_TAG-i686-unknown-freebsd"
    mkdir -p "$staging/"
    cp "target/i686-unknown-freebsd/release/static-web-server" "$staging/"
    strip "$staging/static-web-server"
    cp {README.md,LICENSE-APACHE,LICENSE-MIT} "$staging/"
    tar czf "$staging.tar.gz" "$staging"
  upload_script: |
    #!/usr/bin/env bash

    set -e

    . $HOME/.cargo/env

    if [[ "$GITHUB_TOKEN" == "" ]]; then
      echo "GitHub access token GITHUB_TOKEN env is not provided!"
      exit 1
    fi

    RETRIES=0
    until [ $RETRIES -eq 20 ]
    do
      echo "Finding the GitHub release associated with '$CIRRUS_TAG' tag..."
      CIRRUS_RELEASE=$(curl -sL -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$CIRRUS_REPO_FULL_NAME/releases | jq -c "[ .[] | select( .tag_name | contains(\"$CIRRUS_TAG\")) ] | .[0]" | jq -r '.id')
      [[ "$CIRRUS_RELEASE" != "null" ]] && break
      RETRIES=$((RETRIES+1))
      sleep 30
    done

    if [[ "$CIRRUS_RELEASE" == "null" ]]; then
        echo "Can not find the associated GitHub '$CIRRUS_TAG' release!"
        exit 1
    fi

    echo "GitHub release '$CIRRUS_TAG' found. Preparing asset files to upload..."

    file_content_type="application/octet-stream"
    files_to_upload=(
      static-web-server-$CIRRUS_TAG-i686-unknown-freebsd.tar.gz
      static-web-server-$CIRRUS_TAG-x86_64-unknown-freebsd.tar.gz
    )

    for fpath in "${files_to_upload[@]}"
    do
      echo "Uploading GitHub release asset '$fpath'..."
      name=$(basename "$fpath")
      url_to_upload="https://uploads.github.com/repos/$CIRRUS_REPO_FULL_NAME/releases/$CIRRUS_RELEASE/assets?name=$name"
      curl -LX POST \
        --data-binary @$fpath \
        --header "Authorization: token $GITHUB_TOKEN" \
        --header "Content-Type: $file_content_type" \
        $url_to_upload
    done

    echo
    echo "GitHub release '$CIRRUS_TAG' assets uploaded successfully."