From 1c50dba6f8c1e5a7c9a4295c01eb39ae8a5e4ea2 Mon Sep 17 00:00:00 2001 From: Matthew Watkins Date: Thu, 25 Apr 2024 15:20:23 +0100 Subject: [PATCH 1/3] Chore: Manually update bootstrap code Signed-off-by: Matthew Watkins --- .github/workflows/bootstrap.yaml | 128 +++++++++++++++++++------------ scripts/bootstrap.sh | 4 +- setup.py | 15 ---- 3 files changed, 81 insertions(+), 66 deletions(-) delete mode 100644 setup.py diff --git a/.github/workflows/bootstrap.yaml b/.github/workflows/bootstrap.yaml index 64b9721..41dac5a 100644 --- a/.github/workflows/bootstrap.yaml +++ b/.github/workflows/bootstrap.yaml @@ -33,17 +33,21 @@ jobs: GH_TOKEN: ${{ github.token }} # yamllint disable rule:line-length run: | - ### SHELL CODE START ### + #SHELLCODESTART - set -eu -o pipefail + set -euo pipefail + # set -x # Define variables DEVOPS_DIR=".devops" AUTOMATION_BRANCH="update-devops-tooling" REPO_DIR=$(git rev-parse --show-toplevel) + GIT_ORIGIN=$(git config --get remote.origin.url) + REPO_NAME=$(basename -s .git "$GIT_ORIGIN") EXCLUDE_FILE=".devops-exclusions" DEVOPS_REPO='git@github.com:os-climate/devops-toolkit.git' + HEAD_BRANCH=$(git rev-parse --abbrev-ref HEAD) # Content folder defines the files and folders to update FILES="$DEVOPS_DIR/content/files.txt" @@ -51,11 +55,22 @@ jobs: # Define functions + perform_folder_operation() { + FS_PATH="$1" + if [ -d "$DEVOPS_DIR"/"$FS_PATH" ]; then + echo "Scanning target folder content at: $FS_PATH" + return 0 + else + echo "Upstream folder NOT found: $FS_PATH [skipping]" + return 1 + fi + } + # Allows for selective opt-out components on a per-path basis perform_operation() { FS_PATH="$1" - if [ ! -f "$FS_PATH" ] && [ ! -d "$FS_PATH" ]; then - echo "Skipping missing upstream element at: $FS_PATH" + if [ ! -f "$DEVOPS_DIR"/"$FS_PATH" ]; then + echo "Skipping missing upstream file at: $FS_PATH" return 1 fi # Elements excluded from processing return exit status 1 @@ -73,17 +88,15 @@ jobs: fi } - # Decides whether a file should be updated + # Only updates file if it has changed selective_file_copy() { # Receives a single file path as argument - if (perform_operation "$1"); then - SHA_SRC=$(sha1sum $DEVOPS_DIR/"$1" | awk '{print $1}') - SHA_DST=$(sha1sum "$1" 2>/dev/null | awk '{print $1}' || :) - if [ "$SHA_SRC" != "$SHA_DST" ]; then - echo "Copying: $1" - cp "$DEVOPS_DIR/$1" "$1" - git add "$1" - fi + SHA_SRC=$(sha1sum "$DEVOPS_DIR"/"$1" | awk '{print $1}') + SHA_DST=$(sha1sum "$1" 2>/dev/null | awk '{print $1}' || :) + if [ "$SHA_SRC" != "$SHA_DST" ]; then + echo "Copying: $1" + cp "$DEVOPS_DIR/$1" "$1" + git add "$1" fi } @@ -111,7 +124,7 @@ jobs: fi } - check_authors() { + check_prs() { # Define users to check for pre-existing pull requests AUTOMATION_USER="github-actions[bot]" if [[ -n ${GH_TOKEN+x} ]]; then @@ -135,10 +148,35 @@ jobs: echo "Failed to checkout pull request"; exit 1 fi done + return 1 + } + + # Check if script is running in GHA workflow + in_github() { + if [ -z ${GITHUB_RUN_ID+x} ]; then + echo "Script is NOT running in GitHub" + return 1 + else + echo "Script is running in GitHub" + return 0 + fi + } + + # Check if user is logged into GitHub + logged_in_github() { + if (gh auth status); then + echo "Logged in and authenticated to GitHb" + return 0 + else + echo "Not logged into GitHub, some script operations unavailable" + return 1 + fi } # Main script entry point + echo "Repository name and HEAD branch: $REPO_NAME [$HEAD_BRANCH]" + # Ensure working from top-level of GIT repository CURRENT_DIR=$(pwd) if [ "$REPO_DIR" != "$CURRENT_DIR" ]; then @@ -155,23 +193,6 @@ jobs: # git stash -q # fi - # Verify user is logged into GitHub - if (gh auth status); then - GH_AUTH="true" - else - echo "Not logged into GitHub, some script operations unavailable" - GH_AUTH="false" - fi - - if [ "$GH_AUTH" = "true" ] && ! (check_authors); then - # No existing open pull requests found for this repository - echo "Removing remote branch if it exists: $AUTOMATION_BRANCH" - git push origin --delete "$AUTOMATION_BRANCH" > /dev/null 2>&1 || : - git branch -D "$AUTOMATION_BRANCH" || : - echo "Creating local branch for automated update" - git checkout -b "$AUTOMATION_BRANCH" - fi - # Configure GIT environment only if NOT already configured # i.e. when running in a GitHub Actions workflow TEST=$(git config -l > /dev/null 2>&1) @@ -181,14 +202,26 @@ jobs: "41898282+github-actions[bot]@users.noreply.github.com" fi + + if ! (check_prs); then + # No existing open pull requests found for this repository + + # Remove remote branch if it exists + git push origin --delete "$AUTOMATION_BRANCH" > /dev/null 2>&1 || : + git branch -D "$AUTOMATION_BRANCH" || : + git checkout -b "$AUTOMATION_BRANCH" + else + # The -B flag swaps branch and creates it if NOT present + git checkout -B "$AUTOMATION_BRANCH" + fi + # Only if NOT running in GitHub # (checkout is otherwise performed by earlier steps) - if [ -z ${GITHUB_RUN_ID+x} ]; then + if ! (in_github); then # Remove any stale local copy of the upstream repository if [ -d "$DEVOPS_DIR" ]; then rm -Rf "$DEVOPS_DIR" fi - printf "Cloning DevOps repository into: %s" "$DEVOPS_DIR" if (git clone "$DEVOPS_REPO" "$DEVOPS_DIR" > /dev/null 2>&1); then echo " [success]" @@ -215,7 +248,7 @@ jobs: for FOLDER in "${LINE[@]}"; do # Check to see if this folder should be skipped - if (perform_operation "$FOLDER"); then + if (perform_folder_operation "$FOLDER"); then # If necessary, create target folder if [ ! -d "$FOLDER" ]; then echo "Creating target folder: $FOLDER" @@ -255,18 +288,21 @@ jobs: # Next step is only performed if running as GitHub Action if [[ -n ${GH_TOKEN+x} ]]; then - # Script is running in a GitHub Acsions workflow + # Script is running in a GitHub actions workflow # Set outputs for use by the next actions/steps + # shellcheck disable=SC2129 echo "changed=true" >> "$GITHUB_OUTPUT" echo "branchname=$AUTOMATION_BRANCH" >> "$GITHUB_OUTPUT" + echo "headbranch=$HEAD_BRANCH" >> "$GITHUB_OUTPUT" # Move to the next workflow step to raise the PR + git push --set-upstream origin "$AUTOMATION_BRANCH" exit 0 fi # If running shell code locally, continue to raise the PR # Reinstate exit on unbound variables - set -eu -o pipefail + set -euo pipefail git status if ! (git commit -as -S -m "Chore: Update DevOps tooling from central repository [skip ci]" \ @@ -275,32 +311,26 @@ jobs: else # Push branch to remote repository git push --set-upstream origin "$AUTOMATION_BRANCH" - - # ToDo: need to verify if we are running in a GHA - # gh pr create --head ModeSevenIndustrialSolutions/test-bootstrap --title \ - # "Chore: Pull DevOps tooling from upstream repository" \ - # --body 'Automated by a GitHub workflow: bootstrap.yaml' - - gh pr create --repo ModeSevenIndustrialSolutions/test-bootstrap \ - --head "$AUTOMATION_BRANCH" --title \ - "Chore: Pull DevOps tooling from upstream repository" \ + # Create PR request + gh pr create \ + --title "Chore: Pull DevOps tooling from upstream repository" \ --body 'Automated by a GitHub workflow: bootstrap.yaml' fi # echo "Unstashing unstaged changes, if any exist" # git stash pop -q || : - ### SHELL CODE END ### + #SHELLCODEEND - name: Create Pull Request if: steps.update-repository.outputs.changed == 'true' uses: peter-evans/create-pull-request@v6 - env: - GITHUB_TOKEN: ${{ github.token }} + # env: + # GITHUB_TOKEN: ${{ github.token }} with: # Note: Requires a specific/defined Personal Access Token token: ${{ secrets.ACTIONS_WORKFLOW }} commit-message: "Chore: Update DevOps tooling from central repository [skip ci]" signoff: "true" - base: main + base: ${{ steps.update-repository.outputs.headbranch }} branch: ${{ steps.update-repository.outputs.branchname }} delete-branch: true title: "Chore: Update DevOps tooling from central repository [skip ci]" diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 322e1d5..83afb31 100755 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -103,7 +103,7 @@ echo " $WGET_URL" echo "Extracting shell code from: $SOURCE_FILE" EXTRACT="false" while read -r LINE; do - if [ "$LINE" = "### SHELL CODE START ###" ]; then + if [ "$LINE" = "#SHELLCODESTART" ]; then EXTRACT="true" SHELL_SCRIPT=$(mktemp -t script-XXXXXXXX.sh) touch "$SHELL_SCRIPT" @@ -113,7 +113,7 @@ while read -r LINE; do fi if [ "$EXTRACT" = "true" ]; then echo "$LINE" >> "$SHELL_SCRIPT" - if [ "$LINE" = "### SHELL CODE END ###" ]; then + if [ "$LINE" = "#SHELLCODEEND" ]; then break fi fi diff --git a/setup.py b/setup.py deleted file mode 100644 index 4ca48a8..0000000 --- a/setup.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Package manifest for this template repo.""" - -from setuptools import find_packages, setup - -__version__ = "0.1.0" - -setup( - name="src", - packages=find_packages(), - version=__version__, - description="template for the team to use", - author="aicoe-aiops", - license="", - install_requires=["click", "python-dotenv>=0.5.1"], -) From 7bdb93550eeb91477873b365f63f77b362caa658 Mon Sep 17 00:00:00 2001 From: Matthew Watkins Date: Thu, 25 Apr 2024 15:23:12 +0100 Subject: [PATCH 2/3] Chore: Manually update bootstrap code Signed-off-by: Matthew Watkins --- .github/workflows/bootstrap.yaml | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/.github/workflows/bootstrap.yaml b/.github/workflows/bootstrap.yaml index 88b2bc8..41dac5a 100644 --- a/.github/workflows/bootstrap.yaml +++ b/.github/workflows/bootstrap.yaml @@ -193,24 +193,6 @@ jobs: # git stash -q # fi - # Verify user is logged into GitHub - if (gh auth status); then - GH_AUTH="true" - else - echo "Not logged into GitHub, some script operations unavailable" - GH_AUTH="false" - fi - - if [ "$GH_AUTH" = "true" ] && ! (check_authors); then - # No existing open pull requests found for this repository - echo "Removing remote branch if it exists: $AUTOMATION_BRANCH" - git push origin --delete "$AUTOMATION_BRANCH" > /dev/null 2>&1 || : - git branch -D "$AUTOMATION_BRANCH" || : - fi - - echo "Creating local branch for automated update" - git checkout -b "$AUTOMATION_BRANCH" - # Configure GIT environment only if NOT already configured # i.e. when running in a GitHub Actions workflow TEST=$(git config -l > /dev/null 2>&1) From f921c2212359650d79c88f2f5db5f18366f2c36a Mon Sep 17 00:00:00 2001 From: ModeSevenIndustrialSolutions <93649628+ModeSevenIndustrialSolutions@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:24:20 +0000 Subject: [PATCH 3/3] Chore: Update DevOps tooling from central repository [skip ci] Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .github/workflows/builds.yaml | 24 ++++++++++++++++++------ .pre-commit-config.yaml | 9 +++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.github/workflows/builds.yaml b/.github/workflows/builds.yaml index 1d157ae..09b1a5b 100644 --- a/.github/workflows/builds.yaml +++ b/.github/workflows/builds.yaml @@ -47,10 +47,10 @@ jobs: with: python-version: ${{ matrix.python-version }} - - name: "Install dependencies" - run: | - python -m pip install --upgrade pip - pip install tox tox-gh-actions + - name: "Setup PDM for build commands" + uses: pdm-project/setup-pdm@v4 + with: + python-version: ${{ matrix.python-version }} - name: "Tag for test release" # Delete all local tags, then create a synthetic tag for testing @@ -62,6 +62,18 @@ jobs: git checkout "tags/v${{ steps.setenv.outputs.vernum }}" grep version pyproject.toml - - name: "Build with TOX" + - name: "Performing build" run: | - tox -e build + PDM_CMD=$(which pdm) + if [ -f tox.ini ]; then + python -m pip install --upgrade pip + pip install tox tox-gh-actions + echo "Building with command: tox -e build" + tox -e build + elif [ -x "$PDM_CMD" ]; then + echo "Building with command: pdm build" + pdm build + else + echo "Building with command: python -m build" + python -m build + fi diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d53a717..b46ab8a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -80,7 +80,7 @@ repos: # args: ['--py37-plus'] - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.3.0 + rev: 24.4.0 hooks: - id: black - id: black-jupyter @@ -124,7 +124,12 @@ repos: rev: v1.35.1 hooks: - id: yamllint - args: [--strict] + args: [ + "-d", + "{rules: {line-length: {max: 120}}, + ignore-from-file: [.gitignore], + }", + ] - repo: https://github.com/Mateusz-Grzelinski/actionlint-py rev: v1.6.27.13