-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Adam Jeliński <[email protected]>
- Loading branch information
Showing
3 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
name: 'Conda Lock Updating Action' | ||
description: 'Action Pushes a Branch with Conda Lock Update' | ||
|
||
inputs: | ||
commit_message: | ||
description: 'Message of the bump commit' | ||
required: true | ||
default: '[BOT] Bump Conda Lock' | ||
user_name: | ||
description: 'Name of the user creating commit' | ||
required: true | ||
default: 'CONDA_LOCK_UPDATING_BOT' | ||
user_email: | ||
description: 'Email address of the user creating commit' | ||
required: true | ||
default: '<>' | ||
branch_name: | ||
description: "Name of the pushed branch; it will be suffixed with GH Action's Run ID" | ||
required: true | ||
default: 'update_lock' | ||
environment_file: | ||
description: 'Path of the `environment.yml` file' | ||
required: true | ||
default: 'environment.yml' | ||
conda_lock_file: | ||
description: 'Path of the Conda Lock file (needs to have txt/yml/yaml extension)' | ||
required: true | ||
default: 'conda_lock.yml' | ||
pr_title: | ||
description: 'Title of the Pull Request; it will be suffixed with UTC time and date' | ||
required: true | ||
default: '[BOT] Bump Conda Lock' | ||
github_token: | ||
description: 'GitHub Access Token; use Private Access Token to trigger workflows issuing the Pull Request' | ||
required: true | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- id: lock-update | ||
shell: bash | ||
run: | | ||
echo "::group::Check git" | ||
which git | ||
git --version | ||
echo "::endgroup::" | ||
echo "::group::Check conda" | ||
which conda | ||
conda -V | ||
echo | ||
echo "Packages in the current environment:" | ||
conda list | ||
echo "::endgroup::" | ||
echo "::group::Check python" | ||
which python3 | ||
python3 -V | ||
echo | ||
echo "Python modules installed in pip:" | ||
python3 -m pip list | ||
echo "::endgroup::" | ||
echo "::group::Check curl" | ||
which curl | ||
curl -V | ||
echo "::endgroup::" | ||
echo "::group::Capture Base Branch Name" | ||
set -x | ||
export PR_BASE=$(git branch --show-current) | ||
if [ "$PR_BASE" = "" ]; then | ||
echo "Capturing base branch failed!" | ||
exit 1 | ||
fi | ||
set +x | ||
echo "::endgroup::" | ||
echo "::group::Create New Branch" | ||
export BRANCH_NAME="${{ inputs.branch_name }}_${{ github.run_id }}" | ||
git checkout -b "$BRANCH_NAME" | ||
echo "::endgroup::" | ||
echo "::group::Install ruamel.yaml" | ||
python3 -m pip install ruamel.yaml | ||
echo "::endgroup::" | ||
echo "::group::Update Conda Lock" | ||
set -x | ||
# Export variables used by the script | ||
export BOT_CONDA_LOCK="${{ inputs.conda_lock_file }}" | ||
export BOT_ENV_YML="${{ inputs.environment_file }}" | ||
EXIT_CODE=0 | ||
python3 $GITHUB_ACTION_PATH/update_lock.py || EXIT_CODE=$? | ||
echo "Script returned code: $EXIT_CODE" | ||
if [ $EXIT_CODE -eq 3 ]; then | ||
exit 0 | ||
elif [ $EXIT_CODE -ne 0 ]; then | ||
exit $EXIT_CODE | ||
fi | ||
set +x | ||
echo "::endgroup::" | ||
echo "::group::Add And Commit Changes" | ||
set -x | ||
git config user.name "${{ inputs.user_name }}" | ||
git config user.email "${{ inputs.user_email }}" | ||
git add "$BOT_CONDA_LOCK" | ||
git commit -m "${{ inputs.commit_message }}" | ||
set +x | ||
echo "::endgroup::" | ||
echo "::group::Push Changes" | ||
git push -u origin "$BRANCH_NAME" | ||
echo "::endgroup::" | ||
echo "::group::Create a Pull Request" | ||
# Export variables used by the script | ||
export GITHUB_TOKEN="${{ inputs.github_token }}" | ||
set -x | ||
export GITHUB_REPOSITORY="${{ github.repository }}" | ||
# PR_BASE has been already exported before the `git checkout -b` command | ||
export PR_HEAD="$BRANCH_NAME" | ||
export PR_TITLE="${{ inputs.pr_title }} $(date -u +'%Y-%m-%d %H:%M %p %Z')" | ||
python3 $GITHUB_ACTION_PATH/create_pull_request.py | ||
set +x | ||
echo "::endgroup::" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
import requests | ||
import sys | ||
from os import environ | ||
|
||
|
||
def create_pull_request(gh_repo, gh_token, pr_base, pr_head, pr_title): | ||
response = requests.post( | ||
'https://api.github.com/repos/' + gh_repo + '/pulls', | ||
headers={ | ||
'Authorization': 'token ' + gh_token, | ||
'Accept': 'application/vnd.github.v3+json', | ||
}, | ||
json={ | ||
'base': pr_base, | ||
'head': pr_head, | ||
'title': pr_title, | ||
}, | ||
) | ||
if response.status_code != 201: | ||
print('ERROR: Pull Request creation failed with status: ' | ||
+ str(response.status_code) + ' ' + response.reason) | ||
print() | ||
print('GitHub API response data was:') | ||
json.dump(response.json(), sys.stdout, indent=2) | ||
print() | ||
sys.exit(1) | ||
else: | ||
print('Pull Request created successfully!') | ||
print('It\'s available at: ' + response.json()['html_url']) | ||
|
||
|
||
if __name__ == '__main__': | ||
create_pull_request( | ||
environ['GITHUB_REPOSITORY'], | ||
environ['GITHUB_TOKEN'], | ||
environ['PR_BASE'], | ||
environ['PR_HEAD'], | ||
environ['PR_TITLE'], | ||
) |
Oops, something went wrong.