-
-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GHA action to initialize pantsbuild env #5727
Conversation
411e3bb
to
bd8610f
Compare
- name: 'Set up Python for Pants (${{ inputs.pants-python-version }})' | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '${{ inputs.pants-python-version }}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the pants PoC workflow, this was here:
st2/.github/workflows/lint.yaml
Lines 77 to 80 in 9cf11f4
- name: 'Set up Python for Pants (3.9)' | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.9' |
I made the python-version configurable to facilitate upgrading the python version used.
# generally the matrix.python-version used for st2 itself | ||
|
||
runs: | ||
using: "composite" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB: this composite action has to be available on the master branch before it can be used because you have to provide a version, which we will set to @master
. So, you cannot modify the action in the same PR where you use the action.
- name: Get Pants Cache Merge Base Commit (base branch commit to pull cache from) | ||
id: pants-cache-commit | ||
run: | | ||
COMMIT=$(git merge-base ${GITHUB_BASE_REF:-${{ inputs.base-branch }}} HEAD | head -n1) | ||
echo MERGEBASE=${COMMIT} | ||
echo "::set-output name=MERGEBASE::${COMMIT}" | ||
|
||
- name: Cache Pants Caches (${{ inputs.gha-cache-key }}) | ||
uses: actions/cache@v2 | ||
id: pants-cache | ||
with: | ||
path: | | ||
~/.cache/pants/setup | ||
~/.cache/pants/lmdb_store | ||
~/.cache/pants/named_caches | ||
key: ${{ runner.os }}-pants-${{ inputs.gha-cache-key }}-${{ github.sha }} | ||
restore-keys: | | ||
${{ runner.os }}-pants-${{ inputs.gha-cache-key }}-${{ steps.pants-cache-commit.outputs.MERGEBASE }} | ||
${{ runner.os }}-pants-${{ inputs.gha-cache-key }}- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These steps make it so that we always use the cache from the last merge to master.
In the pants PoC workflow, this was here:
st2/.github/workflows/lint.yaml
Lines 86 to 104 in 9cf11f4
- name: Get Pants Cache Merge Base Commit (base branch commit to pull cache from) | |
id: pants-cache-commit | |
# TODO: switch default base branch from pants to main/master | |
run: | | |
COMMIT=$(git merge-base ${GITHUB_BASE_REF:-pants} HEAD | head -n1) | |
echo MERGEBASE=${COMMIT} | |
echo "::set-output name=MERGEBASE::${COMMIT}" | |
- name: Cache Pants Caches (Python ${{ matrix.python-version }}) | |
uses: actions/cache@v2 | |
id: pants-cache | |
with: | |
path: | | |
~/.cache/pants/setup | |
~/.cache/pants/lmdb_store | |
~/.cache/pants/named_caches | |
key: ${{ runner.os }}-pants-py${{ matrix.python-version }}-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-pants-py${{ matrix.python-version }}-${{ steps.pants-cache-commit.outputs.MERGEBASE }} | |
${{ runner.os }}-pants-py${{ matrix.python-version }}- |
I also made the base branch (master) configurable so that we can easily switch to main at some point (if we choose to do that).
- name: Bootstrap Pants | ||
run: | | ||
./pants --version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the pants PoC workflow, this was here:
st2/.github/workflows/lint.yaml
Lines 132 to 134 in 9cf11f4
- name: Bootstrap Pants | |
run: | | |
./pants --version |
- name: Tell pants to use CI config | ||
run: | | ||
echo "PANTS_CONFIG_FILES=pants.ci.toml" >> ${GITHUB_ENV} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding env vars like this to ${GITHUB_ENV}
makes the vars show up for all subsequent steps in the workflow (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
bd8610f
to
a174a6c
Compare
a174a6c
to
fba11fa
Compare
The scope of this PR changed as the action was moved under the pantsbuild umbrella. I'll open a different one. |
Background
This is another part of introducing
pants
, as discussed in the TSC Meetings on 12 July 2022, 02 Aug 2022 and 06 Sept 2022. Pants has fine-grained per-file caching of results for lint, fmt (like black), test, etc. It also has lockfiles that work well for monorepos that have multiple python packages. With these lockfiles CI should not break when any of our dependencies or our transitive dependencies release new versions, because CI will continue to use the locked version until we explicitly relock with updates.To keep PRs as manageable/reviewable as possible, introducing pants will take a series of PRs. I do not know yet how many PRs; I will break this up into logical steps with these goals:
pants
to the st2 repo, andpants
step-by-step.Previous pants PRs include:
Overview of this PR
It's helpful to review this PR one commit at a time.
This PR prepares us to run pants in GitHub Actions (GHA). There are 2 parts to this:
pants.ci.toml
(used in addition topants.toml
)pants-init GHA composite action
We are going to use pants in a lot of different workflows. I developed the steps that make up this action in the pants PoC:
https://github.com/st2sandbox/st2/blob/pants/.github/workflows/lint.yaml
That PoC workflow was based on this (but following some of the idioms from our st2 workflows):
https://github.com/pantsbuild/example-python/blob/main/.github/workflows/pants.yaml
Using this action will look like this:
It also allows changing the python version that pants uses (the version pants uses can be different from the versions we target for our code - that is OK and expected) and the base branch (eg to switch from master to main) like this:
I've added some comments to point things out in this workflow.
CI-specific pants config
The pants docs recommend adding a CI-specific pants config file called
pants.ci.toml
:https://www.pantsbuild.org/docs/using-pants-in-ci#configuring-pants-for-ci-pantscitoml-optional
Please note this quote from the docs:
So,
pants.ci.toml
does NOT replacepants.toml
, it extends it. So, pants will still use thepants.toml
, but we can tune settings specifically for GHA, and possibly CircleCI (if we need to use pants there).To make this work, the GHA action handles adding the
PANTS_CONFIG_FILES
env var. This way, authors that use our composite action will not have to remember to add that env var in their workflow.