Skip to content

Commit

Permalink
Add options for the pants-init action (#21)
Browse files Browse the repository at this point in the history
* Add configuration options for cache location

* Reformat the if-conditions

* Add GH_HOST configuration for enterprise

* Move the path setting to the top of the step

This should ensure that warnings aren't produced about $HOME/bin not
being on the path.

* Use the temp folder set for the runner

Instead of hardcoding /tmp as the temporary path

* Apply suggestions from code review

Thanks for catching the typos!

Co-authored-by: Andreas Stenius <git@astekk.se>

* Update docs

* Review comments

---------

Co-authored-by: Andreas Stenius <git@astekk.se>
  • Loading branch information
bryanwweber and kaos authored May 5, 2023
1 parent 402e2fb commit 7151afe
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions init-pants/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ inputs:
The hash to cache the named caches against. Should be a hash generated by hashFiles()
on all inputs to named caches, which are typically your lockfiles.
required: true
named-caches-location:
description: |
The location of the named cache, as specified in `pants.toml` or another pants config
file. Default is `~/.cache/pants/named_caches`.
required: false
default: "~/.cache/pants/named_caches"
pants-ci-config:
description: |
An extra config file to use in CI. Defaults to pants.ci.toml if present, or no
Expand All @@ -48,16 +54,31 @@ inputs:
avoids these problems.
required: false
default: 'false' # a string!
lmdb-store-location:
description: |
The location of the lmdb store, as specified in `pants.toml` or another pants config file.
Default is `~/.cache/pants/lmdb_store`.
required: false
default: "~/.cache/pants/lmdb_store"
setup-python-for-plugins:
description: |
If set to 'true', this action will set up a Python interpreter suitable for testing/linting
custom Pants plugin code in your repo. Pants plugins will run on the interpreter embedded
custom Pants plugin code in your repo. Pants plugins will run on the interpreter embedded
in Pants, and so must be tested/linted on an interpreter of the same version (currently 3.9).
This may be different than the interpreter version(s) your other Python code requires.
So this convenience option streamlines installing an interpreter specifically for
testing/linting plugin code.
required: false
default: 'false' # a string!
gh-host:
description: |
The lmdb store can be restored from the base branch, if it exists. This option configures the
host that is used to determine the latest commit on the base branch. This should be configured
as the hostname only, no protocol (for example, https) should be included. This is useful
if you are using an enterprise instance of GitHub whose host is not 'https://github.com'.
See https://cli.github.com/manual/gh_help_environment.
required: false
default: "github.com"

runs:
using: "composite"
Expand All @@ -66,19 +87,19 @@ runs:
shell: bash
run: |
if ! command -v pants; then
echo "$HOME/bin" >> $GITHUB_PATH
if [[ -f ./get-pants.sh ]]; then
./get-pants.sh
else
curl --proto '=https' --tlsv1.2 -fsSLo /tmp/get-pants.sh \
curl --proto '=https' --tlsv1.2 -fsSLo ${{ runner.temp }}/get-pants.sh \
https://raw.githubusercontent.com/pantsbuild/setup/${{ inputs.setup-commit }}/get-pants.sh
chmod +x /tmp/get-pants.sh
/tmp/get-pants.sh
chmod +x ${{ runner.temp }}/get-pants.sh
${{ runner.temp }}/get-pants.sh
fi
echo "$HOME/bin" >> $GITHUB_PATH
fi
- name: Setup interpreter for testing in-repo Pants plugins
if: ${{ inputs.setup-python-for-plugins == 'true' }}
if: inputs.setup-python-for-plugins == 'true'
uses: actions/setup-python@v4
with:
python-version: '3.9'
Expand Down Expand Up @@ -107,8 +128,7 @@ runs:
- name: Cache Pants named caches
uses: actions/cache@v3
with:
path: |
~/.cache/pants/named_caches
path: ${{ inputs.named-caches-location }}
key: pants-named-caches-${{ runner.os }}-${{ inputs.gha-cache-key }}-${{ hashFiles('pants.toml') }}-${{ inputs.named-caches-hash }}
restore-keys: |
pants-named-caches-${{ runner.os }}-${{ inputs.gha-cache-key }}-${{ hashFiles('pants.toml') }}-
Expand All @@ -117,7 +137,7 @@ runs:
# Looking up the commit allows us to use the cache from the latest commit on the base branch.
- name: Get Pants Cache Commit (base branch commit to pull cache from)
id: pants_cache_commit
if: ${{ inputs.cache-lmdb-store == 'true' }}
if: inputs.cache-lmdb-store == 'true'
shell: bash
# we could use this, but only if fetch-depth goes back far enough
# COMMIT=$(git merge-base ${GITHUB_BASE_REF:-${{ inputs.base-branch }}} HEAD | head -n1)
Expand All @@ -130,14 +150,17 @@ runs:
echo "CACHECOMMIT=${CACHECOMMIT}" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
# According to https://cli.github.com/manual/gh_help_environment, when GH_HOST is used to configure
# the hostname, the token must be named GITHUB_ENTERPRISE_TOKEN.
GITHUB_ENTERPRISE_TOKEN: ${{ github.token }}
GH_HOST: ${{ inputs.gh-host }}

- name: Cache Pants LMDB store
if: ${{ inputs.cache-lmdb-store == 'true' }}
if: inputs.cache-lmdb-store == 'true'
uses: actions/cache@v3
id: cache-pants-lmdb-store
with:
path: |
~/.cache/pants/lmdb_store
path: ${{ inputs.lmdb-store-location }}
# The commit SHA serves as a hash of all files in the repo.
# A remote cache service integrates with Pants's fine-grained invalidation and avoids these problems.
key: pants-lmdb-store-${{ runner.os }}-${{ inputs.gha-cache-key }}-${{ github.sha }}
Expand All @@ -149,7 +172,7 @@ runs:
# in the workflow that uses this composite action.
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
- name: Tell pants to use CI config
if: ${{ inputs.pants-ci-config != '' }}
if: inputs.pants-ci-config != ''
shell: bash
env:
PANTS_CONFIG_FILES: ${{ inputs.pants-ci-config }}
Expand Down

0 comments on commit 7151afe

Please sign in to comment.