From f033f07bb58dfa25a602ed0ccd0b081ebb19b926 Mon Sep 17 00:00:00 2001 From: Steven Schattenberg <122639296+steven-schattenberg-itential@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:28:47 -0400 Subject: [PATCH] Add files for pipeline to push to galaxy (#1) * Add files for pipeline to push to galaxy * Lint issues --- .github/workflows/ansible-lint.yml | 15 +++ .../workflows/publish_ansible_collection.yml | 64 +++++++++++++ .github/workflows/updateChangelog.yml | 30 ++++++ galaxy.yml | 15 +-- scripts/changelog.py | 92 +++++++++++++++++++ 5 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ansible-lint.yml create mode 100644 .github/workflows/publish_ansible_collection.yml create mode 100644 .github/workflows/updateChangelog.yml create mode 100755 scripts/changelog.py diff --git a/.github/workflows/ansible-lint.yml b/.github/workflows/ansible-lint.yml new file mode 100644 index 0000000..7d10c2e --- /dev/null +++ b/.github/workflows/ansible-lint.yml @@ -0,0 +1,15 @@ +# .github/workflows/ansible-lint.yml +name: ansible-lint +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] +jobs: + build: + name: Ansible Lint # Naming the build is important to use it as a status check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run ansible-lint + uses: ansible/ansible-lint@main # or version tag instead of 'main' diff --git a/.github/workflows/publish_ansible_collection.yml b/.github/workflows/publish_ansible_collection.yml new file mode 100644 index 0000000..7bd78a3 --- /dev/null +++ b/.github/workflows/publish_ansible_collection.yml @@ -0,0 +1,64 @@ +name: Publish Ansible Galaxy Collection + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + publish-collection: + runs-on: ubuntu-latest + steps: + - name: Checkout repo content + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ssh-key: ${{ secrets.DEPLOY_PRIVATE_KEY }} + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.8 + + - name: Install Ansible + run: pip install ansible + + - name: Determine Ansible Galaxy version + run: echo "GALAXY_VERSION=$(echo ${{ github.ref_name }} | cut -c2-)" >> $GITHUB_ENV + + - name: Update Ansible Galaxy version + run: | + git config user.name github-actions + git config user.email github-actions@github.com + git checkout main + echo "Updating version to ${{ env.GALAXY_VERSION }} in galaxy.yml" + sed -i 's/\(version:\) .*/\1 ${{ env.GALAXY_VERSION }}/g' galaxy.yml + git add galaxy.yml + + - name: Update changelog + run: | + python scripts/changelog.py > CHANGELOG.md + git add CHANGELOG.md + + - name: Commit changes + run: | + git commit -m "Update galaxy version and changelog for release ${{ env.GALAXY_VERSION }} [skip ci]" + git push origin HEAD:main + + - name: Update release tag + run: | + git pull + git tag -f ${{ github.ref_name }} + git push origin HEAD:main + git push origin -f ${{ github.ref_name }} + + - name: Build Ansible collection + run: ansible-galaxy collection build + + - name: Determine Ansible collection archive name + run: echo "ARTIFACT=itential-core-${{ env.GALAXY_VERSION }}.tar.gz" >> $GITHUB_ENV + + - name: Publish collection to Ansible Galaxy + env: + ANSIBLE_API_TOKEN: ${{ secrets.ANSIBLE_API_TOKEN }} + run: ansible-galaxy collection publish ${{ env.ARTIFACT }} --token $ANSIBLE_API_TOKEN diff --git a/.github/workflows/updateChangelog.yml b/.github/workflows/updateChangelog.yml new file mode 100644 index 0000000..3ad3425 --- /dev/null +++ b/.github/workflows/updateChangelog.yml @@ -0,0 +1,30 @@ +name: Run changelog Script + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + updateChangelog: + runs-on: ubuntu-latest + steps: + - name: checkout repo content + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: setup python + uses: actions/setup-python@v5 + with: + python-version: 3.8 #install the python needed + - name: Install dependencies + run: | + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: execute py script + run: | + python scripts/changelog.py > CHANGELOG.md + git config user.name github-actions + git config user.email github-actions@github.com + git add . + git commit -m "Update CHANGELOG.md" + git push origin HEAD:main diff --git a/galaxy.yml b/galaxy.yml index 78889b9..8a2e50d 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -17,7 +17,7 @@ readme: README.md # A list of the collection's content authors. Can be just the name or in the format 'Full Name (url) # @nicks:irc/im.site#channel' authors: -- Itential + - Itential ### OPTIONAL but strongly recommended # A short summary description of the collection @@ -29,19 +29,20 @@ description: | # Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only # accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file' license: -- GPL-3.0-or-later + - GPL-3.0-or-later # The path to the license file for the collection. This path is relative to the root of the collection. This key is # mutually exclusive with 'license' -#license_file: '' +# license_file: '' # A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character # requirements as 'namespace' and 'name' tags: -- itential -- automation -- gateway -- core + - itential + - automation + - gateway + - core + - tools # Collections that this collection requires to be installed for it to be usable. The key of the dict is the # collection label 'namespace.name'. The value is a version range diff --git a/scripts/changelog.py b/scripts/changelog.py new file mode 100755 index 0000000..3d9c4d9 --- /dev/null +++ b/scripts/changelog.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 + +"""changelog +This script outputs a changelog markdown to stdout. +""" + +import subprocess +import re + +# Get all the tags for the project +git_tag_result = subprocess.run( + ["git", "tag"], capture_output=True, text=True +) + +# Make a tag list +tags = git_tag_result.stdout.split("\n") + +# Remove v from tags +for i in range(len(tags)): + tags[i] = tags[i].replace('v','') + +# Remove empty strings from tags +tags[:] = [x for x in tags if x] + +# Sort tags by semver number +tags.sort(key = lambda x: [int(y) for y in x.split('.')]) + +# Add v back into tags +for i in range(len(tags)): + tags[i] = 'v'+tags[i] + +# Iterate through all the tags in the tag list and get a list of commits +count = 0 +changelogs = {} +for tag in tags: + if tag: + if count == 0: + # If this is the first tag, get a list of the commits up to when the tag was created. + git_log_result = subprocess.getoutput( + f'git log --pretty=oneline {tag} | grep -v Merge | cut -d " " -f 2-' + ) + else: + # For subsequents tags, get a list of the commits since the previous tag. + git_log_result = subprocess.getoutput( + f'git log --pretty=oneline {tag}...{prev_tag} | grep -v Merge | cut -d " " -f 2-' + ) + + # Convert the commit list to a set and then back to a list to remove any duplicate commits. + git_logs = list(set(git_log_result.split('\n'))) + + # Sort git_logs so they are in the same order each time + git_logs.sort() + + # Add links to pull requests + for i in range(len(git_logs)): + if re.search(r'\(\#\d*\)',git_logs[i]): + test = re.search(r'\(\#\d*\)',git_logs[i]) + num = test.group() + num = num[2:] + num = num[:-1] + sub = ' https://github.com/itential/itential.core/pull/' + num + git_logs[i] = re.sub('\(\#\d*\)',sub,git_logs[i]) + + # Add the commits to the changelogs dictionary using the tag as the key and the + # commit list as the value + changelogs[tag] = git_logs + + # Save the tag as the previous tag and increment the counter + prev_tag = tag + count = count + 1 + +# Create the changelog markdown output +print('# Changelog\n') +for release,changes in reversed(changelogs.items()): + # Get the tag date in the date format we want + release_date = subprocess.getoutput( + f'git log -1 --format=%ad --date=format:"%B %d, %Y" {release}' + ) + + # Print the tag (release) and the date it was created + print(f'## {release} ({release_date})\n') + + # Print an unordered list of the commits (changes) + for change in changes: + print(f'* {change}') + print() + + for i in range(len(tags)): + if i > 0: + if release == tags[i]: + full = 'https://github.com/itential/itential.core/compare/' + tags[i-1] + '...' + release + print('Full Changelog:', full, '\n\n')