Skip to content

Commit

Permalink
Merge pull request #94 from pieces-app/test_ci
Browse files Browse the repository at this point in the history
CI Ready to rock
  • Loading branch information
nathan-courtney-pieces committed May 7, 2024
2 parents 5650219 + 297a51e commit 26d162e
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 33 deletions.
221 changes: 221 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
name: Build and Release CLI Agent

on:
workflow_call:
inputs:
deploy:
type: boolean
required: true
default: false
secrets:
access_token:
required: true
pypi_test_token:
required: false
pypi_token:
required: false
ssh_private_key:
required: true
credentials_json:
required: false

jobs:
build:
runs-on: macos-latest
steps:
### Checking out our Repo
- uses: actions/checkout@v3

### Setting up python
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: 3.9

### Getting the version from the git tag or the branch name if there is none
- name: Get the version
shell: bash
id: get_version
run: echo "VERSION=$(echo $GITHUB_REF | cut -d / -f 3)" >> $GITHUB_OUTPUT

### Generating our staging version number if we are not production
- name: Get staging version
shell: bash
id: staging_version
run: echo "STAGING_VERSION=$(/bin/bash staging_versioning.sh)" >> $GITHUB_OUTPUT
if: inputs.deploy == false

### Installing our dependencies
- name: Install dependencies
run: |
pip install poetry -U
poetry install
### Setting the version in the pyproject.toml for unix builds
- name: Set Version Shell Script
run: |
if [[ ${{ steps.get_version.outputs.VERSION }} =~ [0-9]+.[0-9]+.[0-9]+$ ]]
then
echo "This is a tagged build"
export RELEASE_VERSION='${{ steps.get_version.outputs.VERSION }}'
RELEASE_VERSION="${RELEASE_VERSION#v}"
echo $RELEASE_VERSION
poetry version $RELEASE_VERSION
else
echo "This is not a tagged build"
export STAGING_VERSION='${{ steps.staging_version.outputs.STAGING_VERSION }}'
STAGING_VERSION=${STAGING_VERSION%"-staging"}
poetry version $STAGING_VERSION
fi
### Building the Library
- name: Build library
run: poetry build

### Building Standalone Package
- name: Build stand alone
run: |
poetry run pyinstaller -c --onefile src/pieces/app.py --hidden-import=pydantic_core --name=pieces
### Writing our staging version to a file to be pulled in the last step to update our json in the cloud
- name: Write staging version to file
shell: bash
run: echo "${{ steps.staging_version.outputs.STAGING_VERSION }}" > staging_version$GITHUB_RUN_NUMBER.txt
if: ${{ inputs.deploy == false && inputs.beta == false }}

### Uploading our builds and version text files to be pulled down in the next job
- uses: actions/upload-artifact@v3
with:
path: dist/pieces*

- uses: actions/upload-artifact@v3
with:
name: output
path: dist/pieces*

### Uploading our staging version text file to be pulled down later
- uses: actions/upload-artifact@v3
with:
name: staging_version
path: "*.txt"
if: inputs.deploy == false



### Pushing the built packages to GCP and GitHub
push-build:
runs-on: ubuntu-latest
needs:
- build
steps:

### Pulling down the previously built plugins
- uses: actions/download-artifact@v3
with:
name: output

### Pulling down the staging version number to pass to gcp and GitHub releases
- uses: actions/download-artifact@v3
with:
name: staging_version
if: inputs.deploy == false

### What is in here??
- name: List
run: ls -la

### Authenticating with gcloud
- name: Authenticate with Google cloud
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.credentials_json }}
project_id: ${{ inputs.project_id }}
create_credentials_file: true

### Setting up gcloud cli
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v2'

### Verifying that we are good to go with gcloud
- name: 'Use gcloud CLI'
run: 'gcloud info'

### Getting either the git tag or branch name to be set in the json
- name: Get the version
id: get_version
run: echo "VERSION=$(echo $GITHUB_REF | cut -d / -f 3)" >> $GITHUB_OUTPUT

### Installing some machine deps
- name: Install Dependencies
run: sudo apt-get install jq -y

### This big, long if statement handles pushing the builds to the correct location, as well as setting the proper
### build info for the json files read by the server
- name: Upload Artifacts
run: |
GITHUB_RUN_NUMBER+="gha"
if [[ ${{ steps.get_version.outputs.VERSION }} =~ [0-9]+.[0-9]+.[0-9]+ ]]; then
echo "Git Tag Versioning"
export CHANNEL=production
export VERSION=${{ steps.get_version.outputs.VERSION }}
export BUCKET=app-releases-production
export production_json="$(curl https://storage.googleapis.com/app-releases-production/pieces_cli/production/latest.json -H "Accept: application/json")"
export reversed_production_json=$(echo $production_json | jq 'reverse')
export updated_production_json=$(echo $reversed_production_json | jq --arg number "$GITHUB_RUN_NUMBER" --arg channel "$CHANNEL" --arg branch "${{ steps.get_version.outputs.VERSION }}" --arg tag "$VERSION" --arg sha1 "${{ github.sha }}" '.[length] |= . + {channel: $channel, number: $number, branch: $branch, tag: $tag, sha1: $sha1}')
echo $updated_production_json | jq 'reverse' > latest.json
jq -n --arg number "$GITHUB_RUN_NUMBER" --arg channel "$CHANNEL" --arg branch "${{ steps.get_version.outputs.VERSION }}" --arg tag "$VERSION" --arg sha1 "${{ github.sha }}" '{channel: $channel, number: $number, branch: $branch, tag: $tag, sha1: $sha1}' > latest-single.json
else
echo "Staging Versioning"
export CHANNEL=staging
export VERSION=$(echo "$(set -- ./*.txt; echo "$1")" | xargs head -n 1)
export BUCKET=app-releases-staging
export staging_json="$(curl https://storage.googleapis.com/app-releases-staging/pieces_cli/staging/latest.json -H "Accept: application/json")"
export reversed_staging_json=$(echo $staging_json | jq 'reverse')
export updated_staging_json=$(echo $reversed_staging_json | jq --arg number "$GITHUB_RUN_NUMBER" --arg channel "$CHANNEL" --arg branch "${{ steps.get_version.outputs.VERSION }}" --arg tag "$VERSION" --arg sha1 "${{ github.sha }}" '.[length] |= . + {channel: $channel, number: $number, branch: $branch, tag: $tag, sha1: $sha1}')
echo $updated_staging_json | jq 'reverse' > latest.json
jq -n --arg number "$GITHUB_RUN_NUMBER" --arg channel "$CHANNEL" --arg branch "${{ steps.get_version.outputs.VERSION }}" --arg tag "$VERSION" --arg sha1 "${{ github.sha }}" '{channel: $channel, number: $number, branch: $branch, tag: $tag, sha1: $sha1}' > latest-single.json
fi
gsutil -h "Cache-Control: max-age=0" cp **.tar.gz gs://$BUCKET/pieces_cli/$GITHUB_RUN_NUMBER/pieces.tar.gz
gsutil -h "Cache-Control: max-age=0" cp **.whl gs://$BUCKET/pieces_cli/$GITHUB_RUN_NUMBER/pieces.tar.gz.whl
gsutil -h "Cache-Control: max-age=0" cp latest-single.json gs://$BUCKET/pieces_cli/$CHANNEL/latest-single.json
gsutil -h "Cache-Control: max-age=0" cp latest.json gs://$BUCKET/pieces_cli/$CHANNEL/latest.json
gsutil cp -r **.tar.gz gs://$BUCKET/pieces_cli/release/
gsutil cp -r **.whl gs://$BUCKET/pieces_cli/release/
echo "releasetag=$VERSION" >> $GITHUB_ENV
### Upload to GitHub releases pre-release
- name: Release to GitHub Releases
uses: softprops/action-gh-release@v1
if: inputs.deploy == false
with:
files: |
**.tar.gz
**.whl
fail_on_unmatched_files: true
name: ${{ env.releasetag }}
tag_name: ${{ env.releasetag }}
generate_release_notes: true
prerelease: true

### Upload to GitHub releases
- name: Release to GitHub Releases
uses: softprops/action-gh-release@v1
if: inputs.deploy == true
with:
files: |
**.tar.gz
**.whl
fail_on_unmatched_files: true
name: ${{ env.releasetag }}
tag_name: ${{ env.releasetag }}
generate_release_notes: true
prerelease: false

### Upload to test Pypi
- name: Upload Packages to PyPi for release
if: inputs.deploy == true
run: |-
mkdir dist
cp *.tar.gz *.whl ./dist/
python3 -m pip install --upgrade twine
python -m twine upload --username "__token__" --password "${{ secrets.pypi_token }}" dist/*
42 changes: 10 additions & 32 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,17 @@ name: Release

on:
push:
branches: [ main ]
tags:
- 'v*.*.*'
- '*.*.*'

jobs:
build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: 3.9

- name: Install dependencies
run: |
pip install poetry -U
poetry install
- name: Build library
run: poetry build

- name: Build stand alone
run: |
poetry run pyinstaller -c --onefile src/pieces/app.py --hidden-import=pydantic_core --name=pieces
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: dist/pieces*



uses: ./.github/workflows/build.yaml
with:
deploy: true
secrets:
access_token: ${{ secrets.GITHUB_TOKEN }}
pypi_token: ${{ secrets.PYPI_TOKEN }}
pypi_test_token: ${{ secrets.PYPI_TEST_TOKEN }}
ssh_private_key: ${{ secrets.PIECES_GITHUB_SSH }}
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
18 changes: 18 additions & 0 deletions .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Staging

on:
push:
branches:
- main

jobs:
build:
uses: ./.github/workflows/build.yaml
with:
deploy: false
secrets:
access_token: ${{ secrets.GITHUB_TOKEN }}
pypi_token: ${{ secrets.PYPI_TOKEN }}
pypi_test_token: ${{ secrets.PYPI_TEST_TOKEN }}
ssh_private_key: ${{ secrets.PIECES_GITHUB_SSH }}
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,5 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

52 changes: 52 additions & 0 deletions staging_versioning.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
#-------------------------------- Getting the Version Data -------------------------

# Get PROD JSON With GET Request
prod_json="$(curl https://storage.googleapis.com/app-releases-production/pieces_cli/production/latest-single.json -H "Accept: application/json")"

# Get First PROD JSON Object in an array, and get the "tag" value
prod_tag=$(echo "$prod_json" | jq '.tag')

# Get STAGING JSON With GET Request
staging_json="$(curl https://storage.googleapis.com/app-releases-staging/pieces_cli/staging/latest-single.json -H "Accept: application/json")"

# Get First JSON Object in an array, and get the "tag" value
staging_tag=$(echo "$staging_json" | jq '.tag')

# Remove "-staging" from tag to manipulate numbers
staging_tag=${staging_tag//"-staging"/}
#---------------------------------------------------------------------------

#------------------------------- Version Extraction ---------------------

prod_tag_pure_num=$(echo "$prod_tag" | sed 's/"//g')
prod_major=$(echo "$prod_tag_pure_num" | cut -d. -f1)
prod_minor=$(echo "$prod_tag_pure_num" | cut -d. -f2)
prod_patch=$(echo "$prod_tag_pure_num" | cut -d. -f3)

staging_tag_pure_num=$(echo "$staging_tag" | sed 's/"//g')
staging_major=$(echo "$staging_tag_pure_num" | cut -d. -f1)
staging_minor=$(echo "$staging_tag_pure_num" | cut -d. -f2)
staging_patch=$(echo "$staging_tag_pure_num" | cut -d. -f3)

#---------------------------------------------------------------------------------

#------------------------------- Staging Versioning Logic ------------------------

# If prod major or minor is higher than staging, update staging major and minor and increment patch to prod plus 1
if [ "$prod_major" -gt "$staging_major" ] || [ "$prod_minor" -gt "$staging_minor" ]
then
increment_patch=$(expr $prod_patch + 1)
final_version="$prod_major.$prod_minor.$increment_patch-staging"
# If prodMajor and prodMinor are the same as stagingMajor and stagingMinor, but prodPatch is greater than staging patch, stagingPatch will be prodPatch incremented by 1
elif [ "$prod_major" -eq "$staging_major" ] && [ "$prod_minor" -eq "$staging_minor" ] && [ "$prod_patch" -gt "$staging_patch" ]
then
increment_patch=$(expr $prod_patch + 1)
final_version="$prod_major.$prod_minor.$increment_patch-staging"
# If prod hasn't changed, increment staging patch by 1
else
increment_patch=$(expr $staging_patch + 1)
final_version="$prod_major.$prod_minor.$increment_patch-staging"
fi

echo $final_version

0 comments on commit 26d162e

Please sign in to comment.