name: main
on:
  pull_request:
    branches:
    - master
  push:
    branches:
    - master
    tags:
    - v*
env:
  AGE_VERSION: 1.0.0
  GO_VERSION: 1.19.3
  GOFUMPT_VERSION: 0.4.0
  GOLANGCI_LINT_VERSION: 1.50.1
  GOVERSIONINFO_VERSION: 1.4.0
  TPARSE_VERSION: 0.11.1
jobs:
  changes:
    runs-on: ubuntu-20.04
    outputs:
      code: ${{ steps.filter.outputs.code }}
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - id: filter
      uses: dorny/paths-filter@4512585405083f25c027a35db413c2b3b9006d50
      with:
        filters: |
          code:
          - '**/*.go'
          - '**/*.ps1'
          - '**/*.tmpl'
          - '.github/workflows/**'
          - 'Makefile'
          - 'assets/docker/**'
          - 'assets/scripts/**'
          - 'assets/vagrant/**'
          - 'go.*'
          - 'pkg/**'
  codeql:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: ubuntu-18.04
    permissions:
      security-events: write
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
      with:
        fetch-depth: 1
    - uses: github/codeql-action/init@ec3cf9c605b848da5f1e41e8452719eb1ccfb9a6
      with:
        languages: go
    - uses: github/codeql-action/analyze@ec3cf9c605b848da5f1e41e8452719eb1ccfb9a6
  misspell:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: reviewdog/action-misspell@811b1e15f531430be3a5784e3d591bd657df18b0
      with:
        locale: US
  test-alpine:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - name: test
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        ( cd assets/docker && ./test.sh alpine )
  test-archlinux:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - name: test
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        ( cd assets/docker && ./test.sh archlinux )
  test-debian-i386:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: macos-12
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7
      with:
        path: ~/.vagrant.d
        key: ${{ runner.os }}-vagrant-debian-i386-${{ hashFiles('assets/vagrant/debian11-i386.Vagrantfile')
          }}
        restore-keys: |
          ${{ runner.os }}-vagrant-debian-i386-
    - name: test
      run: |
        ( cd assets/vagrant && ./test.sh debian11-i386 )
  test-fedora:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - name: test
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        ( cd assets/docker && ./test.sh fedora )
  test-freebsd:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: macos-12
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7
      with:
        path: ~/.vagrant.d
        key: ${{ runner.os }}-vagrant-freebsd13-${{ hashFiles('assets/vagrant/freebsd13.Vagrantfile')
          }}
        restore-keys: |
          ${{ runner.os }}-vagrant-freebsd13-
    - name: test
      run: |
        ( cd assets/vagrant && ./test.sh freebsd13 )
  test-legacy-go:
    strategy:
      matrix:
        go-version:
        - 1.18.x
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ matrix.go-version }}
    - name: build
      run: |
        go build ./...
    - name: run
      run: |
        go run . --version
    - name: install-age
      run: |
        cd $(mktemp -d)
        curl -fsSL https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-linux-amd64.tar.gz | tar xzf -
        sudo install -m 755 age/age /usr/local/bin
        sudo install -m 755 age/age-keygen /usr/local/bin
    - name: test
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        go test ./...
  test-macos:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: macos-11
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - name: build
      run: |
        go build ./...
    - name: run
      run: |
        go run . --version
    - name: install-age
      run: |
        cd $(mktemp -d)
        curl -fsSL https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-darwin-amd64.tar.gz | tar xzf -
        sudo install -m 755 age/age /usr/local/bin
        sudo install -m 755 age/age-keygen /usr/local/bin
    - name: test
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        go test -race ./...
    - name: test-install.sh
      run: |
        sh assets/scripts/install.sh
        bin/chezmoi --version
  test-release:
    needs: changes
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
      with:
        fetch-depth: 0
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - name: install-release-dependencies
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      run: |
        sudo apt-get --quiet update
        sudo apt-get --no-install-suggests --no-install-recommends --quiet --yes install musl-tools snapcraft
    - name: create-syso
      run: |
        make create-syso
    - name: build-release
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      uses: goreleaser/goreleaser-action@b508e2e3ef3b19d4e4146d4f8fb3ba9db644a757
      with:
        version: latest
        args: release --skip-publish --skip-sign --snapshot --timeout=1h
    - name: upload-artifact-chezmoi-darwin-amd64
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
      with:
        name: chezmoi-darwin-amd64
        path: dist/chezmoi-nocgo_darwin_amd64_v1/chezmoi
    - name: upload-artifact-chezmoi-darwin-arm64
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
      with:
        name: chezmoi-darwin-arm64
        path: dist/chezmoi-nocgo_darwin_arm64/chezmoi
    - name: upload-artifact-chezmoi-linux-amd64
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
      with:
        name: chezmoi-linux-amd64
        path: dist/chezmoi-cgo-glibc_linux_amd64_v1/chezmoi
    - name: upload-artifact-chezmoi-linux-musl-amd64
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
      with:
        name: chezmoi-linux-amd64-musl
        path: dist/chezmoi-cgo-musl_linux_amd64_v1/chezmoi
    - name: upload-artifact-chezmoi-windows-amd64.exe
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      uses: actions/upload-artifact@83fd05a356d7e2593de66fc9913b3002723633cb
      with:
        name: chezmoi-windows-amd64
        path: dist/chezmoi-nocgo_windows_amd64_v1/chezmoi.exe
  test-ubuntu:
    needs: changes
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
      with:
        fetch-depth: 0
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - name: install-age
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      run: |
        cd $(mktemp -d)
        curl -fsSL https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-linux-amd64.tar.gz | tar xzf -
        sudo install -m 755 age/age /usr/local/bin
        sudo install -m 755 age/age-keygen /usr/local/bin
    - name: build
      run: |
        go build ./...
    - name: run
      run: |
        go run . --version
    - name: install-tparse
      run: |
        curl -fsLO https://github.com/mfridman/tparse/releases/download/v${TPARSE_VERSION}/tparse_linux_x86_64
        chmod a+x tparse_linux_x86_64
        sudo mv tparse_linux_x86_64 /usr/local/bin/tparse
    - name: test-umask-022
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        go test -cover -json -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=0o022" -race ./... | tparse
    - name: test-umask-002
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        go test -cover -json -ldflags="-X github.com/twpayne/chezmoi/pkg/chezmoitest.umaskStr=0o002" -race ./... | tparse
    - name: test-install.sh
      if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
      run: |
        rm -f bin/chezmoi
        sh assets/scripts/install.sh
        bin/chezmoi --version
  test-website:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - name: install-website-dependencies
      run: pip3 install mkdocs-material mkdocs-mermaid2-plugin mkdocs-redirects mkdocs-simple-hooks
    - name: build-website
      run: ( cd assets/chezmoi.io && mkdocs build )
  test-windows:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: windows-2019
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    - name: build
      run: |
        go build ./...
    - name: run
      run: |
        go run . --version
    - name: test
      env:
        CHEZMOI_GITHUB_TOKEN: ${{ secrets.CHEZMOI_GITHUB_TOKEN }}
      run: |
        go test -race ./...
    - name: test-install.ps1
      run: |
        if (Test-Path -Path bin/chezmoi.exe) { Remove-Item -Force bin/chezmoi.exe }
        powershell -c assets/scripts/install.ps1
        bin/chezmoi.exe --version
    - name: test-install-irm
      run: |
        if (Test-Path -Path bin/chezmoi.exe) { Remove-Item -Force bin/chezmoi.exe }
        (irm -useb https://get.chezmoi.io/ps1) | powershell -c -
        bin/chezmoi.exe --version
    - name: test-install-iwr
      run: |
        if (Test-Path -Path bin/chezmoi.exe) { Remove-Item -Force bin/chezmoi.exe }
        (iwr -useb https://get.chezmoi.io/ps1).ToString() | powershell -c -
        bin/chezmoi.exe --version
  check:
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - name: generate
      run: |
        go generate
        git diff --exit-code
    - uses: ludeeus/action-shellcheck@94e0aab03ca135d11a35e5bfc14e6746dc56e7e9
      with:
        ignore: completions
    - name: lint-whitespace
      run: |
        go run ./internal/cmds/lint-whitespace
    - name: lint-txtar
      run: |
        find . -name \*.txtar | xargs go run ./internal/cmds/lint-txtar
    - name: findtypos
      run: |
        go install github.com/twpayne/findtypos@v0.0.1
        findtypos -format=github-actions chezmoi .
  lint:
    needs: changes
    if: github.event_name == 'push' || needs.changes.outputs.code == 'true'
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - uses: golangci/golangci-lint-action@07db5389c99593f11ad7b44463c2d4233066a9b1
      with:
        version: v${{ env.GOLANGCI_LINT_VERSION }}
        args: --timeout=5m
  release: # FIXME this should be merged into test-release above
    if: startsWith(github.ref, 'refs/tags/')
    needs:
    - check
    - lint
    - test-alpine
    - test-archlinux
    - test-debian-i386
    - test-fedora
    - test-freebsd
    - test-legacy-go
    - test-macos
    - test-release
    - test-ubuntu
    - test-website
    - test-windows
    runs-on: ubuntu-18.04
    steps:
    - name: install-build-dependencies
      run: |
        sudo apt-get --quiet update
        sudo apt-get --no-install-suggests --no-install-recommends --quiet --yes install musl-tools snapcraft
        sudo snap install --classic snapcraft
        sudo apt-get --quiet remove snapcraft
    - name: check-snapcraft-credentials
      run: snapcraft whoami
      env:
        SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
      with:
        fetch-depth: 0
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - uses: sigstore/cosign-installer@9becc617647dfa20ae7b1151972e9b3a2c338a2b
    - name: create-syso
      run: |
        make create-syso
    - uses: goreleaser/goreleaser-action@b508e2e3ef3b19d4e4146d4f8fb3ba9db644a757
      with:
        version: latest
        args: release --timeout=1h
      env:
        COSIGN_PWD: ${{ secrets.COSIGN_PWD }}
        GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
        SCOOP_GITHUB_TOKEN: ${{ secrets.SCOOP_GITHUB_TOKEN }}
        SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
      # The following is needed because chezmoi upgrade and
      # assets/scripts/install.sh have inconsistently looked for
      # chezmoi_${VERSION}_checksums.txt and checksums.txt. To ensure
      # compatibility with all versions, upload checksums.txt as well.
    - name: upload-checksums.txt
      run: |
        VERSION=${GITHUB_REF##*/v}
        cp dist/chezmoi_${VERSION}_checksums.txt dist/checksums.txt
        gh release upload v${VERSION} dist/checksums.txt
      env:
        GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
  deploy-website:
    needs:
    - release
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
      with:
        fetch-depth: 0
    - uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        cache: true
        go-version: ${{ env.GO_VERSION }}
    - name: prepare-chezmoi.io
      run: |
        pip3 install mkdocs-material mkdocs-mermaid2-plugin mkdocs-redirects mkdocs-simple-hooks
        ( cd assets/chezmoi.io && mkdocs build )
    - name: push-chezmoi.io
      run: |
        ( cd assets/chezmoi.io && mkdocs gh-deploy )
    - name: prepare-get.chezmoi.io
      run: |
        cp assets/scripts/install.sh assets/get.chezmoi.io/index.html
        cp assets/scripts/install.ps1 assets/get.chezmoi.io/ps1
        cp LICENSE assets/get.chezmoi.io/LICENSE
    - name: push-get.chezmoi.io
      uses: cpina/github-action-push-to-another-repository@9e487f29582587eeb4837c0552c886bb0644b6b9
      env:
        SSH_DEPLOY_KEY: ${{ secrets.GET_CHEZMOI_IO_SSH_DEPLOY_KEY }}
      with:
        source-directory: assets/get.chezmoi.io
        destination-github-username: chezmoi
        destination-repository-name: get.chezmoi.io
        target-branch: gh-pages
        commit-message: 'chore: Update from ORIGIN_COMMIT'
