From 73e4d52ff3c36588d101e377e529d295315b629f Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 26 Mar 2025 10:36:42 +0100 Subject: [PATCH 01/62] actions: Add checkout Signed-off-by: Jorge Marques --- README.md | 17 +++++++++ checkout/README.md | 32 ++++++++++++++++ checkout/action.yml | 90 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 README.md create mode 100644 checkout/README.md create mode 100644 checkout/action.yml diff --git a/README.md b/README.md new file mode 100644 index 00000000..ac14e2f9 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +Shared composite GitHub Actions +=============================== + +Contains reusable and public GitHub Actions focused on performance. +This is the v1 release branch. + +Use as follows: + +``` +on: workflow_call +jobs: + checks: + runs-on: [self-hosted, v1] + + steps: + - uses: analogdevices/doctools/checkout@v1 +``` diff --git a/checkout/README.md b/checkout/README.md new file mode 100644 index 00000000..aa3511c3 --- /dev/null +++ b/checkout/README.md @@ -0,0 +1,32 @@ +Checkout +======== + +An alternative to https://github.com/actions/checkout with entrypoints for +https://github.com/nektos/act/ with https://github.com/containers/podman + +Usage: + +``` +on: workflow_call +jobs: + checks: + runs-on: [self-hosted, v1] + + steps: + - uses: analogdevicesinc/doctools/checkout@v1 +``` + +Key-points: + +* For pull-requests, rebase on base reference instead of awkward merge commit. +* Current checkout branch is always called ``trunk`` +* Sets the ``depth_sha``, ``base_sha``, and ``head_sha`` to ease running checkscripts. + +When working locally, the user can set the following enviroment variables: + +* ACT_DEPTH: The depth when fetching, overwrites depth +* ACT_HEAD: The head to fetch + +At repository level, the owner shall change the local mountpoint from ``/mnt/repo`` to +something else by setting input ``act-mountpoint``. +A motivation maybe compatibility with a specific container, file structure. diff --git a/checkout/action.yml b/checkout/action.yml new file mode 100644 index 00000000..dd98124a --- /dev/null +++ b/checkout/action.yml @@ -0,0 +1,90 @@ +name: Git Checkout +description: A git checkout for local runners and runs + +inputs: + depth: + description: The depth when fetching + required: false + act: + description: Enable local run mode with act + required: false + act-mountpoint: + description: The source mountpoint + default: '/mnt/repo' + gh-token: + description: 'GitHub Token' + default: ${{ github.token }} +#env: +# ACT_DEPTH: The depth when fetching, overwrites depth +# ACT_HEAD: The head to fetch + +runs: + using: composite + steps: + - name: Get depth + shell: bash + run: | + if [[ "${{ env.ACT_DEPTH }}" ]]; then + echo "depth_sha=$(( ${{ env.ACT_DEPTH }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ inputs.depth }}" ]]; then + echo "depth_sha=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.pull_request.commits }}" ]]; then + echo "depth_sha=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + else + echo "depth_sha=6" >> $GITHUB_ENV + fi + + if [[ "${{ env.ACT }}" ]]; then + if [[ "${{ env.ACT_HEAD }}" ]]; then + act_head_sha="${{ env.ACT_HEAD }}" + else + act_head_sha="@" + fi + echo "head_sha=$(cd ${{ inputs.act-mountpoint }} ; git rev-parse $act_head_sha)" >> $GITHUB_ENV + elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then + echo "head_sha=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV + elif [[ "${{ github.sha }}" ]]; then + echo "head_sha=${{ github.sha }}" >> $GITHUB_ENV + fi + + - name: Prepare git + shell: bash + run: | + if [[ "${{ env.ACT }}" ]]; then + url="file://${{ inputs.act-mountpoint }}" + else + url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" + fi + + if [[ ! -d .git ]]; then + git init --initial-branch=trunk . + git remote add origin "$url" + else + git remote set-url origin "$url" + fi + + git reset --hard + git clean -xf . + + if [[ ! "$(git rev-parse --abbrev-ref HEAD 2>/dev/null)" == "trunk" ]]; then + if [[ $(git rev-parse --verify "trunk" 2>/dev/null) ]]; then + git branch -D trunk + fi + git branch -m trunk + fi + + - name: Checkout + shell: bash + run: | + git fetch origin --depth=$depth_sha $head_sha + git reset --hard $head_sha + + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + git fetch origin --depth=1 ${{ github.base_ref }} + git rebase origin/${{ github.base_ref }} + echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" + fi + + echo "base_sha=$(git rev-parse @~$((depth_sha - 1)))" >> "$GITHUB_ENV" + git log --oneline --reverse | head -200 + From c6c950bb13284414d1ebc870b4781e8b8fb3896d Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 26 Mar 2025 10:57:01 +0100 Subject: [PATCH 02/62] ci: Add aux scripts Signed-off-by: Jorge Marques --- entrypoint.sh | 62 +++++++++++++++++++++++++++++ github-api.sh | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 entrypoint.sh create mode 100644 github-api.sh diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..6cb49596 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +runner_version=v1 +github_token=$(cat /run/secrets/github_token 2> /dev/null) +org_repository=$(cat /run/secrets/org_repository 2> /dev/null) + +source /usr/local/bin/github-api.sh + +if [[ -z $org_repository ]]; then + echo "No org_repository provided" + exit 1 +fi + +function get_runner_token () { + if [[ ! -z $github_token ]]; then + runner_token=$( + gh-actions-token $github_token \ + $org_repository \ + runner_token + ) + + if [[ "$runner_token" == "null" ]]; then + echo "Failed to get '$org_repository' runner_token, check github_token permission" + exit 1 + fi + else + runner_token=$(cat /run/secrets/runner_token 2> /dev/null) + + if [[ -z $runner_token ]]; then + echo "No runner_token or github_token provided" + exit 1 + fi + fi +} + +get_runner_token + +if [[ ! -z $runner_labels ]]; then + runner_version+="-$runner_labels" +fi + +name=$(echo $org_repository | sed 's|/|-|g')-$(echo $runner_token | sha3sum -a 256 | head -c4)-$(tr -dc A-Za-z0-9 > "$GITHUB_ENV" + echo $release_id +} + +gh-get-asset-id() +{ + asset_id=$(curl -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$2/releases/$3/assets" \ + | jq -r ".[] | select(.name==\"$asset_name\") | .id") + echo "$4=$asset_id" >> "$GITHUB_ENV" + echo $asset_id +} + +gh-create-release() +{ + release_id=$(curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$2/releases" \ + -d "{\"tag_name\":\"$3\",\"name\":\"$3\",\"make_latest\":\"true\"}" \ + | jq -r .id) + echo "$4=$release_id" >> "$GITHUB_ENV" + echo $release_id +} + +gh-upload-asset() +{ + curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + -H "Content-Type: application/octet-stream" \ + "https://uploads.github.com/repos/$2/releases/$3/assets?name=$4" \ + --data-binary "@$5" +} + +gh-delete-asset() +{ + curl -L \ + -X DELETE \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$2/releases/assets/$3" +} + +gh-update-commitish() +{ + curl -s \ + -X PATCH \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + -d '{"target_commitish": "$3"}' \ + "https://api.github.com/repos/$2/releases/$4" +} + +gh-actions-token() +{ + runner_token=$(curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$2/actions/runners/registration-token" \ + | jq -r .token) + echo $runner_token +} + +gh-get-number-commits() +{ + total_commits=$(curl -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$2/compare/$3...$4" \ + | jq ".total_commits") + echo "total_commits=$total_commits" >> "$GITHUB_ENV" + echo $total_commits +} + +gh-cancel-workflow() +{ + curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $1" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/$2/actions/runs/$3/cancel +} From c07257d1f70b70721b08bd750df5ec7a6934c306 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 26 Mar 2025 11:18:32 +0100 Subject: [PATCH 03/62] ci: Update entrypoint.sh Signed-off-by: Jorge Marques --- entrypoint.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 6cb49596..aebc38d6 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,9 +1,12 @@ #!/bin/bash -runner_version=v1 github_token=$(cat /run/secrets/github_token 2> /dev/null) org_repository=$(cat /run/secrets/org_repository 2> /dev/null) +if [[ -z "$runner_labels" ]]; then + runner_labels="v1" +fi + source /usr/local/bin/github-api.sh if [[ -z $org_repository ]]; then @@ -35,10 +38,6 @@ function get_runner_token () { get_runner_token -if [[ ! -z $runner_labels ]]; then - runner_version+="-$runner_labels" -fi - name=$(echo $org_repository | sed 's|/|-|g')-$(echo $runner_token | sha3sum -a 256 | head -c4)-$(tr -dc A-Za-z0-9 Date: Wed, 26 Mar 2025 12:30:17 +0100 Subject: [PATCH 04/62] ci: Update entrypoint.sh Signed-off-by: Jorge Marques --- entrypoint.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index aebc38d6..b4423996 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -9,13 +9,13 @@ fi source /usr/local/bin/github-api.sh -if [[ -z $org_repository ]]; then +if [[ -z "$org_repository" ]]; then echo "No org_repository provided" exit 1 fi function get_runner_token () { - if [[ ! -z $github_token ]]; then + if [[ ! -z "$github_token" ]]; then runner_token=$( gh-actions-token $github_token \ $org_repository \ @@ -29,7 +29,7 @@ function get_runner_token () { else runner_token=$(cat /run/secrets/runner_token 2> /dev/null) - if [[ -z $runner_token ]]; then + if [[ -z "$runner_token" ]]; then echo "No runner_token or github_token provided" exit 1 fi From a296ca04f3d27a0cbe3363775e66b8936dea9972 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 27 Mar 2025 11:05:31 +0100 Subject: [PATCH 05/62] ci: Update entrypoint.sh Signed-off-by: Jorge Marques --- entrypoint.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index b4423996..0ccb3b42 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -38,7 +38,11 @@ function get_runner_token () { get_runner_token -name=$(echo $org_repository | sed 's|/|-|g')-$(echo $runner_token | sha3sum -a 256 | head -c4)-$(tr -dc A-Za-z0-9 Date: Fri, 28 Mar 2025 13:14:32 +0100 Subject: [PATCH 06/62] ci: abort ongoing git commands Signed-off-by: Jorge Marques --- checkout/action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index dd98124a..16ac4e9d 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -63,6 +63,10 @@ runs: git remote set-url origin "$url" fi + git am --abort 2>/dev/null || true + git rebase --abort 2>/dev/null || true + git cherry-pick --abort 2>/dev/null || true + git reset --hard git clean -xf . From 5bf5d1a036f7ed53f40c8b30db6a36b878c95a99 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 28 Mar 2025 14:07:12 +0100 Subject: [PATCH 07/62] checkout: Use cherry-pick instead of rebase Signed-off-by: Jorge Marques --- checkout/action.yml | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 16ac4e9d..9d152fe0 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -61,32 +61,37 @@ runs: git remote add origin "$url" else git remote set-url origin "$url" - fi - git am --abort 2>/dev/null || true - git rebase --abort 2>/dev/null || true - git cherry-pick --abort 2>/dev/null || true + git am --abort 2>/dev/null || true + git merge --abort 2>/dev/null || true + git rebase --abort 2>/dev/null || true + git cherry-pick --abort 2>/dev/null || true - git reset --hard - git clean -xf . + git reset --hard --quiet + git clean -xf --quiet . - if [[ ! "$(git rev-parse --abbrev-ref HEAD 2>/dev/null)" == "trunk" ]]; then - if [[ $(git rev-parse --verify "trunk" 2>/dev/null) ]]; then - git branch -D trunk + git checkout $(git rev-parse @) + echo "clean-up branches" + branches=$((git branch || true) | (grep -v ^'*' || true)) + if [[ "$branches" ]]; then + git branch -D $branches || true fi - git branch -m trunk + git branch -D trunk || true + git checkout -b trunk fi - name: Checkout shell: bash run: | git fetch origin --depth=$depth_sha $head_sha - git reset --hard $head_sha if [[ "${{ github.event.pull_request.head.sha }}" ]]; then git fetch origin --depth=1 ${{ github.base_ref }} - git rebase origin/${{ github.base_ref }} + git reset --hard origin/${{ github.base_ref }} + git cherry-pick --allow-empty --empty=keep $head_sha~$((depth_sha - 1))..$head_sha echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" + else + git reset --hard $head_sha fi echo "base_sha=$(git rev-parse @~$((depth_sha - 1)))" >> "$GITHUB_ENV" From 764a13c43a7f365c17ad8b3a2996cc2e5da1b59d Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 1 Apr 2025 09:42:00 +0200 Subject: [PATCH 08/62] ci: secret security Remove option to have github_token mounted, only runner_token. As enviroment variables, unset all prior calling GitHub Actions runner. Signed-off-by: Jorge Marques --- entrypoint.sh | 53 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 0ccb3b42..a8e2091a 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,35 +1,42 @@ #!/bin/bash -github_token=$(cat /run/secrets/github_token 2> /dev/null) -org_repository=$(cat /run/secrets/org_repository 2> /dev/null) - -if [[ -z "$runner_labels" ]]; then - runner_labels="v1" +github_token_=$github_token +runner_token_=$runner_token +org_repository_=$org_repository +runner_labels_=$runner_labels + +unset github_token +unset runner_token +unset org_repository +unset runner_labels + +if [[ -z "$runner_labels_" ]]; then + runner_labels_="v1" fi source /usr/local/bin/github-api.sh -if [[ -z "$org_repository" ]]; then +if [[ -z "$org_repository_" ]]; then echo "No org_repository provided" exit 1 fi function get_runner_token () { - if [[ ! -z "$github_token" ]]; then - runner_token=$( - gh-actions-token $github_token \ - $org_repository \ - runner_token + if [[ ! -z "$github_token_" ]]; then + runner_token_=$( + gh-actions-token $github_token_ \ + $org_repository_ ) - if [[ "$runner_token" == "null" ]]; then - echo "Failed to get '$org_repository' runner_token, check github_token permission" + if [[ "$runner_token_" == "null" ]]; then + echo "Failed to get '$org_repository_' runner_token, check github_token permission" exit 1 fi else - runner_token=$(cat /run/secrets/runner_token 2> /dev/null) - - if [[ -z "$runner_token" ]]; then + if [[ -f "/run/secrets/runner_token" ]]; then + runner_token_=$(cat /run/secrets/runner_token) + fi + if [[ -z "$runner_token_" ]]; then echo "No runner_token or github_token provided" exit 1 fi @@ -39,24 +46,26 @@ function get_runner_token () { get_runner_token if [[ -z "$name_label" ]]; then - name_label=$(echo $runner_token | sha3sum -a 256 | head -c4) + name_label=$(echo $runner_token_ | sha3sum -a 256 | head -c4) fi -name=$(echo $org_repository | sed 's|/|-|g')-$name_label +name=$(echo $org_repository_ | sed 's|/|-|g')-$name_label set -e /home/runner/actions-runner/config.sh \ - --url https://github.com/$org_repository \ - --token $runner_token \ - --labels "$runner_labels" \ + --url https://github.com/$org_repository_ \ + --token $runner_token_ \ + --labels "$runner_labels_" \ + --unattended \ + --replace \ --name $name function cleanup () { get_runner_token /home/runner/actions-runner/config.sh remove \ - --token $runner_token + --token $runner_token_ } trap 'cleanup; exit 130' INT From f3f48a9c32ba3818f5ea714247e856cbb1478d83 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 2 Apr 2025 11:07:14 +0200 Subject: [PATCH 09/62] checkout: add cache branches Signed-off-by: Jorge Marques --- checkout/action.yml | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 9d152fe0..e36066a3 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -14,9 +14,14 @@ inputs: gh-token: description: 'GitHub Token' default: ${{ github.token }} -#env: -# ACT_DEPTH: The depth when fetching, overwrites depth -# ACT_HEAD: The head to fetch +# env: +# ACT_DEPTH: The depth when fetching, overwrites depth +# ACT_HEAD: The head to fetch + +# behaviour: +# github.event.pull_request.head.sha is always used to detect the PR case. +# cache/ branches are created to avoid base refs from being garbage collected. +# on PRs, the patch commits are cherry-picked, not merged. runs: using: composite @@ -65,6 +70,7 @@ runs: git am --abort 2>/dev/null || true git merge --abort 2>/dev/null || true git rebase --abort 2>/dev/null || true + git revert --abort 2>/dev/null || true git cherry-pick --abort 2>/dev/null || true git reset --hard --quiet @@ -72,7 +78,7 @@ runs: git checkout $(git rev-parse @) echo "clean-up branches" - branches=$((git branch || true) | (grep -v ^'*' || true)) + branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' || true)) if [[ "$branches" ]]; then git branch -D $branches || true fi @@ -97,3 +103,15 @@ runs: echo "base_sha=$(git rev-parse @~$((depth_sha - 1)))" >> "$GITHUB_ENV" git log --oneline --reverse | head -200 + - name: Generate cache branches + shell: bash + run: | + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + cache_branch=${{ github.base_ref }} + cache_commit=$(git rev-parse origin/${{ github.base_ref }}) + else + cache_branch=${{ github.ref }} + cache_commit=$head_sha + fi + cache_branch=cache/$cache_branch + git branch $cache_branch $cache_commit -f From a61e6f2d2bfbab0759f64098d27ff66108fa5d71 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 2 Apr 2025 17:35:08 +0200 Subject: [PATCH 10/62] actions: checkout: infer depth for push event (no-force, no-new) Signed-off-by: Jorge Marques --- checkout/action.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index e36066a3..6c6219a6 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -35,6 +35,9 @@ runs: echo "depth_sha=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV elif [[ "${{ github.event.pull_request.commits }}" ]]; then echo "depth_sha=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]] && [[ ! "${{ github.event.forced }}" == "true" ]]; then + echo "github_safe_push=true" >> $GITHUB_ENV + echo "depth_sha=100" >> $GITHUB_ENV else echo "depth_sha=6" >> $GITHUB_ENV fi @@ -98,6 +101,11 @@ runs: echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" else git reset --hard $head_sha + + if [[ "$github_safe_push" == "true" ]]; then + depth_sha=$(git rev-list --count ${{ github.event.before }}..$head_sha) + echo "depth_sha=$depth_sha" >> "$GITHUB_ENV" + fi fi echo "base_sha=$(git rev-parse @~$((depth_sha - 1)))" >> "$GITHUB_ENV" From 51508581f70b6ed28e0397b0a3ede4efa49bba2b Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Sat, 5 Apr 2025 21:56:23 +0200 Subject: [PATCH 11/62] ci: add basic bashrc To simplify interactive shell testing Signed-off-by: Jorge Marques --- bashrc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 bashrc diff --git a/bashrc b/bashrc new file mode 100644 index 00000000..37ea8912 --- /dev/null +++ b/bashrc @@ -0,0 +1,33 @@ +#!/bin/bash + +[ -z "$PS1" ] || it=true + +if [[ "$it" == "true" ]]; then + err=0 + if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then + pushd $(git rev-parse --show-toplevel) &>/dev/null + if [[ -f "ci/build.sh" ]]; then + export head_sha=$(git rev-parse --short=20 @) + export base_sha=$(git rev-parse --short=20 @~6) + printf "Set git commit range as (\$base_sha..\$head_sha)\n" + printf "\e[34m$base_sha..$head_sha\e[0m.\n" + printf "Adjust variables range as needed.\n" + source ci/build.sh + printf "\nSourced methods from ci/build.sh:\n\e[34m" + grep -oE '^[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{' ci/build.sh | \ + awk -F'[ ()]+' '{print $1}' | paste -sd ' ' + printf "\e[0m" + else + printf "At a git repository, but ci/build.sh not found." + err=1 + fi + popd &>/dev/null + else + printf "Not a git repository." + err=1 + fi + + if [[ $err == "1" ]]; then + printf " Not sourcing ci/build.sh or setting git commit range variables\n" + fi +fi From 5858a994719bb61ae61a6c5e6bd54c6ccf07290e Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Sun, 6 Apr 2025 11:27:52 +0200 Subject: [PATCH 12/62] ci: add GITHUB_ENV fifo and custom echo %0A Log to console in magenta values written to $GITHUB_ENV and replace every %0A by a new line in interactive mode. Signed-off-by: Jorge Marques --- bashrc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bashrc b/bashrc index 37ea8912..9f380496 100644 --- a/bashrc +++ b/bashrc @@ -30,4 +30,29 @@ if [[ "$it" == "true" ]]; then if [[ $err == "1" ]]; then printf " Not sourcing ci/build.sh or setting git commit range variables\n" fi + + github_pipe_fifo=/tmp/github_env_$(tr -dc A-Za-z0-9 Date: Mon, 7 Apr 2025 10:04:42 +0200 Subject: [PATCH 13/62] ci: bashrc: enable own process group for gh listener Signed-off-by: Jorge Marques --- bashrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashrc b/bashrc index 9f380496..0ef8e2e4 100644 --- a/bashrc +++ b/bashrc @@ -48,7 +48,7 @@ if [[ "$it" == "true" ]]; then } mkfifo $github_pipe_fifo export GITHUB_ENV=$github_pipe_fifo - github_pipe_listener & + set -m ; github_pipe_listener & github_pipe_pid=$(command echo $!) trap github_pipe_exit_handler EXIT From d34017a063c59fc2eef0487ea3bb0173bd4ef9f4 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Mon, 7 Apr 2025 11:17:42 +0200 Subject: [PATCH 14/62] ci: checkout: fix masking last fallback under act Signed-off-by: Jorge Marques --- checkout/action.yml | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 6c6219a6..ca5ec2e5 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -26,20 +26,32 @@ inputs: runs: using: composite steps: - - name: Get depth + - name: Get commit depth shell: bash run: | - if [[ "${{ env.ACT_DEPTH }}" ]]; then - echo "depth_sha=$(( ${{ env.ACT_DEPTH }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ inputs.depth }}" ]]; then - echo "depth_sha=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ github.event.pull_request.commits }}" ]]; then - echo "depth_sha=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]] && [[ ! "${{ github.event.forced }}" == "true" ]]; then - echo "github_safe_push=true" >> $GITHUB_ENV - echo "depth_sha=100" >> $GITHUB_ENV + if [[ "${{ env.ACT }}" ]]; then + if [[ "${{ env.ACT_DEPTH }}" ]]; then + echo "setting depth_sha based on env.ACT_DEPTH" + echo "depth_sha=$(( ${{ env.ACT_DEPTH }} + 1 ))" >> $GITHUB_ENV + else + echo "setting depth_sha as 6 (fallback, consider setting ACT_DEPTH enviroment variable)" + echo "depth_sha=6" >> $GITHUB_ENV + fi else - echo "depth_sha=6" >> $GITHUB_ENV + if [[ "${{ inputs.depth }}" ]]; then + echo "setting depth_sha based on inputs.depth" + echo "depth_sha=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.pull_request.commits }}" ]]; then + echo "setting depth_sha based on github.event.pull_request.commits" + echo "depth_sha=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.before }}" ]] && [[ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + echo "setting depth_sha based on github.event.before" + echo "github_safe_push=true" >> $GITHUB_ENV + echo "depth_sha=100" >> $GITHUB_ENV + else + echo "setting depth_sha as 6 (fallback, either push new-branch or unexpected event type)" + echo "depth_sha=6" >> $GITHUB_ENV + fi fi if [[ "${{ env.ACT }}" ]]; then From b9912c5820f6d20962a773607dcf3196c56a9cfc Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 11 Apr 2025 14:20:07 +0200 Subject: [PATCH 15/62] checkout: check for --keep flag Signed-off-by: Jorge Marques --- checkout/action.yml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index ca5ec2e5..d3724886 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -97,7 +97,7 @@ runs: if [[ "$branches" ]]; then git branch -D $branches || true fi - git branch -D trunk || true + git branch -D trunk 2>/dev/null || true git checkout -b trunk fi @@ -109,7 +109,15 @@ runs: if [[ "${{ github.event.pull_request.head.sha }}" ]]; then git fetch origin --depth=1 ${{ github.base_ref }} git reset --hard origin/${{ github.base_ref }} - git cherry-pick --allow-empty --empty=keep $head_sha~$((depth_sha - 1))..$head_sha + # --empty was added on git 2.45.0 + has_keep=$(git cherry-pick 2>&1 | grep "\--empty" | wc -l) || true + echo $has_keep + if [[ "$has_keep" -gt "0" ]]; then + keep_empty="--empty=keep" + else + keep_empty="--keep-redundant-commits" + fi + git cherry-pick --allow-empty $keep_empty $head_sha~$((depth_sha - 1))..$head_sha echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" else git reset --hard $head_sha From 2f649a10685b67c0e890249a70bf4ccc49c94839 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 11 Apr 2025 17:49:15 +0200 Subject: [PATCH 16/62] bashrc: Source on non-interactive sessions Allows to $ podman-run adi/doctools:latest echo hello world! hello world! Requires sourced podman-run.sh companion script. Signed-off-by: Jorge Marques --- bashrc | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/bashrc b/bashrc index 0ef8e2e4..5fe325b7 100644 --- a/bashrc +++ b/bashrc @@ -2,35 +2,34 @@ [ -z "$PS1" ] || it=true -if [[ "$it" == "true" ]]; then - err=0 - if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then - pushd $(git rev-parse --show-toplevel) &>/dev/null - if [[ -f "ci/build.sh" ]]; then - export head_sha=$(git rev-parse --short=20 @) - export base_sha=$(git rev-parse --short=20 @~6) - printf "Set git commit range as (\$base_sha..\$head_sha)\n" - printf "\e[34m$base_sha..$head_sha\e[0m.\n" - printf "Adjust variables range as needed.\n" - source ci/build.sh +if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then + pushd $(git rev-parse --show-toplevel) &>/dev/null + if [[ -z "$head_sha" ]]; then + export head_sha=$(git rev-parse --short=20 @) + fi + if [[ -z "$base_sha" ]]; then + export base_sha=$(git rev-parse --short=20 @~6) + fi + if [[ "$it" == "true" ]]; then + printf "Set git commit range as (\$base_sha..\$head_sha)\n" + printf "\e[34m$base_sha..$head_sha\e[0m\n" + printf "Adjust variables range as needed.\n" + fi + if [[ -f "ci/build.sh" ]]; then + source ci/build.sh + if [[ "$it" == "true" ]]; then printf "\nSourced methods from ci/build.sh:\n\e[34m" grep -oE '^[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{' ci/build.sh | \ awk -F'[ ()]+' '{print $1}' | paste -sd ' ' printf "\e[0m" - else - printf "At a git repository, but ci/build.sh not found." - err=1 fi - popd &>/dev/null - else - printf "Not a git repository." - err=1 - fi - - if [[ $err == "1" ]]; then - printf " Not sourcing ci/build.sh or setting git commit range variables\n" fi + popd &>/dev/null +else + printf "Not a git repository, not setting git commit range variables or sourcing ci/build.sh\n" +fi +if [[ "$it" == "true" ]]; then github_pipe_fifo=/tmp/github_env_$(tr -dc A-Za-z0-9 Date: Fri, 11 Apr 2025 19:00:28 +0200 Subject: [PATCH 17/62] bashrc: graceful pipe exit, better pipe print Signed-off-by: Jorge Marques --- bashrc | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/bashrc b/bashrc index 5fe325b7..0684cd40 100644 --- a/bashrc +++ b/bashrc @@ -29,28 +29,31 @@ else printf "Not a git repository, not setting git commit range variables or sourcing ci/build.sh\n" fi -if [[ "$it" == "true" ]]; then - github_pipe_fifo=/tmp/github_env_$(tr -dc A-Za-z0-9 Date: Mon, 14 Apr 2025 13:49:39 +0200 Subject: [PATCH 18/62] checkout: set git pager as cat Signed-off-by: Jorge Marques --- checkout/action.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index d3724886..635acc81 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -78,8 +78,10 @@ runs: if [[ ! -d .git ]]; then git init --initial-branch=trunk . + git config core.pager cat git remote add origin "$url" else + git config core.pager cat git remote set-url origin "$url" git am --abort 2>/dev/null || true From b03b4529fbd5082ead82afdc96ca2b939e89fb09 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Mon, 14 Apr 2025 23:44:06 +0200 Subject: [PATCH 19/62] checkout: improve push event Signed-off-by: Jorge Marques --- checkout/action.yml | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 635acc81..7c49a6af 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -5,9 +5,6 @@ inputs: depth: description: The depth when fetching required: false - act: - description: Enable local run mode with act - required: false act-mountpoint: description: The source mountpoint default: '/mnt/repo' @@ -44,12 +41,13 @@ runs: elif [[ "${{ github.event.pull_request.commits }}" ]]; then echo "setting depth_sha based on github.event.pull_request.commits" echo "depth_sha=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ github.event.before }}" ]] && [[ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + elif && [[ ! "${{ github.event.before }}" ~= "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then echo "setting depth_sha based on github.event.before" - echo "github_safe_push=true" >> $GITHUB_ENV - echo "depth_sha=100" >> $GITHUB_ENV + commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; + depth_sha=$(( $(jq <<<"$commits_sha" length) + 1 )) ; + echo "depth_sha=$depth_sha" >> $GITHUB_ENV ; else - echo "setting depth_sha as 6 (fallback, either push new-branch or unexpected event type)" + echo "setting depth_sha as 6 (fallback, either force-push, push-new-branch, or unknown event type)" echo "depth_sha=6" >> $GITHUB_ENV fi fi @@ -123,11 +121,6 @@ runs: echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" else git reset --hard $head_sha - - if [[ "$github_safe_push" == "true" ]]; then - depth_sha=$(git rev-list --count ${{ github.event.before }}..$head_sha) - echo "depth_sha=$depth_sha" >> "$GITHUB_ENV" - fi fi echo "base_sha=$(git rev-parse @~$((depth_sha - 1)))" >> "$GITHUB_ENV" From 5fdbbc9a351e523b9513fbafb5b31b46b297d04d Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 15 Apr 2025 11:28:55 +0200 Subject: [PATCH 20/62] actions: checkout: rename depth_sha to fetch_depth Easier to understand implications. Signed-off-by: Jorge Marques --- checkout/action.yml | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 7c49a6af..93f8b97f 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -28,27 +28,28 @@ runs: run: | if [[ "${{ env.ACT }}" ]]; then if [[ "${{ env.ACT_DEPTH }}" ]]; then - echo "setting depth_sha based on env.ACT_DEPTH" - echo "depth_sha=$(( ${{ env.ACT_DEPTH }} + 1 ))" >> $GITHUB_ENV + echo "setting fetch_depth based on env.ACT_DEPTH" + echo "fetch_depth=$(( ${{ env.ACT_DEPTH }} + 1 ))" >> $GITHUB_ENV else - echo "setting depth_sha as 6 (fallback, consider setting ACT_DEPTH enviroment variable)" - echo "depth_sha=6" >> $GITHUB_ENV + echo "setting fetch_depth as 6 (fallback, consider setting ACT_DEPTH enviroment variable)" + echo "fetch_depth=6" >> $GITHUB_ENV fi else if [[ "${{ inputs.depth }}" ]]; then - echo "setting depth_sha based on inputs.depth" - echo "depth_sha=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV + echo "setting fetch_depth based on inputs.depth" + echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV elif [[ "${{ github.event.pull_request.commits }}" ]]; then - echo "setting depth_sha based on github.event.pull_request.commits" - echo "depth_sha=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV - elif && [[ ! "${{ github.event.before }}" ~= "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then - echo "setting depth_sha based on github.event.before" + echo "setting fetch_depth based on github.event.pull_request.commits" + echo "fetch_depth=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + echo "setting fetch_depth based on github.event.before" commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; - depth_sha=$(( $(jq <<<"$commits_sha" length) + 1 )) ; - echo "depth_sha=$depth_sha" >> $GITHUB_ENV ; + fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; + echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; else - echo "setting depth_sha as 6 (fallback, either force-push, push-new-branch, or unknown event type)" - echo "depth_sha=6" >> $GITHUB_ENV + echo "setting fetch_depth as 6 (fallback, either force-push, push-new-branch, or unknown event type)" + echo "fetch_depth=6" >> $GITHUB_ENV + echo "fetch_depth_fallback=true" >> $GITHUB_ENV fi fi @@ -104,7 +105,7 @@ runs: - name: Checkout shell: bash run: | - git fetch origin --depth=$depth_sha $head_sha + git fetch origin --depth=$fetch_depth $head_sha if [[ "${{ github.event.pull_request.head.sha }}" ]]; then git fetch origin --depth=1 ${{ github.base_ref }} @@ -117,13 +118,13 @@ runs: else keep_empty="--keep-redundant-commits" fi - git cherry-pick --allow-empty $keep_empty $head_sha~$((depth_sha - 1))..$head_sha + git cherry-pick --allow-empty $keep_empty $head_sha~$((fetch_depth - 1))..$head_sha echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" else git reset --hard $head_sha fi - echo "base_sha=$(git rev-parse @~$((depth_sha - 1)))" >> "$GITHUB_ENV" + echo "base_sha=$(git rev-parse @~$((fetch_depth - 1)))" >> "$GITHUB_ENV" git log --oneline --reverse | head -200 - name: Generate cache branches From b712949bc2263c38392b302ee533c4e7ba291820 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Mon, 14 Apr 2025 23:44:33 +0200 Subject: [PATCH 21/62] depth: add action Signed-off-by: Jorge Marques --- depth/README.md | 19 +++++++++++++++++++ depth/action.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 depth/README.md create mode 100644 depth/action.yml diff --git a/depth/README.md b/depth/README.md new file mode 100644 index 00000000..a89728ce --- /dev/null +++ b/depth/README.md @@ -0,0 +1,19 @@ +Depth +===== + +Sets the FETCH_DEPTH variable, to be passed to GitHub's checkout action. + +Usage: + +``` +on: workflow_call +jobs: + checks: + runs-on: [self-hosted, v1] + + steps: + - uses: analogdevicesinc/doctools/range@v1 + - uses: actions/checkout@v4 + with: + fetch-depth: ${{ env.FETCH_DEPTH }} +``` diff --git a/depth/action.yml b/depth/action.yml new file mode 100644 index 00000000..257c61df --- /dev/null +++ b/depth/action.yml @@ -0,0 +1,27 @@ +name: Git depth +description: Get fetch depth from GitHub context + +# behaviour: +# github.event.pull_request.head.sha is always used to detect the PR case. +# Fetch depth is number of commits + 1 (base commit) + +runs: + using: composite + steps: + - name: Get commit depth + shell: bash + run: | + if [[ "${{ github.event.pull_request.commits }}" ]]; then + echo "set FETCH_DEPTH based on github.event.pull_request.commits" + echo "FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + echo "set FETCH_DEPTH based on github.event.before" + commits_sha='${{ toJSON(github.event.commits.*.id) }}' + FETCH_DEPTH=$(( $(jq <<<"$commits_sha" length) + 1 )) + echo "FETCH_DEPTH=$FETCH_DEPTH" >> $GITHUB_ENV + else + echo "setting FETCH_DEPTH as 6 (fallback, either force-push, push-new-branch, or unknown event type)" + echo "FETCH_DEPTH=6" >> $GITHUB_ENV + echo "FETCH_DEPTH_FALLBACK=true" >> $GITHUB_ENV + fi + From 6bd1edd5a84f94e5aa771be654fc74e123302cc1 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 15 Apr 2025 11:51:41 +0200 Subject: [PATCH 22/62] checkout: if unset, set git user and email github-actions[bot] github-actions[bot]@users.noreply.github.com Signed-off-by: Jorge Marques --- checkout/action.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index 93f8b97f..9cc85715 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -102,6 +102,13 @@ runs: git checkout -b trunk fi + if [[ -z "$(git config user.name)" ]]; then + git config user.name "github-actions[bot]" + fi + if [[ -z "$(git config user.email)" ]]; then + git config user.email "github-actions[bot]@users.noreply.github.com" + fi + - name: Checkout shell: bash run: | From 472b3e5088bf014dddc3dec8da4d693f97dce60c Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 23 Apr 2025 14:32:49 +0200 Subject: [PATCH 23/62] depth: change behaviour, consider merge commit To be used with git diff --name-only @~$(( $FETCH_DEPTH - 1 ))..@ For both push and pull_request events. Signed-off-by: Jorge Marques --- depth/README.md | 3 +++ depth/action.yml | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/depth/README.md b/depth/README.md index a89728ce..743af362 100644 --- a/depth/README.md +++ b/depth/README.md @@ -2,6 +2,9 @@ Depth ===== Sets the FETCH_DEPTH variable, to be passed to GitHub's checkout action. +Meant to be used with GitHub default behaviour: +* pr: merge commit +* push: new commits Usage: diff --git a/depth/action.yml b/depth/action.yml index 257c61df..1c7cc1f0 100644 --- a/depth/action.yml +++ b/depth/action.yml @@ -3,7 +3,9 @@ description: Get fetch depth from GitHub context # behaviour: # github.event.pull_request.head.sha is always used to detect the PR case. -# Fetch depth is number of commits + 1 (base commit) +# Fetch depth is : +# * push: number of commits + 1 (base commit) +# * pull_request: merge commit + 1 (base commit) = 2 runs: using: composite @@ -11,9 +13,9 @@ runs: - name: Get commit depth shell: bash run: | - if [[ "${{ github.event.pull_request.commits }}" ]]; then - echo "set FETCH_DEPTH based on github.event.pull_request.commits" - echo "FETCH_DEPTH=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + echo "set FETCH_DEPTH based on github.event.pull_request.head.sha" + echo "FETCH_DEPTH=2" >> $GITHUB_ENV elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then echo "set FETCH_DEPTH based on github.event.before" commits_sha='${{ toJSON(github.event.commits.*.id) }}' From b94fd2738f506948a690bdc65a4b1108b4224834 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Mon, 5 May 2025 10:38:05 +0200 Subject: [PATCH 24/62] actions: checkout: unroll merge commit instead of cherry-pick For PRs, unroll/flatten the merge commit instead of cherry-picking. Allows minor simplification of the logic. * Use GITHUB_SHA for both pull_request (merge commits sha) and push. * Simply git rebase @~ and write new head_sha to environment Add number_commits environment variable, which should be used in most cases instead of fetch_depth. Signed-off-by: Jorge Marques --- checkout/README.md | 3 ++- checkout/action.yml | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/checkout/README.md b/checkout/README.md index aa3511c3..f3da3a0b 100644 --- a/checkout/README.md +++ b/checkout/README.md @@ -20,7 +20,8 @@ Key-points: * For pull-requests, rebase on base reference instead of awkward merge commit. * Current checkout branch is always called ``trunk`` -* Sets the ``depth_sha``, ``base_sha``, and ``head_sha`` to ease running checkscripts. +* Sets the ``fetch_depth``, ``base_sha``, ``head_sha`` and ``number_commits`` to + ease running checkscripts. When working locally, the user can set the following enviroment variables: diff --git a/checkout/action.yml b/checkout/action.yml index 9cc85715..246732c3 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -40,7 +40,7 @@ runs: echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV elif [[ "${{ github.event.pull_request.commits }}" ]]; then echo "setting fetch_depth based on github.event.pull_request.commits" - echo "fetch_depth=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> $GITHUB_ENV + echo "fetch_depth=$(( ${{ github.event.pull_request.commits }} + 2 ))" >> $GITHUB_ENV elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then echo "setting fetch_depth based on github.event.before" commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; @@ -60,10 +60,8 @@ runs: act_head_sha="@" fi echo "head_sha=$(cd ${{ inputs.act-mountpoint }} ; git rev-parse $act_head_sha)" >> $GITHUB_ENV - elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then - echo "head_sha=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV - elif [[ "${{ github.sha }}" ]]; then - echo "head_sha=${{ github.sha }}" >> $GITHUB_ENV + else + echo "head_sha=$GITHUB_SHA" >> $GITHUB_ENV fi - name: Prepare git @@ -114,32 +112,36 @@ runs: run: | git fetch origin --depth=$fetch_depth $head_sha + git reset --hard $head_sha if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - git fetch origin --depth=1 ${{ github.base_ref }} - git reset --hard origin/${{ github.base_ref }} - # --empty was added on git 2.45.0 - has_keep=$(git cherry-pick 2>&1 | grep "\--empty" | wc -l) || true + # --empty was added on git 2.26.0 + has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true echo $has_keep if [[ "$has_keep" -gt "0" ]]; then keep_empty="--empty=keep" else - keep_empty="--keep-redundant-commits" + keep_empty="--keep-empty" fi - git cherry-pick --allow-empty $keep_empty $head_sha~$((fetch_depth - 1))..$head_sha - echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" + git rebase $keep_empty @~ + number_commits=$((fetch_depth - 2)) else - git reset --hard $head_sha + number_commits=$((fetch_depth - 1)) fi + echo "number_commits=$number_commits" >> $GITHUB_ENV + echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" + echo "base_sha=$(git rev-parse @~$number_commits)" >> "$GITHUB_ENV" - echo "base_sha=$(git rev-parse @~$((fetch_depth - 1)))" >> "$GITHUB_ENV" - git log --oneline --reverse | head -200 + - name: Log commit range + shell: bash + run: | + git log --oneline --reverse $base_sha..$head_sha - name: Generate cache branches shell: bash run: | - if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + if [[ "${{ github.event.pull_request.base.sha }}" ]]; then cache_branch=${{ github.base_ref }} - cache_commit=$(git rev-parse origin/${{ github.base_ref }}) + cache_commit=${{ github.event.pull_request.base.sha }} else cache_branch=${{ github.ref }} cache_commit=$head_sha From e93ca5930fb818538280edcbb2c6b8b2861e7fb7 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 9 May 2025 12:57:28 +0200 Subject: [PATCH 25/62] actions: checkout: update fetch depth with the avail number For when the requested default number is greater than the number of commits. Signed-off-by: Jorge Marques --- checkout/action.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/checkout/action.yml b/checkout/action.yml index 246732c3..acf04c85 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -107,11 +107,18 @@ runs: git config user.email "github-actions[bot]@users.noreply.github.com" fi - - name: Checkout + - name: Fetch shell: bash run: | git fetch origin --depth=$fetch_depth $head_sha + avail_depth=$(git rev-list --count $head_sha) + if [[ $fetch_depth -gt $avail_depth ]]; then + echo "fetch_depth=$avail_depth" >> $GITHUB_ENV + fi + - name: Checkout + shell: bash + run: | git reset --hard $head_sha if [[ "${{ github.event.pull_request.head.sha }}" ]]; then # --empty was added on git 2.26.0 From 4a86c615454ff19476f9fdaa1d3640da6c8ead5b Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 9 May 2025 17:12:37 +0200 Subject: [PATCH 26/62] actions: checkout: log branches Signed-off-by: Jorge Marques --- checkout/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/checkout/action.yml b/checkout/action.yml index acf04c85..f491bb98 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -155,3 +155,4 @@ runs: fi cache_branch=cache/$cache_branch git branch $cache_branch $cache_commit -f + git --no-pager branch From ce55960c80e9b671a55b573246b45fffed7b636b Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 9 May 2025 17:50:32 +0200 Subject: [PATCH 27/62] actions: checkout: branch name, don't remove mirror Signed-off-by: Jorge Marques --- checkout/action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index f491bb98..6d1f57f3 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -92,7 +92,7 @@ runs: git checkout $(git rev-parse @) echo "clean-up branches" - branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' || true)) + branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' | grep -v ^'\s\+mirror/' || true)) if [[ "$branches" ]]; then git branch -D $branches || true fi @@ -150,7 +150,7 @@ runs: cache_branch=${{ github.base_ref }} cache_commit=${{ github.event.pull_request.base.sha }} else - cache_branch=${{ github.ref }} + cache_branch=${{ github.ref_name }} cache_commit=$head_sha fi cache_branch=cache/$cache_branch From 3b598f1bd0a1c6dba7494375b8ceb665225f4211 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Mon, 12 May 2025 17:10:03 +0200 Subject: [PATCH 28/62] actions: checkout: remove act hooks Too unfamiliar and hidden, little benefit. Signed-off-by: Jorge Marques --- checkout/README.md | 11 +------- checkout/action.yml | 63 ++++++++++++--------------------------------- 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/checkout/README.md b/checkout/README.md index f3da3a0b..d0b4e3a7 100644 --- a/checkout/README.md +++ b/checkout/README.md @@ -1,8 +1,7 @@ Checkout ======== -An alternative to https://github.com/actions/checkout with entrypoints for -https://github.com/nektos/act/ with https://github.com/containers/podman +An alternative to https://github.com/actions/checkout that flattens pr merge commits. Usage: @@ -23,11 +22,3 @@ Key-points: * Sets the ``fetch_depth``, ``base_sha``, ``head_sha`` and ``number_commits`` to ease running checkscripts. -When working locally, the user can set the following enviroment variables: - -* ACT_DEPTH: The depth when fetching, overwrites depth -* ACT_HEAD: The head to fetch - -At repository level, the owner shall change the local mountpoint from ``/mnt/repo`` to -something else by setting input ``act-mountpoint``. -A motivation maybe compatibility with a specific container, file structure. diff --git a/checkout/action.yml b/checkout/action.yml index 6d1f57f3..dce1a770 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -5,15 +5,9 @@ inputs: depth: description: The depth when fetching required: false - act-mountpoint: - description: The source mountpoint - default: '/mnt/repo' gh-token: description: 'GitHub Token' default: ${{ github.token }} -# env: -# ACT_DEPTH: The depth when fetching, overwrites depth -# ACT_HEAD: The head to fetch # behaviour: # github.event.pull_request.head.sha is always used to detect the PR case. @@ -26,52 +20,29 @@ runs: - name: Get commit depth shell: bash run: | - if [[ "${{ env.ACT }}" ]]; then - if [[ "${{ env.ACT_DEPTH }}" ]]; then - echo "setting fetch_depth based on env.ACT_DEPTH" - echo "fetch_depth=$(( ${{ env.ACT_DEPTH }} + 1 ))" >> $GITHUB_ENV - else - echo "setting fetch_depth as 6 (fallback, consider setting ACT_DEPTH enviroment variable)" - echo "fetch_depth=6" >> $GITHUB_ENV - fi + if [[ "${{ inputs.depth }}" ]]; then + echo "setting fetch_depth based on inputs.depth" + echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.pull_request.commits }}" ]]; then + echo "setting fetch_depth based on github.event.pull_request.commits" + echo "fetch_depth=$(( ${{ github.event.pull_request.commits }} + 2 ))" >> $GITHUB_ENV + elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + echo "setting fetch_depth based on github.event.before" + commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; + fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; + echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; else - if [[ "${{ inputs.depth }}" ]]; then - echo "setting fetch_depth based on inputs.depth" - echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ github.event.pull_request.commits }}" ]]; then - echo "setting fetch_depth based on github.event.pull_request.commits" - echo "fetch_depth=$(( ${{ github.event.pull_request.commits }} + 2 ))" >> $GITHUB_ENV - elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then - echo "setting fetch_depth based on github.event.before" - commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; - fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; - echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; - else - echo "setting fetch_depth as 6 (fallback, either force-push, push-new-branch, or unknown event type)" - echo "fetch_depth=6" >> $GITHUB_ENV - echo "fetch_depth_fallback=true" >> $GITHUB_ENV - fi + echo "setting fetch_depth as 6 (fallback, either force-push, push-new-branch, or unknown event type)" + echo "fetch_depth=6" >> $GITHUB_ENV + echo "fetch_depth_fallback=true" >> $GITHUB_ENV fi - if [[ "${{ env.ACT }}" ]]; then - if [[ "${{ env.ACT_HEAD }}" ]]; then - act_head_sha="${{ env.ACT_HEAD }}" - else - act_head_sha="@" - fi - echo "head_sha=$(cd ${{ inputs.act-mountpoint }} ; git rev-parse $act_head_sha)" >> $GITHUB_ENV - else - echo "head_sha=$GITHUB_SHA" >> $GITHUB_ENV - fi + echo "head_sha=$GITHUB_SHA" >> $GITHUB_ENV - name: Prepare git shell: bash run: | - if [[ "${{ env.ACT }}" ]]; then - url="file://${{ inputs.act-mountpoint }}" - else - url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" - fi + url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" if [[ ! -d .git ]]; then git init --initial-branch=trunk . @@ -90,7 +61,7 @@ runs: git reset --hard --quiet git clean -xf --quiet . - git checkout $(git rev-parse @) + git checkout $(git rev-parse @) || true echo "clean-up branches" branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' | grep -v ^'\s\+mirror/' || true)) if [[ "$branches" ]]; then From ecb4e52c35ccd9108db09e79dd15a13ea19fc007 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 13 May 2025 09:35:31 +0200 Subject: [PATCH 29/62] actions: checkout: better wording Signed-off-by: Jorge Marques --- checkout/action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index dce1a770..98d60933 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -12,7 +12,7 @@ inputs: # behaviour: # github.event.pull_request.head.sha is always used to detect the PR case. # cache/ branches are created to avoid base refs from being garbage collected. -# on PRs, the patch commits are cherry-picked, not merged. +# on PRs, the github automatic merge commit is flattened, allowing to check per commit. runs: using: composite @@ -32,7 +32,7 @@ runs: fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; else - echo "setting fetch_depth as 6 (fallback, either force-push, push-new-branch, or unknown event type)" + echo "setting fetch_depth as 6 (fallback, either due to force-push, new-branch, or uncaught condition)" echo "fetch_depth=6" >> $GITHUB_ENV echo "fetch_depth_fallback=true" >> $GITHUB_ENV fi From 6e4e6cc40578cb59a08fdd8ba55fe7568a5d0c51 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 13 May 2025 17:01:25 +0200 Subject: [PATCH 30/62] actions: checkout: reset on git .lock files Indicates a dirty exit of the previous workflow. Fully reset the repository. Signed-off-by: Jorge Marques --- checkout/action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index 98d60933..ea48a591 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -44,6 +44,10 @@ runs: run: | url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" + if [[ -f .git/shallow.lock ]] || [[ -f .git/index.lock ]]; then + rm -rf .* * + fi + if [[ ! -d .git ]]; then git init --initial-branch=trunk . git config core.pager cat From 7a05bbdb40a37850a02fd653685a8c50ddaa38f3 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 15 May 2025 09:50:08 +0200 Subject: [PATCH 31/62] actions: checkout: use GitHub API to get behind_by and ahead_by Signed-off-by: Jorge Marques --- checkout/action.yml | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index ea48a591..ab7f9b39 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -23,9 +23,26 @@ runs: if [[ "${{ inputs.depth }}" ]]; then echo "setting fetch_depth based on inputs.depth" echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ github.event.pull_request.commits }}" ]]; then - echo "setting fetch_depth based on github.event.pull_request.commits" - echo "fetch_depth=$(( ${{ github.event.pull_request.commits }} + 2 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then + echo "setting fetch_depth based on github.event.pull_request.head.sha" + json=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{ inputs.gh-token }}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${{ github.repository }}/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }}" \ + | jq '{behind_by, ahead_by}') + ahead_by=$(echo $json | jq '.ahead_by') + behind_by=$(echo $json | jq '.behind_by') + if [[ "$ahead_by" == "null" ]]; then + echo "GitHub API failed, setting based on github.event.pull_request.commits + 100" + ahead_by=${{ github.event.pull_request.commits }} + echo "ahead_by=$ahead_by" >> $GITHUB_ENV + fetch_depth=$(( ahead_by + 100 )) + else + echo "ahead_by=$ahead_by" >> $GITHUB_ENV + fetch_depth=$(( ahead_by + behind_by )) + fi + echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then echo "setting fetch_depth based on github.event.before" commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; @@ -105,13 +122,12 @@ runs: keep_empty="--keep-empty" fi git rebase $keep_empty @~ - number_commits=$((fetch_depth - 2)) else - number_commits=$((fetch_depth - 1)) + ahead_by=$((fetch_depth - 1)) fi - echo "number_commits=$number_commits" >> $GITHUB_ENV + echo "ahead_by=$ahead_by" >> $GITHUB_ENV echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" - echo "base_sha=$(git rev-parse @~$number_commits)" >> "$GITHUB_ENV" + echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" - name: Log commit range shell: bash From a14d6ff950b97ff1f12544b2eda7da775a315d4e Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 15 May 2025 13:02:09 +0200 Subject: [PATCH 32/62] actions: checkout: run prepare git first Signed-off-by: Jorge Marques --- checkout/action.yml | 78 ++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index ab7f9b39..97e8a1b3 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -17,45 +17,6 @@ inputs: runs: using: composite steps: - - name: Get commit depth - shell: bash - run: | - if [[ "${{ inputs.depth }}" ]]; then - echo "setting fetch_depth based on inputs.depth" - echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV - elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then - echo "setting fetch_depth based on github.event.pull_request.head.sha" - json=$(curl -s -L \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer ${{ inputs.gh-token }}" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/${{ github.repository }}/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }}" \ - | jq '{behind_by, ahead_by}') - ahead_by=$(echo $json | jq '.ahead_by') - behind_by=$(echo $json | jq '.behind_by') - if [[ "$ahead_by" == "null" ]]; then - echo "GitHub API failed, setting based on github.event.pull_request.commits + 100" - ahead_by=${{ github.event.pull_request.commits }} - echo "ahead_by=$ahead_by" >> $GITHUB_ENV - fetch_depth=$(( ahead_by + 100 )) - else - echo "ahead_by=$ahead_by" >> $GITHUB_ENV - fetch_depth=$(( ahead_by + behind_by )) - fi - echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV - elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then - echo "setting fetch_depth based on github.event.before" - commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; - fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; - echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; - else - echo "setting fetch_depth as 6 (fallback, either due to force-push, new-branch, or uncaught condition)" - echo "fetch_depth=6" >> $GITHUB_ENV - echo "fetch_depth_fallback=true" >> $GITHUB_ENV - fi - - echo "head_sha=$GITHUB_SHA" >> $GITHUB_ENV - - name: Prepare git shell: bash run: | @@ -99,6 +60,45 @@ runs: git config user.email "github-actions[bot]@users.noreply.github.com" fi + - name: Get commit depth + shell: bash + run: | + if [[ "${{ inputs.depth }}" ]]; then + echo "setting fetch_depth based on inputs.depth" + echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV + elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then + echo "setting fetch_depth based on github.event.pull_request.head.sha" + json=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{ inputs.gh-token }}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${{ github.repository }}/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }}" \ + | jq '{behind_by, ahead_by}') + ahead_by=$(echo $json | jq '.ahead_by') + behind_by=$(echo $json | jq '.behind_by') + if [[ "$ahead_by" == "null" ]]; then + echo "GitHub API failed, setting based on github.event.pull_request.commits + 100" + ahead_by=${{ github.event.pull_request.commits }} + echo "ahead_by=$ahead_by" >> $GITHUB_ENV + fetch_depth=$(( ahead_by + 100 )) + else + echo "ahead_by=$ahead_by" >> $GITHUB_ENV + fetch_depth=$(( ahead_by + behind_by )) + fi + echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV + elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + echo "setting fetch_depth based on github.event.before" + commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; + fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; + echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; + else + echo "setting fetch_depth as 6 (fallback, either due to force-push, new-branch, or uncaught condition)" + echo "fetch_depth=6" >> $GITHUB_ENV + echo "fetch_depth_fallback=true" >> $GITHUB_ENV + fi + + echo "head_sha=$GITHUB_SHA" >> $GITHUB_ENV + - name: Fetch shell: bash run: | From bd8dd9ed5276b52b0b140a5049d43bb506b7125a Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 15 May 2025 13:59:55 +0200 Subject: [PATCH 33/62] actions: checkout: add support to checking out a different branch Signed-off-by: Jorge Marques --- checkout/README.md | 11 ++++++++--- checkout/action.yml | 40 +++++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/checkout/README.md b/checkout/README.md index d0b4e3a7..d07f5a26 100644 --- a/checkout/README.md +++ b/checkout/README.md @@ -15,10 +15,15 @@ jobs: - uses: analogdevicesinc/doctools/checkout@v1 ``` +Inputs: +* branch: checkout another branch, in this case the variables assume the values: + - `fetch_depth`: 1. + - `base_sha`, `head_sha` and `ahead_by`: not set + Key-points: * For pull-requests, rebase on base reference instead of awkward merge commit. -* Current checkout branch is always called ``trunk`` -* Sets the ``fetch_depth``, ``base_sha``, ``head_sha`` and ``number_commits`` to - ease running checkscripts. +* Current checkout branch is always called `trunk` +* Sets the `fetch_depth`, `base_sha`, `head_sha` and `ahead_by` to + ease running check scripts. diff --git a/checkout/action.yml b/checkout/action.yml index 97e8a1b3..58d7e916 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -2,11 +2,11 @@ name: Git Checkout description: A git checkout for local runners and runs inputs: - depth: - description: The depth when fetching - required: false + branch: + description: "Branch to use (don't use context)" + default: '' gh-token: - description: 'GitHub Token' + description: "GitHub Token" default: ${{ github.token }} # behaviour: @@ -63,9 +63,9 @@ runs: - name: Get commit depth shell: bash run: | - if [[ "${{ inputs.depth }}" ]]; then - echo "setting fetch_depth based on inputs.depth" - echo "fetch_depth=$(( ${{ inputs.depth }} + 1 ))" >> $GITHUB_ENV + if [[ "${{ inputs.branch }}" ]]; then + echo "setting fetch_depth based on inputs.branch" + echo "fetch_depth=1" >> $GITHUB_ENV elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then echo "setting fetch_depth based on github.event.pull_request.head.sha" json=$(curl -s -L \ @@ -97,21 +97,24 @@ runs: echo "fetch_depth_fallback=true" >> $GITHUB_ENV fi - echo "head_sha=$GITHUB_SHA" >> $GITHUB_ENV + - name: Fetch & checkout (branch) + shell: bash + if: ${{ inputs.branch != '' }} + run: | + git fetch origin --depth=$fetch_depth "${{ inputs.branch }}" + git reset --hard origin/${{ inputs.branch }} - - name: Fetch + - name: Fetch & checkout (context) shell: bash + if: ${{ inputs.branch == '' }} run: | - git fetch origin --depth=$fetch_depth $head_sha - avail_depth=$(git rev-list --count $head_sha) + git fetch origin --depth=$fetch_depth $GITHUB_SHA + avail_depth=$(git rev-list --count $GITHUB_SHA) if [[ $fetch_depth -gt $avail_depth ]]; then echo "fetch_depth=$avail_depth" >> $GITHUB_ENV fi - - name: Checkout - shell: bash - run: | - git reset --hard $head_sha + git reset --hard $GITHUB_SHA if [[ "${{ github.event.pull_request.head.sha }}" ]]; then # --empty was added on git 2.26.0 has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true @@ -124,18 +127,20 @@ runs: git rebase $keep_empty @~ else ahead_by=$((fetch_depth - 1)) + echo "ahead_by=$ahead_by" >> $GITHUB_ENV fi - echo "ahead_by=$ahead_by" >> $GITHUB_ENV echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" - name: Log commit range shell: bash + if: ${{ inputs.branch == '' }} run: | git log --oneline --reverse $base_sha..$head_sha - - name: Generate cache branches + - name: Generate cache branches & rename shell: bash + if: ${{ inputs.branch == '' }} run: | if [[ "${{ github.event.pull_request.base.sha }}" ]]; then cache_branch=${{ github.base_ref }} @@ -146,4 +151,5 @@ runs: fi cache_branch=cache/$cache_branch git branch $cache_branch $cache_commit -f + git branch -m $cache_branch || true git --no-pager branch From 3120c9fffae780068a5a660e61a6ce075a3141aa Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 15 May 2025 14:42:15 +0200 Subject: [PATCH 34/62] actions: checkout: remove checkout other branch Signed-off-by: Jorge Marques --- checkout/README.md | 5 ----- checkout/action.yml | 20 ++------------------ 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/checkout/README.md b/checkout/README.md index d07f5a26..c450de55 100644 --- a/checkout/README.md +++ b/checkout/README.md @@ -15,11 +15,6 @@ jobs: - uses: analogdevicesinc/doctools/checkout@v1 ``` -Inputs: -* branch: checkout another branch, in this case the variables assume the values: - - `fetch_depth`: 1. - - `base_sha`, `head_sha` and `ahead_by`: not set - Key-points: * For pull-requests, rebase on base reference instead of awkward merge commit. diff --git a/checkout/action.yml b/checkout/action.yml index 58d7e916..9b3a9d3f 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -2,9 +2,6 @@ name: Git Checkout description: A git checkout for local runners and runs inputs: - branch: - description: "Branch to use (don't use context)" - default: '' gh-token: description: "GitHub Token" default: ${{ github.token }} @@ -63,10 +60,7 @@ runs: - name: Get commit depth shell: bash run: | - if [[ "${{ inputs.branch }}" ]]; then - echo "setting fetch_depth based on inputs.branch" - echo "fetch_depth=1" >> $GITHUB_ENV - elif [[ "${{ github.event.pull_request.head.sha }}" ]]; then + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then echo "setting fetch_depth based on github.event.pull_request.head.sha" json=$(curl -s -L \ -H "Accept: application/vnd.github+json" \ @@ -97,16 +91,8 @@ runs: echo "fetch_depth_fallback=true" >> $GITHUB_ENV fi - - name: Fetch & checkout (branch) - shell: bash - if: ${{ inputs.branch != '' }} - run: | - git fetch origin --depth=$fetch_depth "${{ inputs.branch }}" - git reset --hard origin/${{ inputs.branch }} - - - name: Fetch & checkout (context) + - name: Fetch & checkout shell: bash - if: ${{ inputs.branch == '' }} run: | git fetch origin --depth=$fetch_depth $GITHUB_SHA avail_depth=$(git rev-list --count $GITHUB_SHA) @@ -134,13 +120,11 @@ runs: - name: Log commit range shell: bash - if: ${{ inputs.branch == '' }} run: | git log --oneline --reverse $base_sha..$head_sha - name: Generate cache branches & rename shell: bash - if: ${{ inputs.branch == '' }} run: | if [[ "${{ github.event.pull_request.base.sha }}" ]]; then cache_branch=${{ github.base_ref }} From 8503400d88e626a3bff738e9c95b2da5ce086853 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 16 May 2025 22:44:52 +0200 Subject: [PATCH 35/62] actions: checkout: increase fetch depth by two It is necessary to have the base commit fetched also. Signed-off-by: Jorge Marques --- checkout/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checkout/action.yml b/checkout/action.yml index 9b3a9d3f..2599c7f9 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -77,7 +77,7 @@ runs: fetch_depth=$(( ahead_by + 100 )) else echo "ahead_by=$ahead_by" >> $GITHUB_ENV - fetch_depth=$(( ahead_by + behind_by )) + fetch_depth=$(( ahead_by + behind_by + 2 )) fi echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then From b10127678aa40f87d09cde9d5e44a92fdc39b92b Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 20 May 2025 18:24:21 +0200 Subject: [PATCH 36/62] actions: checkout: fixup rm on lock Signed-off-by: Jorge Marques --- checkout/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checkout/action.yml b/checkout/action.yml index 2599c7f9..55e3cb53 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -20,7 +20,7 @@ runs: url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" if [[ -f .git/shallow.lock ]] || [[ -f .git/index.lock ]]; then - rm -rf .* * + rm -rf -- ..?* .[!.]* * fi if [[ ! -d .git ]]; then From 60e68178142ee154a71204d55ad06a78a7f5654e Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 27 May 2025 09:41:14 +0200 Subject: [PATCH 37/62] actions: checkout: reset bisect and remove untracked directories Signed-off-by: Jorge Marques --- checkout/action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/checkout/action.yml b/checkout/action.yml index 55e3cb53..a4a378b7 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -36,9 +36,10 @@ runs: git rebase --abort 2>/dev/null || true git revert --abort 2>/dev/null || true git cherry-pick --abort 2>/dev/null || true + git bisect reset 2>/dev/null || true git reset --hard --quiet - git clean -xf --quiet . + git clean -ffdx --quiet . git checkout $(git rev-parse @) || true echo "clean-up branches" From 7442410f205f3e4aa4c83af0024f5c09185abb24 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 27 May 2025 09:41:33 +0200 Subject: [PATCH 38/62] actions: checkout: fix branch rename and suppress warning The action tries to use the original pr/branch name instead of trunk, and then logs all branches cached. Signed-off-by: Jorge Marques --- checkout/action.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index a4a378b7..d24b689e 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -128,13 +128,12 @@ runs: shell: bash run: | if [[ "${{ github.event.pull_request.base.sha }}" ]]; then - cache_branch=${{ github.base_ref }} - cache_commit=${{ github.event.pull_request.base.sha }} + branch=${{ github.base_ref }} + commit=${{ github.event.pull_request.base.sha }} else - cache_branch=${{ github.ref_name }} - cache_commit=$head_sha + branch=${{ github.ref_name }} + commit=$head_sha fi - cache_branch=cache/$cache_branch - git branch $cache_branch $cache_commit -f - git branch -m $cache_branch || true + git branch cache/$branch $commit -f + git branch -m $branch 2>/dev/null || true git --no-pager branch From 6dfebf2ea406ce52dd0056b020e2931e1d0af0fc Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 10 Jun 2025 10:08:39 +0200 Subject: [PATCH 39/62] sync bashrc, entrypoint.sh For the exact motive I forgot to sync between main and here, we should curl from main instead, and these files removed Signed-off-by: Jorge Marques --- bashrc | 4 ++-- entrypoint.sh | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/bashrc b/bashrc index 0684cd40..2ee431b8 100644 --- a/bashrc +++ b/bashrc @@ -5,10 +5,10 @@ if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then pushd $(git rev-parse --show-toplevel) &>/dev/null if [[ -z "$head_sha" ]]; then - export head_sha=$(git rev-parse --short=20 @) + export head_sha=@ fi if [[ -z "$base_sha" ]]; then - export base_sha=$(git rev-parse --short=20 @~6) + export base_sha=@~6 fi if [[ "$it" == "true" ]]; then printf "Set git commit range as (\$base_sha..\$head_sha)\n" diff --git a/entrypoint.sh b/entrypoint.sh index a8e2091a..5d963ce0 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -4,16 +4,24 @@ github_token_=$github_token runner_token_=$runner_token org_repository_=$org_repository runner_labels_=$runner_labels +config_flags_=$config_flags +name_label_=$name_label unset github_token unset runner_token unset org_repository unset runner_labels +unset config_flags +unset name_label if [[ -z "$runner_labels_" ]]; then runner_labels_="v1" fi +if [[ -z "$config_flags_" ]]; then + config_flags_="--replace" +fi + source /usr/local/bin/github-api.sh if [[ -z "$org_repository_" ]]; then @@ -33,8 +41,8 @@ function get_runner_token () { exit 1 fi else - if [[ -f "/run/secrets/runner_token" ]]; then - runner_token_=$(cat /run/secrets/runner_token) + if [[ -p "/var/run/secrets/runner_token" ]] || [[ -f "/var/run/secrets/runner_token" ]]; then + runner_token_=$(cat /var/run/secrets/runner_token) fi if [[ -z "$runner_token_" ]]; then echo "No runner_token or github_token provided" @@ -45,27 +53,28 @@ function get_runner_token () { get_runner_token -if [[ -z "$name_label" ]]; then - name_label=$(echo $runner_token_ | sha3sum -a 256 | head -c4) +if [[ -z "$name_label_" ]]; then + name_label_=$(echo $runner_token_ | sha3sum -a 256 | head -c4) fi -name=$(echo $org_repository_ | sed 's|/|-|g')-$name_label +name=$(echo $org_repository_ | sed 's|/|-|g')-$name_label_ set -e -/home/runner/actions-runner/config.sh \ +(cd /home/runner/actions-runner ; ./config.sh \ --url https://github.com/$org_repository_ \ --token $runner_token_ \ --labels "$runner_labels_" \ --unattended \ - --replace \ - --name $name + --name $name \ + $config_flags_ \ +) function cleanup () { get_runner_token - /home/runner/actions-runner/config.sh remove \ - --token $runner_token_ + (cd /home/runner/actions-runner ; ./config.sh remove \ + --token $runner_token_) } trap 'cleanup; exit 130' INT From 864ea536f97ed65676fd8e7b7035f475a45b2bba Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 10 Jun 2025 10:16:33 +0200 Subject: [PATCH 40/62] actions: suggest action branch instead of v1 Signed-off-by: Jorge Marques --- README.md | 4 ++-- checkout/README.md | 2 +- depth/README.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ac14e2f9..bdb05dee 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Shared composite GitHub Actions =============================== Contains reusable and public GitHub Actions focused on performance. -This is the v1 release branch. +This is the action release branch. Use as follows: @@ -13,5 +13,5 @@ jobs: runs-on: [self-hosted, v1] steps: - - uses: analogdevices/doctools/checkout@v1 + - uses: analogdevices/doctools/checkout@action ``` diff --git a/checkout/README.md b/checkout/README.md index c450de55..6bdf7523 100644 --- a/checkout/README.md +++ b/checkout/README.md @@ -12,7 +12,7 @@ jobs: runs-on: [self-hosted, v1] steps: - - uses: analogdevicesinc/doctools/checkout@v1 + - uses: analogdevicesinc/doctools/checkout@action ``` Key-points: diff --git a/depth/README.md b/depth/README.md index 743af362..250736da 100644 --- a/depth/README.md +++ b/depth/README.md @@ -2,7 +2,7 @@ Depth ===== Sets the FETCH_DEPTH variable, to be passed to GitHub's checkout action. -Meant to be used with GitHub default behaviour: +Meant to be used with GitHub default behavior: * pr: merge commit * push: new commits @@ -15,7 +15,7 @@ jobs: runs-on: [self-hosted, v1] steps: - - uses: analogdevicesinc/doctools/range@v1 + - uses: analogdevicesinc/doctools/range@action - uses: actions/checkout@v4 with: fetch-depth: ${{ env.FETCH_DEPTH }} From c4f9fb3c3c63d283ea5460b47bd21eb800d2c743 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 11 Jun 2025 09:27:38 +0200 Subject: [PATCH 41/62] actions: checkout: use switch instead of checkout Better options and state machine for force overwrite, detach head. Signed-off-by: Jorge Marques --- checkout/action.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index d24b689e..997852f4 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -41,14 +41,13 @@ runs: git reset --hard --quiet git clean -ffdx --quiet . - git checkout $(git rev-parse @) || true + git switch -d @ echo "clean-up branches" branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' | grep -v ^'\s\+mirror/' || true)) if [[ "$branches" ]]; then git branch -D $branches || true fi - git branch -D trunk 2>/dev/null || true - git checkout -b trunk + git switch -C trunk fi if [[ -z "$(git config user.name)" ]]; then @@ -92,7 +91,7 @@ runs: echo "fetch_depth_fallback=true" >> $GITHUB_ENV fi - - name: Fetch & checkout + - name: Fetch & reset shell: bash run: | git fetch origin --depth=$fetch_depth $GITHUB_SHA From 5430a301aaecdeab522f4a80511f4e4f0619c090 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 4 Jul 2025 14:25:38 +0200 Subject: [PATCH 42/62] actions: checkout: deepen by fetch depth if rebase fails A massive 400 commits will retry incrementing depth by 400, while a simpler pr will increment by 2, until succeeds. So the level of effort is proportional to the changes. Signed-off-by: Jorge Marques --- checkout/action.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 997852f4..f6ce884c 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -95,11 +95,6 @@ runs: shell: bash run: | git fetch origin --depth=$fetch_depth $GITHUB_SHA - avail_depth=$(git rev-list --count $GITHUB_SHA) - if [[ $fetch_depth -gt $avail_depth ]]; then - echo "fetch_depth=$avail_depth" >> $GITHUB_ENV - fi - git reset --hard $GITHUB_SHA if [[ "${{ github.event.pull_request.head.sha }}" ]]; then # --empty was added on git 2.26.0 @@ -110,13 +105,17 @@ runs: else keep_empty="--keep-empty" fi - git rebase $keep_empty @~ + while ! git rebase $keep_empty @~; do + git rebase --abort + git fetch origin --deepen=$fetch_depth $GITHUB_SHA + done else ahead_by=$((fetch_depth - 1)) echo "ahead_by=$ahead_by" >> $GITHUB_ENV fi echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" + echo "fetch_depth=$(git rev-list --count $GITHUB_SHA)" >> "$GITHUB_ENV" - name: Log commit range shell: bash From 8771d19acfa6042416ca9517812c3ede0f59d6b1 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Mon, 28 Jul 2025 18:39:53 +0200 Subject: [PATCH 43/62] actions: checkout: grab no-ref For: fatal: You are on a branch yet to be born Signed-off-by: Jorge Marques --- checkout/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checkout/action.yml b/checkout/action.yml index f6ce884c..f84677f2 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -41,7 +41,7 @@ runs: git reset --hard --quiet git clean -ffdx --quiet . - git switch -d @ + git switch -d || true echo "clean-up branches" branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' | grep -v ^'\s\+mirror/' || true)) if [[ "$branches" ]]; then From 94d2655051a7e5646e3ad8b2bb25f04af8da21e8 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 29 Jul 2025 09:42:08 +0200 Subject: [PATCH 44/62] actions: checkout: fallback to branch head Fallback to branch head after 10 rebases fails. Signed-off-by: Jorge Marques --- checkout/action.yml | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index f84677f2..f5541f7b 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -77,6 +77,7 @@ runs: fetch_depth=$(( ahead_by + 100 )) else echo "ahead_by=$ahead_by" >> $GITHUB_ENV + echo "behind_by=$behind_by" >> $GITHUB_ENV fetch_depth=$(( ahead_by + behind_by + 2 )) fi echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV @@ -96,6 +97,7 @@ runs: run: | git fetch origin --depth=$fetch_depth $GITHUB_SHA git reset --hard $GITHUB_SHA + retry=10 if [[ "${{ github.event.pull_request.head.sha }}" ]]; then # --empty was added on git 2.26.0 has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true @@ -105,17 +107,31 @@ runs: else keep_empty="--keep-empty" fi - while ! git rebase $keep_empty @~; do + while ! git rebase $keep_empty --rebase-merges @~; do git rebase --abort + if [[ "$retry" -eq 0 ]]; then + break + fi + echo "deepening by $fetch_depth commits" git fetch origin --deepen=$fetch_depth $GITHUB_SHA + retry=$((retry - 1)) done + if [[ "$retry" -eq 0 ]]; then + echo "::warning::Failed to rebase, using source branch instead" + git reset --hard ${{ github.event.pull_request.head.sha }} + echo "rebase_fail=true" >> $GITHUB_ENV + fi else ahead_by=$((fetch_depth - 1)) echo "ahead_by=$ahead_by" >> $GITHUB_ENV fi echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" - echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" - echo "fetch_depth=$(git rev-list --count $GITHUB_SHA)" >> "$GITHUB_ENV" + if [[ "$retry" -eq 0 ]]; then + echo "base_sha=$(git rev-list --first-parent HEAD | tail -n 1)" >> "$GITHUB_ENV" + else + echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" + fi + echo "fetch_depth=$(git rev-list --count --first-parent $GITHUB_SHA)" >> "$GITHUB_ENV" - name: Log commit range shell: bash From 29d70650818e027876cf423fb83d71538d2ccdd5 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 29 Jul 2025 11:23:39 +0200 Subject: [PATCH 45/62] actions: checkout: Don't rebase on behind_by 0, trust --rebase-merges Signed-off-by: Jorge Marques --- checkout/action.yml | 50 +++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index f5541f7b..618ad30e 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -73,13 +73,12 @@ runs: if [[ "$ahead_by" == "null" ]]; then echo "GitHub API failed, setting based on github.event.pull_request.commits + 100" ahead_by=${{ github.event.pull_request.commits }} - echo "ahead_by=$ahead_by" >> $GITHUB_ENV fetch_depth=$(( ahead_by + 100 )) else - echo "ahead_by=$ahead_by" >> $GITHUB_ENV - echo "behind_by=$behind_by" >> $GITHUB_ENV fetch_depth=$(( ahead_by + behind_by + 2 )) fi + echo "ahead_by=$ahead_by" >> $GITHUB_ENV + echo "behind_by=$behind_by" >> $GITHUB_ENV echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then echo "setting fetch_depth based on github.event.before" @@ -95,39 +94,36 @@ runs: - name: Fetch & reset shell: bash run: | - git fetch origin --depth=$fetch_depth $GITHUB_SHA - git reset --hard $GITHUB_SHA - retry=10 if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - # --empty was added on git 2.26.0 - has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true - echo $has_keep - if [[ "$has_keep" -gt "0" ]]; then - keep_empty="--empty=keep" + if [[ "$behind_by" -eq 0 ]]; then + git fetch origin --depth=$fetch_depth ${{ github.event.pull_request.head.sha }} + git reset --hard ${{ github.event.pull_request.head.sha }} else - keep_empty="--keep-empty" - fi - while ! git rebase $keep_empty --rebase-merges @~; do - git rebase --abort - if [[ "$retry" -eq 0 ]]; then - break + git fetch origin --depth=$fetch_depth $GITHUB_SHA + git reset --hard $GITHUB_SHA + # --empty was added on git 2.26.0 + has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true + echo $has_keep + if [[ "$has_keep" -gt "0" ]]; then + keep_empty="--empty=keep" + else + keep_empty="--keep-empty" fi - echo "deepening by $fetch_depth commits" - git fetch origin --deepen=$fetch_depth $GITHUB_SHA - retry=$((retry - 1)) - done - if [[ "$retry" -eq 0 ]]; then - echo "::warning::Failed to rebase, using source branch instead" - git reset --hard ${{ github.event.pull_request.head.sha }} - echo "rebase_fail=true" >> $GITHUB_ENV + while ! git rebase $keep_empty --rebase-merges @~; do + git rebase --abort + echo "deepening by $fetch_depth commits" + git fetch origin --deepen=$fetch_depth $GITHUB_SHA + done fi else + git fetch origin --depth=$fetch_depth $GITHUB_SHA + git reset --hard $GITHUB_SHA ahead_by=$((fetch_depth - 1)) echo "ahead_by=$ahead_by" >> $GITHUB_ENV fi echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" - if [[ "$retry" -eq 0 ]]; then - echo "base_sha=$(git rev-list --first-parent HEAD | tail -n 1)" >> "$GITHUB_ENV" + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + echo "base_sha=${{ github.event.pull_request.base.sha }}" >> "$GITHUB_ENV" else echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" fi From 4e5e43c23d6960a69f5e125cfdb5aa73d655e18c Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 29 Jul 2025 15:14:07 +0200 Subject: [PATCH 46/62] bashrc: hide _ suffixed methods Signed-off-by: Jorge Marques --- bashrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashrc b/bashrc index 2ee431b8..01f1250f 100644 --- a/bashrc +++ b/bashrc @@ -19,7 +19,7 @@ if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then source ci/build.sh if [[ "$it" == "true" ]]; then printf "\nSourced methods from ci/build.sh:\n\e[34m" - grep -oE '^[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{' ci/build.sh | \ + grep -oE '^[a-zA-Z][a-zA-Z0-9_]*\s*\(\)\s*\{' ci/build.sh | \ awk -F'[ ()]+' '{print $1}' | paste -sd ' ' printf "\e[0m" fi From aedec2ecadf1bdd7ba9408ea61f5d9bb2ab286d1 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 28 Aug 2025 19:01:06 +0200 Subject: [PATCH 47/62] blank: add action Signed-off-by: Jorge Marques --- blank/README.md | 16 +++++++++++++ blank/action.yml | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 blank/README.md create mode 100644 blank/action.yml diff --git a/blank/README.md b/blank/README.md new file mode 100644 index 00000000..74a42698 --- /dev/null +++ b/blank/README.md @@ -0,0 +1,16 @@ +Blank +===== + +An alternative to https://github.com/actions/checkout that leaves checkout in an orphan branch without commits. + +Usage: + +``` +on: workflow_call +jobs: + checks: + runs-on: [self-hosted, v1] + + steps: + - uses: analogdevicesinc/doctools/blank@action +``` diff --git a/blank/action.yml b/blank/action.yml new file mode 100644 index 00000000..a3426565 --- /dev/null +++ b/blank/action.yml @@ -0,0 +1,58 @@ +name: Git Blank +description: A git init for local runners + +inputs: + gh-token: + description: "GitHub Token" + default: ${{ github.token }} + +# behaviour: +# just inits the github repo, if not yet + +runs: + using: composite + steps: + - name: Prepare git + shell: bash + run: | + url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" + + if [[ -f .git/shallow.lock ]] || [[ -f .git/index.lock ]]; then + rm -rf -- ..?* .[!.]* * + fi + + if [[ ! -d .git ]]; then + git init --initial-branch=trunk . + git config core.pager cat + git remote add origin "$url" + else + git config core.pager cat + git remote set-url origin "$url" + + git am --abort 2>/dev/null || true + git merge --abort 2>/dev/null || true + git rebase --abort 2>/dev/null || true + git revert --abort 2>/dev/null || true + git cherry-pick --abort 2>/dev/null || true + git bisect reset 2>/dev/null || true + + git reset --hard --quiet + git clean -ffdx --quiet . + + git switch -d || true + echo "clean-up branches" + branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' | grep -v ^'\s\+mirror/' || true)) + if [[ "$branches" ]]; then + git branch -D $branches || true + fi + git checkout --orphan trunk + git reset --hard + fi + + if [[ -z "$(git config user.name)" ]]; then + git config user.name "github-actions[bot]" + fi + if [[ -z "$(git config user.email)" ]]; then + git config user.email "github-actions[bot]@users.noreply.github.com" + fi + From 5e90c9fe3bcf61648159cf28c164be57e8b8963e Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 5 Sep 2025 12:43:38 +0200 Subject: [PATCH 48/62] gh-pages-*: add deploy and rm-path actions Signed-off-by: Jorge Marques --- gh-pages-deploy/README.md | 41 +++++++++++++ gh-pages-deploy/action.yml | 118 ++++++++++++++++++++++++++++++++++++ gh-pages-rm-path/README.md | 24 ++++++++ gh-pages-rm-path/action.yml | 65 ++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 gh-pages-deploy/README.md create mode 100644 gh-pages-deploy/action.yml create mode 100644 gh-pages-rm-path/README.md create mode 100644 gh-pages-rm-path/action.yml diff --git a/gh-pages-deploy/README.md b/gh-pages-deploy/README.md new file mode 100644 index 00000000..18dd879f --- /dev/null +++ b/gh-pages-deploy/README.md @@ -0,0 +1,41 @@ +GH Pages deploy +=============== + +Deploy a path to gh-pages. +Is protected against forks. + +On push, adds artifact to root ./, if new_tag is true, add to ./ as well. +On pull_request, adds to ./pull/ + +Important: if the workflow runs in other branches, filter the branches as in the example below. +If not, it will write to root and create tags (if new_tag == true) as well! + + +Usage: + +``` + +on: + push: + branches: + - main + - dev + - dev/* + + pull_request: + +jobs: + deploy-gh-pages: + runs-on: [self-hosted, v1] + permissions: + contents: write + if: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/pull/') }} + + steps: + - uses: analogdevicesinc/doctools/gh-pages-deploy@action + with: + new_tag: ${{ inputs.new_tag }} + tag: ${{ inputs.tag }} + name: html + +``` diff --git a/gh-pages-deploy/action.yml b/gh-pages-deploy/action.yml new file mode 100644 index 00000000..a259a370 --- /dev/null +++ b/gh-pages-deploy/action.yml @@ -0,0 +1,118 @@ +name: GH Pages Deploy +description: Deploy to gh-pages + +inputs: + gh-token: + description: "GitHub Token" + default: ${{ github.token }} + new_tag: + description: "If is a new tag" + required: true + tag: + description: "Tag value" + required: true + branch: + description: "Branch to manage" + default: gh-pages + name: + description: "Artifact to deploy" + required: true + +# behaviour: +# on push: +# * adds doc to root ./ +# * if is a new tag, add to ./ as well +# on pull_request: +# * adds to ./pull/ +# +# Important: +# if the workflow runs in other branches, +# do not run this workflow! + +runs: + using: composite + steps: + - name: Check run condition + shell: bash + run: | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + if [[ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then + echo "gh_pages_deploy_run=true" >> "$GITHUB_ENV" + else + echo "gh_pages_deploy_run=" >> "$GITHUB_ENV" + fi + elif [[ "${{ github.event_name }}" == "push" ]]; then + echo "gh_pages_deploy_run=true" >> "$GITHUB_ENV" + else + echo "gh_pages_deploy_run=" >> "$GITHUB_ENV" + fi + + - uses: analogdevicesinc/doctools/blank@action + if: ${{ env.gh_pages_deploy_run == 'true' }} + + - name: Context to path + if: ${{ env.gh_pages_deploy_run == 'true' }} + shell: bash + run: | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "doc_path=pull/${{ github.event.number }}" >> "$GITHUB_ENV" + elif [[ "${{ inputs.new_tag }}" == "true" ]]; then + echo "doc_path=${{ inputs.tag }}" >> "$GITHUB_ENV" + fi + + - name: Prepare gh-pages branch + if: ${{ env.gh_pages_deploy_run == 'true' }} + shell: bash + run: > + git ls-remote --exit-code --heads origin refs/heads/${{ inputs.branch }} && + ( + git fetch origin ${{ inputs.branch }}:${{ inputs.branch }} -f ; + git switch ${{ inputs.branch }} ; + if [[ "${{ github.event_name }}" == "push" ]]; then + built_docs=$(find . -mindepth 2 -name objects.inv -exec sh -c 'dirname {}' ';') ; + git rm -r . --quiet || true ; + if ! [ -z "$built_docs" ]; then + git checkout @ -- $built_docs ; + fi ; + fi ; + if ! [ -z "${{ env.doc_path }}" ]; then + git rm -r ${{ env.doc_path }} --quiet || true ; + fi ; + ) || ( + git checkout --orphan ${{ inputs.branch }} ; + git reset --hard ; + git commit -m "initial commit" --allow-empty + ) + + - uses: actions/download-artifact@v4 + if: ${{ env.gh_pages_deploy_run == 'true' && github.event_name == 'push' }} + with: + name: ${{ inputs.name }} + + - uses: actions/download-artifact@v4 + if: ${{ env.gh_pages_deploy_run == 'true' && env.doc_path != '' }} + with: + name: ${{ inputs.name }} + path: ${{ env.doc_path }} + + - name: Generate aux files + if: ${{ env.gh_pages_deploy_run == 'true' }} + shell: bash + run: > + touch .nojekyll ; + find . -name objects.inv -exec sh -c 'dirname {}' ';' | + cut -c 3- | sort -Vr | + jq --raw-input . | + jq --slurp . > tags.json + + - name: Commit and push to gh-pages + if: ${{ env.gh_pages_deploy_run == 'true' }} + shell: bash + run: | + git add . >> /dev/null + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + context=" #${{ github.event.number }} (${{ github.event.action }})" + fi + git commit -m "deploy: ${GITHUB_SHA}$context" --allow-empty + git push origin ${{ inputs.branch }}:${{ inputs.branch }} + diff --git a/gh-pages-rm-path/README.md b/gh-pages-rm-path/README.md new file mode 100644 index 00000000..3b6bd6ae --- /dev/null +++ b/gh-pages-rm-path/README.md @@ -0,0 +1,24 @@ +GH Pages remove path +==================== + +Removes a path from gh-pages, mostly useful for pull_requests. +Is protected against forks. + +Usage: + +``` +on: + pull_request: + types: [closed] + +jobs: + clean-gh-pages: + runs-on: [self-hosted, v1] + permissions: + contents: write + + steps: + - uses: analogdevicesinc/doctools/gh-pages-rm-path@action + with: + path: pull/${{ github.event.number }} +``` diff --git a/gh-pages-rm-path/action.yml b/gh-pages-rm-path/action.yml new file mode 100644 index 00000000..0cf68ad8 --- /dev/null +++ b/gh-pages-rm-path/action.yml @@ -0,0 +1,65 @@ +name: GH Pages Remove Path +description: Remove path from gh-pages + +inputs: + gh-token: + description: "GitHub Token" + default: ${{ github.token }} + path: + description: "Path(s) to delete" + required: true + branch: + description: "Branch to manage" + default: gh-pages + +# behaviour: +# removes the paths provides from gh-pages branch + +runs: + using: composite + steps: + - name: Check run condition + shell: bash + run: | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + if [[ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then + echo "gh_pages_remove_path_run=true" >> "$GITHUB_ENV" + else + echo "gh_pages_remove_path_run=" >> "$GITHUB_ENV" + fi + else + echo "gh_pages_remove_path_run=true" >> "$GITHUB_ENV" + fi + + - uses: analogdevicesinc/doctools/blank@action + if: ${{ env.gh_pages_remove_path_run == 'true' }} + + - name: Clean-up gh-pages branch + shell: bash + if: ${{ env.gh_pages_remove_path_run == 'true' }} + run: > + git ls-remote --exit-code --heads origin refs/heads/${{ inputs.branch }} && + ( + git fetch origin ${{ inputs.branch }}:${{ inputs.branch }} -f ; + git switch ${{ inputs.branch }} ; + git rm -r ${{ inputs.path }} --quiet || true ; + echo "gh_pages_exist=true" >> "$GITHUB_ENV" ; + ) + + - name: Generate aux files + shell: bash + if: ${{ env.gh_pages_remove_path_run == 'true' && env.gh_pages_exist == 'true' }} + run: > + touch .nojekyll ; + find . -name objects.inv -exec sh -c 'dirname {}' ';' | + cut -c 3- | sort -Vr | + jq --raw-input . | + jq --slurp . > tags.json + + - name: Commit and push to gh-pages + shell: bash + if: ${{ env.gh_pages_remove_path_run == 'true' && env.gh_pages_exist == 'true' }} + run: | + git add . >> /dev/null + git commit -m "deploy: ${GITHUB_SHA} #${{ github.event.number }} (${{ github.event.action }})" --allow-empty + git push origin ${{ inputs.branch }}:${{ inputs.branch }} From c29bb73732e43f36cb40f238a00721a412affacc Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 5 Sep 2025 13:08:28 +0200 Subject: [PATCH 49/62] gh-pages-*: protect against concurrency Signed-off-by: Jorge Marques --- gh-pages-deploy/action.yml | 17 ++++++++++++++++- gh-pages-rm-path/action.yml | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/gh-pages-deploy/action.yml b/gh-pages-deploy/action.yml index a259a370..7ca9994e 100644 --- a/gh-pages-deploy/action.yml +++ b/gh-pages-deploy/action.yml @@ -114,5 +114,20 @@ runs: context=" #${{ github.event.number }} (${{ github.event.action }})" fi git commit -m "deploy: ${GITHUB_SHA}$context" --allow-empty - git push origin ${{ inputs.branch }}:${{ inputs.branch }} + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + json=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{ inputs.gh-token }}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" \ + | jq '{state, merged}') + state=$(echo "$json" | jq -r '.state') + if [[ "$state" == "open" ]]; then + git push origin ${{ inputs.branch }}:${{ inputs.branch }} + else + echo "pr state '$state' is not 'open', not pushing" + fi + else + git push origin ${{ inputs.branch }}:${{ inputs.branch }} + fi diff --git a/gh-pages-rm-path/action.yml b/gh-pages-rm-path/action.yml index 0cf68ad8..e75d6b51 100644 --- a/gh-pages-rm-path/action.yml +++ b/gh-pages-rm-path/action.yml @@ -62,4 +62,19 @@ runs: run: | git add . >> /dev/null git commit -m "deploy: ${GITHUB_SHA} #${{ github.event.number }} (${{ github.event.action }})" --allow-empty - git push origin ${{ inputs.branch }}:${{ inputs.branch }} + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + json=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{ inputs.gh-token }}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" \ + | jq '{state, merged}') + state=$(echo "$json" | jq -r '.state') + if [[ "$state" == "closed" ]]; then + git push origin ${{ inputs.branch }}:${{ inputs.branch }} + else + echo "pr state '$state' is not 'closed', not pushing" + fi + else + git push origin ${{ inputs.branch }}:${{ inputs.branch }} + fi From 37ea2d9dc20c5d52d70bae5bd1349de06f14ce37 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 5 Sep 2025 13:20:50 +0200 Subject: [PATCH 50/62] gh-pages-*: default new_tag=false and tag= Signed-off-by: Jorge Marques --- gh-pages-deploy/README.md | 1 + gh-pages-deploy/action.yml | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/gh-pages-deploy/README.md b/gh-pages-deploy/README.md index 18dd879f..a673cff2 100644 --- a/gh-pages-deploy/README.md +++ b/gh-pages-deploy/README.md @@ -39,3 +39,4 @@ jobs: name: html ``` + diff --git a/gh-pages-deploy/action.yml b/gh-pages-deploy/action.yml index 7ca9994e..09507f3d 100644 --- a/gh-pages-deploy/action.yml +++ b/gh-pages-deploy/action.yml @@ -7,10 +7,10 @@ inputs: default: ${{ github.token }} new_tag: description: "If is a new tag" - required: true + default: false tag: description: "Tag value" - required: true + default: "" branch: description: "Branch to manage" default: gh-pages @@ -56,7 +56,7 @@ runs: run: | if [[ "${{ github.event_name }}" == "pull_request" ]]; then echo "doc_path=pull/${{ github.event.number }}" >> "$GITHUB_ENV" - elif [[ "${{ inputs.new_tag }}" == "true" ]]; then + elif [[ "${{ inputs.new_tag }}" == "true" ]] && [[ -n "${{ inputs.tag }}" ]]; then echo "doc_path=${{ inputs.tag }}" >> "$GITHUB_ENV" fi From 029eebb43157d31915c17087fb09f5ee0cb5c6fb Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 18 Sep 2025 17:36:56 +0200 Subject: [PATCH 51/62] entrypoint.sh: add repo-only label Signed-off-by: Jorge Marques --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 5d963ce0..2a31c043 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,7 +15,7 @@ unset config_flags unset name_label if [[ -z "$runner_labels_" ]]; then - runner_labels_="v1" + runner_labels_="repo-only,v1" fi if [[ -z "$config_flags_" ]]; then From 7f99ab977c89be27dc9f9dd7e778f0f458e0da32 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 23 Sep 2025 15:40:18 +0200 Subject: [PATCH 52/62] checkout: clear stash Signed-off-by: Jorge Marques --- checkout/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/checkout/action.yml b/checkout/action.yml index 618ad30e..d91c7aba 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -37,6 +37,7 @@ runs: git revert --abort 2>/dev/null || true git cherry-pick --abort 2>/dev/null || true git bisect reset 2>/dev/null || true + git stash clear || true git reset --hard --quiet git clean -ffdx --quiet . From 4dac38cdcceba6e231f4700a0a508c405c766b21 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 23 Sep 2025 16:57:01 +0200 Subject: [PATCH 53/62] checkout: try to apply fixups Do a rebase --autosquash after resolving. Signed-off-by: Jorge Marques --- checkout/README.md | 1 + checkout/action.yml | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/checkout/README.md b/checkout/README.md index 6bdf7523..b902f9af 100644 --- a/checkout/README.md +++ b/checkout/README.md @@ -17,6 +17,7 @@ jobs: Key-points: +* Fixups are auto-squashed, as an alternative to force pushing. * For pull-requests, rebase on base reference instead of awkward merge commit. * Current checkout branch is always called `trunk` * Sets the `fetch_depth`, `base_sha`, `head_sha` and `ahead_by` to diff --git a/checkout/action.yml b/checkout/action.yml index d91c7aba..9a3b5092 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -95,6 +95,15 @@ runs: - name: Fetch & reset shell: bash run: | + # --empty was added on git 2.26.0 + has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true + echo $has_keep + if [[ "$has_keep" -gt "0" ]]; then + keep_empty="--empty=keep" + else + keep_empty="--keep-empty" + fi + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then if [[ "$behind_by" -eq 0 ]]; then git fetch origin --depth=$fetch_depth ${{ github.event.pull_request.head.sha }} @@ -102,14 +111,6 @@ runs: else git fetch origin --depth=$fetch_depth $GITHUB_SHA git reset --hard $GITHUB_SHA - # --empty was added on git 2.26.0 - has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true - echo $has_keep - if [[ "$has_keep" -gt "0" ]]; then - keep_empty="--empty=keep" - else - keep_empty="--keep-empty" - fi while ! git rebase $keep_empty --rebase-merges @~; do git rebase --abort echo "deepening by $fetch_depth commits" @@ -122,12 +123,19 @@ runs: ahead_by=$((fetch_depth - 1)) echo "ahead_by=$ahead_by" >> $GITHUB_ENV fi - echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - echo "base_sha=${{ github.event.pull_request.base.sha }}" >> "$GITHUB_ENV" + base_sha=${{ github.event.pull_request.base.sha }} else - echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" + base_sha=$(git rev-parse @~$ahead_by) fi + + # try to apply fixups + git rebase $keep_empty --rebase-merges --autosquash $base_sha \ + || git rebase --abort || true + + echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" + echo "base_sha=$base_sha" >> "$GITHUB_ENV" echo "fetch_depth=$(git rev-list --count --first-parent $GITHUB_SHA)" >> "$GITHUB_ENV" - name: Log commit range From 778cd026ee1ba07f4a34dd2a98de1028e0d795cf Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 23 Sep 2025 17:33:09 +0200 Subject: [PATCH 54/62] checkout: fix fetch_depth on behind-by=0, labels Signed-off-by: Jorge Marques --- checkout/action.yml | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 9a3b5092..52645984 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -17,6 +17,7 @@ runs: - name: Prepare git shell: bash run: | + # Prepare git url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" if [[ -f .git/shallow.lock ]] || [[ -f .git/index.lock ]]; then @@ -61,6 +62,7 @@ runs: - name: Get commit depth shell: bash run: | + # Get commit depth if [[ "${{ github.event.pull_request.head.sha }}" ]]; then echo "setting fetch_depth based on github.event.pull_request.head.sha" json=$(curl -s -L \ @@ -95,6 +97,7 @@ runs: - name: Fetch & reset shell: bash run: | + # Fetch & reset # --empty was added on git 2.26.0 has_keep=$(git rebase -help 2>&1 | grep "\--empty" | wc -l) || true echo $has_keep @@ -125,27 +128,40 @@ runs: fi if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - base_sha=${{ github.event.pull_request.base.sha }} + echo "base_sha=${{ github.event.pull_request.base.sha }}" >> "$GITHUB_ENV" else - base_sha=$(git rev-parse @~$ahead_by) + echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" fi - # try to apply fixups + - name: Apply fixups + shell: bash + run: | + # Apply fixups git rebase $keep_empty --rebase-merges --autosquash $base_sha \ || git rebase --abort || true + - name: Fill environment + shell: bash + run: | + # Fill environment echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" - echo "base_sha=$base_sha" >> "$GITHUB_ENV" - echo "fetch_depth=$(git rev-list --count --first-parent $GITHUB_SHA)" >> "$GITHUB_ENV" + + if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + if [[ "$behind_by" -ne 0 ]]; then + echo "fetch_depth=$(git rev-list --count --first-parent $GITHUB_SHA)" >> "$GITHUB_ENV" + fi + fi - name: Log commit range shell: bash run: | + # Log commit range git log --oneline --reverse $base_sha..$head_sha - name: Generate cache branches & rename shell: bash run: | + # Generate cache branches & rename if [[ "${{ github.event.pull_request.base.sha }}" ]]; then branch=${{ github.base_ref }} commit=${{ github.event.pull_request.base.sha }} From 552c86e6036970afe5ddb4431afe8b9d9845faaf Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 24 Sep 2025 14:09:06 +0200 Subject: [PATCH 55/62] checkout: rework rebase strategy Signed-off-by: Jorge Marques --- checkout/action.yml | 81 +++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 52645984..0a33ab7e 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -7,7 +7,6 @@ inputs: default: ${{ github.token }} # behaviour: -# github.event.pull_request.head.sha is always used to detect the PR case. # cache/ branches are created to avoid base refs from being garbage collected. # on PRs, the github automatic merge commit is flattened, allowing to check per commit. @@ -43,7 +42,7 @@ runs: git reset --hard --quiet git clean -ffdx --quiet . - git switch -d || true + git switch -d 2>/dev/null || true echo "clean-up branches" branches=$((git branch || true) | (grep -v ^'*' | grep -v ^'\s\+cache/' | grep -v ^'\s\+mirror/' || true)) if [[ "$branches" ]]; then @@ -63,35 +62,40 @@ runs: shell: bash run: | # Get commit depth - if [[ "${{ github.event.pull_request.head.sha }}" ]]; then + if [[ "${{ github.event_name }}" == "pull_request" ]]; then echo "setting fetch_depth based on github.event.pull_request.head.sha" json=$(curl -s -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${{ inputs.gh-token }}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ "https://api.github.com/repos/${{ github.repository }}/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }}" \ - | jq '{behind_by, ahead_by}') + | jq '{behind_by, ahead_by, merge_base_sha: .merge_base_commit.sha}') ahead_by=$(echo $json | jq '.ahead_by') behind_by=$(echo $json | jq '.behind_by') - if [[ "$ahead_by" == "null" ]]; then - echo "GitHub API failed, setting based on github.event.pull_request.commits + 100" - ahead_by=${{ github.event.pull_request.commits }} - fetch_depth=$(( ahead_by + 100 )) + merge_base_sha=$(echo $json | jq '.merge_base_sha') + if [[ "$merge_base_sha" == "null" ]]; then + echo "::warning ::GitHub API failed to determine merge base sha, will checkout merge commit." + echo "fetch_depth=2" >> $GITHUB_ENV + echo "checkout_fallback=true" >> $GITHUB_ENV else fetch_depth=$(( ahead_by + behind_by + 2 )) + echo "behind_by=$behind_by" >> $GITHUB_ENV + echo "merge_base_sha=$merge_base_commit" >> $GITHUB_ENV + echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV fi - echo "ahead_by=$ahead_by" >> $GITHUB_ENV - echo "behind_by=$behind_by" >> $GITHUB_ENV - echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then echo "setting fetch_depth based on github.event.before" commits_sha='${{ toJSON(github.event.commits.*.id) }}' ; fetch_depth=$(( $(jq <<<"$commits_sha" length) + 1 )) ; echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV ; else - echo "setting fetch_depth as 6 (fallback, either due to force-push, new-branch, or uncaught condition)" + if [[ "${{ github.event.before }}" =~ "0000000000000000" ]]; then + echo "::notice ::New branch, will set base commit as last 5 commits." + else + echo "::notice ::Force pushed, will set base commit as last 5 commits." + fi echo "fetch_depth=6" >> $GITHUB_ENV - echo "fetch_depth_fallback=true" >> $GITHUB_ENV + echo "checkout_fallback=true" >> $GITHUB_ENV fi - name: Fetch & reset @@ -107,14 +111,17 @@ runs: keep_empty="--keep-empty" fi - if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - if [[ "$behind_by" -eq 0 ]]; then + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + if [[ "$checkout_fallback" == "true" ]]; then + git fetch origin --depth=$fetch_depth $GITHUB_SHA + git reset --hard $GITHUB_SHA + elif [[ "$behind_by" -eq 0 ]]; then git fetch origin --depth=$fetch_depth ${{ github.event.pull_request.head.sha }} git reset --hard ${{ github.event.pull_request.head.sha }} else git fetch origin --depth=$fetch_depth $GITHUB_SHA - git reset --hard $GITHUB_SHA - while ! git rebase $keep_empty --rebase-merges @~; do + git reset --hard ${{ github.event.pull_request.head.sha }} + while ! git rebase --rebase-merges $keep_empty ${{ github.event.pull_request.base.sha }}; do git rebase --abort echo "deepening by $fetch_depth commits" git fetch origin --deepen=$fetch_depth $GITHUB_SHA @@ -123,34 +130,30 @@ runs: else git fetch origin --depth=$fetch_depth $GITHUB_SHA git reset --hard $GITHUB_SHA - ahead_by=$((fetch_depth - 1)) - echo "ahead_by=$ahead_by" >> $GITHUB_ENV fi - if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - echo "base_sha=${{ github.event.pull_request.base.sha }}" >> "$GITHUB_ENV" + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + if [[ "$checkout_fallback" != "true" ]]; then + base_sha=${{ github.event.pull_request.base.sha }} + else + base_sha=$(git rev-parse @~) + fi else - echo "base_sha=$(git rev-parse @~$ahead_by)" >> "$GITHUB_ENV" + if [[ "$checkout_fallback" != "true" ]]; then + base_sha=${{ github.event.before }} + else + base_sha=$(git rev-parse @~$((fetch_depth - 1))) + fi fi - - name: Apply fixups - shell: bash - run: | - # Apply fixups - git rebase $keep_empty --rebase-merges --autosquash $base_sha \ - || git rebase --abort || true + if [[ "$checkout_fallback" != "true" ]]; then + echo "Applying fixups" + git rebase $keep_empty --rebase-merges --autosquash $base_sha \ + || git rebase --abort || true + fi - - name: Fill environment - shell: bash - run: | - # Fill environment echo "head_sha=$(git rev-parse @)" >> "$GITHUB_ENV" - - if [[ "${{ github.event.pull_request.head.sha }}" ]]; then - if [[ "$behind_by" -ne 0 ]]; then - echo "fetch_depth=$(git rev-list --count --first-parent $GITHUB_SHA)" >> "$GITHUB_ENV" - fi - fi + echo "base_sha=$base_sha" >> "$GITHUB_ENV" - name: Log commit range shell: bash @@ -162,7 +165,7 @@ runs: shell: bash run: | # Generate cache branches & rename - if [[ "${{ github.event.pull_request.base.sha }}" ]]; then + if [[ "${{ github.event_name }}" == "pull_request" ]]; then branch=${{ github.base_ref }} commit=${{ github.event.pull_request.base.sha }} else From 796fd188a6ac559839e65f22dacbcbef9819498a Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Wed, 24 Sep 2025 14:43:27 +0200 Subject: [PATCH 56/62] checkout: clean-up merge_base_sha, echo Signed-off-by: Jorge Marques --- checkout/action.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/checkout/action.yml b/checkout/action.yml index 0a33ab7e..1f5f5b97 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -63,7 +63,6 @@ runs: run: | # Get commit depth if [[ "${{ github.event_name }}" == "pull_request" ]]; then - echo "setting fetch_depth based on github.event.pull_request.head.sha" json=$(curl -s -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${{ inputs.gh-token }}" \ @@ -80,7 +79,6 @@ runs: else fetch_depth=$(( ahead_by + behind_by + 2 )) echo "behind_by=$behind_by" >> $GITHUB_ENV - echo "merge_base_sha=$merge_base_commit" >> $GITHUB_ENV echo "fetch_depth=$fetch_depth" >> $GITHUB_ENV fi elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then From 6514abf80038c64e375e7b5800792c932f9a73a4 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 2 Oct 2025 10:34:57 +0200 Subject: [PATCH 57/62] checkout: add lfs input, default to false To not fetch lfs artifacts by default. Signed-off-by: Jorge Marques --- checkout/action.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index 1f5f5b97..fb94b0d0 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -5,6 +5,10 @@ inputs: gh-token: description: "GitHub Token" default: ${{ github.token }} + lfs: + description: "Enable Git LFS" + default: 'false' + # behaviour: # cache/ branches are created to avoid base refs from being garbage collected. @@ -16,6 +20,15 @@ runs: - name: Prepare git shell: bash run: | + if command -v git-lfs >/dev/null 2>&1; then + if [ "${{ inputs.lfs }}" = "true" ]; then + git lfs install + else + git lfs install --skip-smudge + fi + elif [ "${{ inputs.lfs }}" = "true" ]; then + echo "::warning ::Git LFS set to true, but git-lfs is not installed." + fi # Prepare git url="https://x-access-token:${{ inputs.gh-token }}@github.com/${{ github.repository }}.git" From 7f7dde974f438d85f0033731cf5cb48cf2df61f3 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Fri, 3 Oct 2025 19:33:27 +0200 Subject: [PATCH 58/62] bashrc: add support for ci branch Add handlers for using scripts from the ci branch. ci/build.sh at current branch is not supported anymore. It will prepare a worktree at $git-common-dir/../_ci, the source either user.sh (higher precedence) or build.sh. build.sh has the old behaviour of printing all the methods sourced, while user.sh is intended for a full custom implementation, for example, farseeing installing a "ci" (pip)? package. Signed-off-by: Jorge Marques --- bashrc | 56 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/bashrc b/bashrc index 01f1250f..915ef152 100644 --- a/bashrc +++ b/bashrc @@ -1,32 +1,56 @@ #!/bin/bash -[ -z "$PS1" ] || it=true +it=false ; [ -z "$PS1" ] || it=true if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then - pushd $(git rev-parse --show-toplevel) &>/dev/null + pushd $(realpath $(git rev-parse --git-common-dir)/..) 1>/dev/null + + ci_worktree=$(git worktree list | grep '\[_ci\]' | cut -f1 -d " ") + if [[ -z "$ci_worktree" ]]; then + remote=$(git remote -v | grep analogdevicesinc | head -1 | cut -f1) + if [[ ! -z "$remote" ]]; then + $it && printf "Remote '\e[34m$remote\e[0m' matches '\e[34manalogdevicesinc\e[0m'." + if git ls-remote --exit-code --heads $remote refs/heads/ci 1>/dev/null ; then + $it && printf "\b, fetching branch '\e[34mci\e[0m'..." + git fetch --quiet $remote ci + git branch -D _ci &>/dev/null + git worktree add -q -b _ci _ci $remote/ci + ci_worktree=$(git worktree list | grep '\[_ci\]' | cut -f1 -d " ") + $it && printf "\33[2K\rFetched CI branch to '$ci_worktree'.\n" + $it && printf "To \e[34mclean-up\e[0m, use: \n" + $it && printf " \e[34mgit worktree remove _ci\e[0m\n\n" + fi + fi + fi + if [[ ! -z "$ci_worktree" ]]; then + if [[ -f "$ci_worktree/ci/user.sh" ]]; then + source $ci_worktree/ci/user.sh + elif [[ -f "$ci_worktree/ci/build.sh" ]]; then + source $ci_worktree/ci/build.sh + if $it; then + printf "Sourced methods from '$ci_worktree/ci/build.sh':\n\e[34m" + grep -oE '^[a-zA-Z][a-zA-Z0-9_]*\s*\(\)\s*\{' $ci_worktree/ci/build.sh | \ + awk -F'[ ()]+' '{print $1}' | column + printf "\e[0m" + fi + fi + fi + if [[ -z "$head_sha" ]]; then export head_sha=@ fi if [[ -z "$base_sha" ]]; then export base_sha=@~6 fi - if [[ "$it" == "true" ]]; then - printf "Set git commit range as (\$base_sha..\$head_sha)\n" - printf "\e[34m$base_sha..$head_sha\e[0m\n" - printf "Adjust variables range as needed.\n" - fi - if [[ -f "ci/build.sh" ]]; then - source ci/build.sh - if [[ "$it" == "true" ]]; then - printf "\nSourced methods from ci/build.sh:\n\e[34m" - grep -oE '^[a-zA-Z][a-zA-Z0-9_]*\s*\(\)\s*\{' ci/build.sh | \ - awk -F'[ ()]+' '{print $1}' | paste -sd ' ' - printf "\e[0m" - fi + if $it; then + printf "The git commit range was set to:\n" + printf "\$base_sha..\$head_sha: \e[34m$base_sha..$head_sha\e[0m \n" + printf "Adjust '\$base_sha' and '\$head_sha' to your work range.\n" fi + popd &>/dev/null else - printf "Not a git repository, not setting git commit range variables or sourcing ci/build.sh\n" + printf "\e[31mNot\e[0m a git repository! Won't set ci tools, nor git commit range.\n" fi github_pipe_fifo=/tmp/github_env_$(tr -dc A-Za-z0-9 Date: Fri, 3 Oct 2025 19:54:48 +0200 Subject: [PATCH 59/62] bashrc,entrypoint.sh: remove, moved to ci branch Signed-off-by: Jorge Marques --- bashrc | 84 --------------------------------------------------- entrypoint.sh | 83 -------------------------------------------------- 2 files changed, 167 deletions(-) delete mode 100644 bashrc delete mode 100644 entrypoint.sh diff --git a/bashrc b/bashrc deleted file mode 100644 index 915ef152..00000000 --- a/bashrc +++ /dev/null @@ -1,84 +0,0 @@ -#!/bin/bash - -it=false ; [ -z "$PS1" ] || it=true - -if git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then - pushd $(realpath $(git rev-parse --git-common-dir)/..) 1>/dev/null - - ci_worktree=$(git worktree list | grep '\[_ci\]' | cut -f1 -d " ") - if [[ -z "$ci_worktree" ]]; then - remote=$(git remote -v | grep analogdevicesinc | head -1 | cut -f1) - if [[ ! -z "$remote" ]]; then - $it && printf "Remote '\e[34m$remote\e[0m' matches '\e[34manalogdevicesinc\e[0m'." - if git ls-remote --exit-code --heads $remote refs/heads/ci 1>/dev/null ; then - $it && printf "\b, fetching branch '\e[34mci\e[0m'..." - git fetch --quiet $remote ci - git branch -D _ci &>/dev/null - git worktree add -q -b _ci _ci $remote/ci - ci_worktree=$(git worktree list | grep '\[_ci\]' | cut -f1 -d " ") - $it && printf "\33[2K\rFetched CI branch to '$ci_worktree'.\n" - $it && printf "To \e[34mclean-up\e[0m, use: \n" - $it && printf " \e[34mgit worktree remove _ci\e[0m\n\n" - fi - fi - fi - if [[ ! -z "$ci_worktree" ]]; then - if [[ -f "$ci_worktree/ci/user.sh" ]]; then - source $ci_worktree/ci/user.sh - elif [[ -f "$ci_worktree/ci/build.sh" ]]; then - source $ci_worktree/ci/build.sh - if $it; then - printf "Sourced methods from '$ci_worktree/ci/build.sh':\n\e[34m" - grep -oE '^[a-zA-Z][a-zA-Z0-9_]*\s*\(\)\s*\{' $ci_worktree/ci/build.sh | \ - awk -F'[ ()]+' '{print $1}' | column - printf "\e[0m" - fi - fi - fi - - if [[ -z "$head_sha" ]]; then - export head_sha=@ - fi - if [[ -z "$base_sha" ]]; then - export base_sha=@~6 - fi - if $it; then - printf "The git commit range was set to:\n" - printf "\$base_sha..\$head_sha: \e[34m$base_sha..$head_sha\e[0m \n" - printf "Adjust '\$base_sha' and '\$head_sha' to your work range.\n" - fi - - popd &>/dev/null -else - printf "\e[31mNot\e[0m a git repository! Won't set ci tools, nor git commit range.\n" -fi - -github_pipe_fifo=/tmp/github_env_$(tr -dc A-Za-z0-9 Date: Fri, 3 Oct 2025 19:57:50 +0200 Subject: [PATCH 60/62] github-api.sh: remove from branch Located on main Signed-off-by: Jorge Marques --- github-api.sh | 106 -------------------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 github-api.sh diff --git a/github-api.sh b/github-api.sh deleted file mode 100644 index 1d72503e..00000000 --- a/github-api.sh +++ /dev/null @@ -1,106 +0,0 @@ -#!/bin/bash - -gh-get-release-id() -{ - release_id=$(curl -L \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$2/releases/tags/$3" \ - | jq -r .id) - echo "$4=$release_id" >> "$GITHUB_ENV" - echo $release_id -} - -gh-get-asset-id() -{ - asset_id=$(curl -L \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$2/releases/$3/assets" \ - | jq -r ".[] | select(.name==\"$asset_name\") | .id") - echo "$4=$asset_id" >> "$GITHUB_ENV" - echo $asset_id -} - -gh-create-release() -{ - release_id=$(curl -L \ - -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$2/releases" \ - -d "{\"tag_name\":\"$3\",\"name\":\"$3\",\"make_latest\":\"true\"}" \ - | jq -r .id) - echo "$4=$release_id" >> "$GITHUB_ENV" - echo $release_id -} - -gh-upload-asset() -{ - curl -L \ - -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - -H "Content-Type: application/octet-stream" \ - "https://uploads.github.com/repos/$2/releases/$3/assets?name=$4" \ - --data-binary "@$5" -} - -gh-delete-asset() -{ - curl -L \ - -X DELETE \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$2/releases/assets/$3" -} - -gh-update-commitish() -{ - curl -s \ - -X PATCH \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - -d '{"target_commitish": "$3"}' \ - "https://api.github.com/repos/$2/releases/$4" -} - -gh-actions-token() -{ - runner_token=$(curl -L \ - -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$2/actions/runners/registration-token" \ - | jq -r .token) - echo $runner_token -} - -gh-get-number-commits() -{ - total_commits=$(curl -L \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$2/compare/$3...$4" \ - | jq ".total_commits") - echo "total_commits=$total_commits" >> "$GITHUB_ENV" - echo $total_commits -} - -gh-cancel-workflow() -{ - curl -L \ - -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $1" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - https://api.github.com/repos/$2/actions/runs/$3/cancel -} From ee7a91298b515572f9ddd580d9aa864da79c23a5 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 7 Oct 2025 18:44:19 +0200 Subject: [PATCH 61/62] changed-files: add action Signed-off-by: Jorge Marques --- changed-files/README.md | 71 ++++++++++++++++++++++++ changed-files/action.yml | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 changed-files/README.md create mode 100644 changed-files/action.yml diff --git a/changed-files/README.md b/changed-files/README.md new file mode 100644 index 00000000..0d681155 --- /dev/null +++ b/changed-files/README.md @@ -0,0 +1,71 @@ +Changed files +============= + +Get if files changed. Changed files list is written to changed_files. Writes to +both environment and output. $base_sha and $head_sha can be on the enviroment, +passed explicitely, or obtained through the github action context. The +resolution is equal to doctools/checkout@action, and the action can be added +right after. + +Usage: + +``` +on: workflow_call +jobs: + checks: + runs-on: [self-hosted, repo-only] + outputs: + package: ${{ steps.state.outputs.package }} + doc: ${{ steps.state.outputs.doc }} + #ci: ${{ steps.state.outputs.ci }} suppressed + + steps: + - uses: analogdevicesinc/doctools/changed-files@action + id: state + with: + rules: | + package:^adi_doctools/ + doc:^doc/ + ci:^.github/\|^ci/ +``` + +Example + +``` +on: + push: + branches: + - main + - dev/* + pull_request: + +jobs: + state: + runs-on: [self-hosted, repo-only] + outputs: + changed: ${{ steps.state.outputs.changed_keys }} + steps: + - uses: analogdevicesinc/doctools/changed-files@action-debug + id: state + with: + rules: | + package:^adi_doctools/ + doc:^doc/ + ci:^.github/\|^ci/ + + build-package: + needs: [state] + if: contains(needs.state.outputs.changed, 'pakage') + uses: ./.github/workflows/build-package.yml + secrets: inherit + + debug-state: + runs-on: [self-hosted, repo-only] + needs: [state] + env: + changed: ${{ needs.state.outputs.changed }} + steps: + - run: echo $changed + - run: echo ${{ needs.state.outputs.changed }} + +``` diff --git a/changed-files/action.yml b/changed-files/action.yml new file mode 100644 index 00000000..5bbbf848 --- /dev/null +++ b/changed-files/action.yml @@ -0,0 +1,117 @@ +name: Git Changed files +description: A git changed_keys files for local runners + +inputs: + gh-token: + description: "GitHub Token" + default: ${{ github.token }} + rules: + description: | + A newline-separated list of key:pattern rules, e.g.,: + package:^adi_doctools/ + doc:^doc/ + ci:^.github/\|^ci/ + required: true + base_sha: + required: false + head_sha: + required: false + +outputs: + changed_keys: + description: "Comma-separated list of matches" + value: ${{ steps.check.outputs.changed_keys }} + + all_changed_files: + description: "New line separated list of all files" + value: ${{ steps.check.outputs.all_changed_files }} + +runs: + using: composite + steps: + - name: Get changed_keys files + id: check + shell: bash + run: | + # Get changed_keys files + changed_keys="" + rules="${{ inputs.rules }}" + + [[ -n "${{ inputs.base_sha }}" ]] && base_sha="${{ inputs.base_sha }}" + [[ -n "${{ inputs.head_sha }}" ]] && head_sha="${{ inputs.head_sha }}" + + if [[ -z "$base_sha" || -z "$head_sha" ]]; then + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + base_sha=${{ github.event.pull_request.base.sha }} + head_sha=${{ github.event.pull_request.head.sha }} + elif [[ ! "${{ github.event.before }}" =~ "0000000000000000" ]] && [[ "${{ github.event.forced }}" == "false" ]]; then + base_sha=${{ github.event.before }} + head_sha=${{ github.sha }} + else + if [[ "${{ github.event.before }}" =~ "0000000000000000" ]]; then + echo "::notice ::New branch, will set all rules to true." + else + echo "::notice ::Force pushed, will set all rules to true." + fi + while IFS= read -r line; do + [[ -z "$key" ]] && continue + key="${line%%:*}" + echo "$key=true" >> "$GITHUB_ENV" + changed_keys="$changed_keys,$key" + done <<< "$rules" + echo "changed_keys=$changed_keys" >> "$GITHUB_OUTPUT" + exit + fi + elif [[ "$checkout_fallback" == "true" ]]; then + echo "::notice ::Checkout reported fallback, will set all rules to true." + while IFS= read -r line; do + [[ -z "$key" ]] && continue + key="${line%%:*}" + echo "$key=true" >> "$GITHUB_ENV" + changed_keys="$changed_keys,$key" + done <<< "$rules" + echo "changed_keys=$changed_keys" >> "$GITHUB_OUTPUT" + exit + fi + + json=$(curl -sfL \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{ inputs.gh-token }}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/${{ github.repository }}/compare/$base_sha...$head_sha" \ + | jq '{ahead_by, files}') + + ahead_by=$(echo $json | jq '.ahead_by') + if [[ "$ahead_by" == "null" ]]; then + echo "::warning ::GitHub API failed to determine compare, will set all rules to true." + while IFS= read -r line; do + [[ -z "$key" ]] && continue + key="${line%%:*}" + echo "$key=true" >> "$GITHUB_ENV" + changed_keys="$changed_keys,$key" + done <<< "$rules" + echo "changed_keys=$changed_keys" >> "$GITHUB_OUTPUT" + exit + fi + + all_changed_files=$(echo $json | jq '.files[].filename' -r) + echo "$all_changed_files" + + echo "all_changed_files=$all_changed_files" >> "$GITHUB_ENV" + echo "all_changed_files=$all_changed_files" >> "$GITHUB_OUTPUT" + + while IFS= read -r line; do + key="${line%%:*}" + pattern="${line#*:}" + + [[ -z "$key" ]] && continue + + echo "$pattern" + + if echo "$all_changed_files" | grep "$pattern"; then + echo "$key=true" >> "$GITHUB_ENV" + changed_keys="$changed_keys,$key" + fi + done <<< "$rules" + + echo "changed_keys=$changed_keys" >> "$GITHUB_OUTPUT" From b4174b1f3376f8b8a04b20e4dcc70226fdd93668 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Tue, 14 Oct 2025 18:36:30 +0200 Subject: [PATCH 62/62] checkout: add deepen to push event In case github reports a wonky number. Signed-off-by: Jorge Marques --- checkout/action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/checkout/action.yml b/checkout/action.yml index fb94b0d0..459d854c 100644 --- a/checkout/action.yml +++ b/checkout/action.yml @@ -157,6 +157,10 @@ runs: fi fi + while ! git rev-parse $base_sha ; do + git fetch origin --deepen=$fetch_depth $GITHUB_SHA + done + if [[ "$checkout_fallback" != "true" ]]; then echo "Applying fixups" git rebase $keep_empty --rebase-merges --autosquash $base_sha \