I am currently using Azure DevOps and am migrating to GitHub. We have one pipeline that runs when a pull request in made active as it first caches the files, compares the main branch to the feature branch, and runs pre-commit on those changed files.
The ADO pipeline looks like:
---
# Pipeline to run pre-commit hooks
trigger: none
pool:
vmImage: ubuntu-latest
variables:
PRE_COMMIT_HOME: $(Pipeline.Workspace)/pre-commit-cache
steps:
- checkout: self
persistCredentials: true
- bash: echo "##vso[task.setvariable variable=PY]`python -V`"
displayName: Get python version
- task: CacheBeta@0
inputs:
key: pre-commit | .pre-commit-config.yaml | "$(PY)"
path: $(PRE_COMMIT_HOME)
- bash: |
git remote set-head origin --auto
[[ -n "${SYSTEM_PULLREQUEST_TARGETBRANCH}" ]] && COMPARE_BASE="origin/${SYSTEM_PULLREQUEST_TARGETBRANCH#refs/heads/}"
CHANGED_FILES=`git diff-tree --diff-filter=ACMRT --name-only -r ${COMPARE_BASE:-origin/HEAD}..${BUILD_SOURCEVERSION}`
if [[ -z "${CHANGED_FILES}" ]]; then
echo '##vso[task.logissue type=error]Can not detect changed files'
echo '##vso[task.complete result=Failed;]'
else
for file in ${CHANGED_FILES}; do
echo "##[debug]${file}"
done
sudo ln -s /usr/bin/pwsh /usr/bin/powershell
pip install --quiet pre-commit
pre-commit run --files ${CHANGED_FILES} || echo "Exit status is $?"
fi
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
displayName: 'Run pre-commit'
- pwsh: |
# Construct the REST URL to obtain Build ID
$uri = "$(System.TeamFoundationCollectionUri)/$(System.TeamProjectId)/_apis/build/builds/$(Build.BuildId)/logs/8?api-version=6.0"
# Invoke the REST call and capture the results
Write-Output "Getting the logs from the following URI: $uri"
$log= Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization = "Bearer $Env:SYSTEM_ACCESSTOKEN"}
$logs
if($log -match "exit code: 1")
{
Write-Host "##vso[task.setvariable variable=testsStatus]failed"
# Option 1: fail the pipeline
Write-Error "The tests not passed! Please check Run pre-commit step for details"
} elseif ($log -match "exit code: 3") {
Write-Host "##vso[task.setvariable variable=testsStatus]succeeded"
Write-Output "Succeeded with exit code 3 - baseline file update"
} else {
Write-Output "No error codes found in pre-commit. Tests passed successfully"
}
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
displayName: 'Check pre-commit status'
I am new to using GitHub actions and so far I have:
name: Pre-commit
on:
pull_request:
branches: [ "main" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: set PY
run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- name: Get changed files
id: changes
run: |
echo "::set-output name=all::$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)"
lint:
runs-on: ubuntu-latest
needs: build
steps:
- name: echo changed files
shell: bash
run:
git remote set-head origin --auto
sudo ln -s /usr/bin/pwsh /usr/bin/powershell
pip install --quiet pre-commit
pre-commit run --files ${{needs.build.outputs.all}} || echo "Exit status is $?"
With this GitHub action I am getting an error in the lint job saying fatal: not a git repository (or any of the parent directories): .git
. Is the issue within the lint job or a syntax elsewhere? The first job does not give any errors or telling messages.
CodePudding user response:
Every jobs run in isolated context so you should checkout the code in the lint
one. So, as example:
lint:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: echo changed files
shell: bash
As suggestion about linting only changed files, I would suggest to refactor your workflow in order to use the dorny/paths-filter action, that is suited for that (See the Real world usage examples)