<commit_before>name: Continuous Integration

on:
  push:
    paths-ignore:
      - '**/*.md'
  pull_request:
    paths-ignore:
      - 'doc/**'
      - 'contrib/**'
      - '**/*.md'

env:
  MAKEJOBS: 3
  DOCKER_BUILDKIT: 1
  GITIAN_DIR: /opt/gitian-builder
  GITIAN_CACHE: /opt/gitian-builder/cache
  DEBIAN_FRONTEND: noninteractive
  USE_DOCKER: 1

jobs:
  binary:
    runs-on: ubuntu-latest
    container: docker:stable-dind
    strategy:
      matrix:
        name:
          - armhf-linux
          - aarch64-linux
          - x86_64-linux
          - x86_64-macos
          - x86_64-win
        include:
          - os: linux
            name: armhf-linux
            host: arm-linux-gnueabihf
          - os: linux
            name: aarch64-linux
            host: aarch64-linux-gnu
          - os: linux
            name: x86_64-linux
            host: x86_64-linux-gnu
          - os: osx
            name: x86_64-macos
            host: x86_64-apple-darwin16
          - os: win
            name: x86_64-win
            host: x86_64-w64-mingw32
    steps:
      - name: Set up environment
        run: |
          apk add --no-cache curl make ruby wget git sed tar grep bash
          git clone https://github.com/devrandom/gitian-builder.git ${GITIAN_DIR}
          mkdir -p ${GITIAN_DIR}/inputs ${GITIAN_DIR}/var
          wget https://github.com/mikefarah/yq/releases/download/v4.13.5/yq_linux_amd64.tar.gz -O - |\
            tar xz && mv yq_linux_amd64 /usr/bin/yq

      - name: Fetch OSX SDK
        if: ${{ matrix.os == 'osx' }}
        run: |
          wget -N https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX10.14.sdk.tar.xz
          tar xf MacOSX10.14.sdk.tar.xz
          tar czf ${GITIAN_DIR}/inputs/MacOSX10.14.sdk.tar.gz MacOSX10.14.sdk
          rm -rf MacOSX10.14.sdk MacOSX10.14.sdk.tar.xz

      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Detect suite
        id: detect
        run: |
          DESCRIPTOR=$PWD/contrib/gitian-descriptors/gitian-${{ matrix.os }}.yml
          NAME=$(yq e '.name' ${DESCRIPTOR})
          echo ::set-output name=descriptor::${DESCRIPTOR}
          echo ::set-output name=name::${NAME}
          echo ::set-output name=version::$(echo ${NAME} | grep -Eo [0-9.]+)
          echo ::set-output name=suite::$(yq e '.suites[0]' ${DESCRIPTOR})
          echo ::set-output name=architecture::$(yq e '.architectures[0]' ${DESCRIPTOR})
          echo ::set-output name=build-dir::${PWD}

      - name: Build gitian base image
        run: |
          docker build --tag base-${{ steps.detect.outputs.suite }}-${{ steps.detect.outputs.architecture }} -<<EOF
            FROM ubuntu:${{ steps.detect.outputs.suite }}
            ENV DEBIAN_FRONTEND=noninteractive
            RUN apt-get update && apt-get --no-install-recommends -y install pciutils build-essential git subversion language-pack-en wget lsb-release
            RUN useradd -ms /bin/bash -U ubuntu
            USER ubuntu:ubuntu
            WORKDIR /home/ubuntu
            CMD ["sleep", "infinity"]
          EOF

      - name: Cache common
        uses: actions/cache@v2
        id: gitian-common
        env:
          cache-name: gitian-common
        with:
          path: ${{ env.GITIAN_CACHE }}/common
          key: ${{ env.cache-name }}-${{ hashFiles('depends/packages/*') }}

      - name: Cache arch
        uses: actions/cache@v2
        env:
          cache-name: gitian-host
        with:
          path: ${{ env.GITIAN_CACHE }}/${{ steps.detect.outputs.name }}/${{ matrix.host }}
          key: ${{ matrix.name }}-${{ env.cache-name }}-${{ hashFiles('depends/packages/*') }}

      - name: Change release description if tag
        if: startsWith(github.ref, 'refs/tags/')
        shell: bash
        run: |
          sed -i "s|^\(DESC=\"\).*|\1$(echo ${GITHUB_REF/refs\/tags\//})\"|" share/genbuild.sh
          git config user.name temp
          git config user.email <EMAIL>
          git add share/genbuild.sh
          git commit -m "Update DESC"

      - name: Download common
        if: steps.gitian-common.outputs.cache-hit != 'true'
        run: |
          cd ${GITIAN_DIR}
          make -j $MAKEJOBS -C ${{ steps.detect.outputs.build-dir }}/depends download SOURCES_PATH=${GITIAN_CACHE}/common
          cd -

      - name: Build binary
        run: |
          cd ${GITIAN_DIR}
          sed -i "s/^\ \ \(HOSTS=\"\).*/\ \ \1${{ matrix.host }}\"/g" ${{ steps.detect.outputs.descriptor }}
          ./bin/gbuild -j $MAKEJOBS --commit peercoin=$(git -C ${{ steps.detect.outputs.build-dir }} log --format="%H" -n 1) --url peercoin=${{ steps.detect.outputs.build-dir }} ${{ steps.detect.outputs.descriptor }}
          cp -r ${GITIAN_DIR}/build/out/* ${{ steps.detect.outputs.build-dir }}/
          cd -

      - name: Get short SHA
        id: slug
        run: echo ::set-output name=sha8::$(echo ${{ github.sha }} | cut -c1-8)

      - uses: actions/upload-artifact@v2
        with:
          name: peercoin-${{ steps.slug.outputs.sha8 }}-${{ matrix.name }}
          path: |
            *.tar.gz
            *.zip
            *.exe
            *.dmg
            !*-debug*
            !*-unsigned.tar.gz
          retention-days: 5
  docker:
    runs-on: ubuntu-latest
    needs: [binary]
    steps:
      - uses: actions/checkout@v2
        with:
          repository: peercoin/packaging

      - uses: actions/download-artifact@v2
        with:
          path: docker

      - name: Place artifacts
        working-directory: docker
        run: |<commit_msg>do not cache common downloads<commit_after>name: Continuous Integration

on:
  push:
    paths-ignore:
      - '**/*.md'
  pull_request:
    paths-ignore:
      - 'doc/**'
      - 'contrib/**'
      - '**/*.md'

env:
  MAKEJOBS: 3
  DOCKER_BUILDKIT: 1
  GITIAN_DIR: /opt/gitian-builder
  GITIAN_CACHE: /opt/gitian-builder/cache
  DEBIAN_FRONTEND: noninteractive
  USE_DOCKER: 1

jobs:
  binary:
    runs-on: ubuntu-latest
    container: docker:stable-dind
    strategy:
      matrix:
        name:
          - armhf-linux
          - aarch64-linux
          - x86_64-linux
          - x86_64-macos
          - x86_64-win
        include:
          - os: linux
            name: armhf-linux
            host: arm-linux-gnueabihf
          - os: linux
            name: aarch64-linux
            host: aarch64-linux-gnu
          - os: linux
            name: x86_64-linux
            host: x86_64-linux-gnu
          - os: osx
            name: x86_64-macos
            host: x86_64-apple-darwin16
          - os: win
            name: x86_64-win
            host: x86_64-w64-mingw32
    steps:
      - name: Set up environment
        run: |
          apk add --no-cache curl make ruby wget git sed tar grep bash
          git clone https://github.com/devrandom/gitian-builder.git ${GITIAN_DIR}
          mkdir -p ${GITIAN_DIR}/inputs ${GITIAN_DIR}/var
          wget https://github.com/mikefarah/yq/releases/download/v4.13.5/yq_linux_amd64.tar.gz -O - |\
            tar xz && mv yq_linux_amd64 /usr/bin/yq

      - name: Fetch OSX SDK
        if: ${{ matrix.os == 'osx' }}
        run: |
          wget -N https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX10.14.sdk.tar.xz
          tar xf MacOSX10.14.sdk.tar.xz
          tar czf ${GITIAN_DIR}/inputs/MacOSX10.14.sdk.tar.gz MacOSX10.14.sdk
          rm -rf MacOSX10.14.sdk MacOSX10.14.sdk.tar.xz

      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Detect suite
        id: detect
        run: |
          DESCRIPTOR=$PWD/contrib/gitian-descriptors/gitian-${{ matrix.os }}.yml
          NAME=$(yq e '.name' ${DESCRIPTOR})
          echo ::set-output name=descriptor::${DESCRIPTOR}
          echo ::set-output name=name::${NAME}
          echo ::set-output name=version::$(echo ${NAME} | grep -Eo [0-9.]+)
          echo ::set-output name=suite::$(yq e '.suites[0]' ${DESCRIPTOR})
          echo ::set-output name=architecture::$(yq e '.architectures[0]' ${DESCRIPTOR})
          echo ::set-output name=build-dir::${PWD}

      - name: Build gitian base image
        run: |
          docker build --tag base-${{ steps.detect.outputs.suite }}-${{ steps.detect.outputs.architecture }} -<<EOF
            FROM ubuntu:${{ steps.detect.outputs.suite }}
            ENV DEBIAN_FRONTEND=noninteractive
            RUN apt-get update && apt-get --no-install-recommends -y install pciutils build-essential git subversion language-pack-en wget lsb-release
            RUN useradd -ms /bin/bash -U ubuntu
            USER ubuntu:ubuntu
            WORKDIR /home/ubuntu
            CMD ["sleep", "infinity"]
          EOF

      - name: Cache dependencies
        uses: actions/cache@v2
        id: gitian-deps
        env:
          cache-name: gitian-host
        with:
          path: ${{ env.GITIAN_CACHE }}/${{ steps.detect.outputs.name }}/${{ matrix.host }}
          key: ${{ matrix.name }}-${{ env.cache-name }}-${{ hashFiles('depends/packages/*') }}

      - name: Change release description if tag
        if: startsWith(github.ref, 'refs/tags/')
        shell: bash
        run: |
          sed -i "s|^\(DESC=\"\).*|\1$(echo ${GITHUB_REF/refs\/tags\//})\"|" share/genbuild.sh
          git config user.name temp
          git config user.email <EMAIL>
          git add share/genbuild.sh
          git commit -m "Update DESC"

      - name: Build binary
        working-directory: ${{ env.GITIAN_DIR }}
        run: |
          sed -i "s/^\ \ \(HOSTS=\"\).*/\ \ \1${{ matrix.host }}\"/g" ${{ steps.detect.outputs.descriptor }}
          ./bin/gbuild -j $MAKEJOBS --commit peercoin=$(git -C ${{ steps.detect.outputs.build-dir }} log --format="%H" -n 1) --url peercoin=${{ steps.detect.outputs.build-dir }} ${{ steps.detect.outputs.descriptor }}
          cp -r ${GITIAN_DIR}/build/out/* ${{ steps.detect.outputs.build-dir }}/

      - name: Get short SHA
        id: slug
        run: echo ::set-output name=sha8::$(echo ${{ github.sha }} | cut -c1-8)

      - uses: actions/upload-artifact@v2
        with:
          name: peercoin-${{ steps.slug.outputs.sha8 }}-${{ matrix.name }}
          path: |
            *.tar.gz
            *.zip
            *.exe
            *.dmg
            !*-debug*
            !*-unsigned.tar.gz
          retention-days: 5
  docker:
    runs-on: ubuntu-latest
    needs: [binary]
    steps:
      - uses: actions/checkout@v2
        with:
          repository: peercoin/packaging

      - uses: actions/download-artifact@v2
        with:
          path: docker

      - name: Place artifacts
        working-directory: docker
        run: |
{'fraction_non_alphanumeric': 0.14287056171331955, 'fraction_numerical': 0.019631786586511366, 'mean_word_length': 1.7321016166281755, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5600240', 'n_tokens_mistral': 3775, 'n_tokens_neox': 3447, 'n_words': 746}