Skip to content

Commit

Permalink
Using composite action
Browse files Browse the repository at this point in the history
  • Loading branch information
arash77 committed Jun 14, 2024
1 parent e874d66 commit 46a704f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 77 deletions.
107 changes: 107 additions & 0 deletions .github/actions/galaxy-social/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: Galaxy Social composite action
description: Perform common steps for getting changed files, setting up the environment, and running the main logic.
inputs:
files:
description: "File patterns to check for changes"
default: "posts/**"

preview:
description: "Whether to create a preview or not"
default: "false"

GITHUB_TOKEN:
description: "GitHub token"
required: true

PR_NUMBER:
description: "Pull Request number"
required: true

MASTODON_ACCESS_TOKEN:
description: "Mastodon access token"

BLUESKY_PASSWORD:
description: "Bluesky password"

MATRIX_ACCESS_TOKEN:
description: "Matrix access token"

SLACK_ACCESS_TOKEN:
description: "Slack access token"

MASTODON_EU_FR_TOKEN:
description: "Mastodon EU FR token"

runs:
using: "composite"
steps:
- name: Get changed files in posts folder
id: get_changed_files
uses: tj-actions/changed-files@v44
with:
files: ${{ inputs.files }}
json: "true"

- name: get published files cache
if: steps.get_changed_files.outputs.any_changed == 'true'
shell: bash
run: |
if ! git ls-remote --heads origin | grep -q "refs/heads/processed_files"; then
git checkout --orphan processed_files
git rm -rf .
echo "{}" > processed_files.json
git add processed_files.json
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git commit -m "Initialize processed_files branch with processed_files.json"
git push origin processed_files
git checkout main
fi
git fetch origin processed_files:processed_files
git checkout processed_files -- processed_files.json
- name: Set up Python
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Install dependencies
if: steps.get_changed_files.outputs.any_changed == 'true'
shell: bash
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Create env variables
if: steps.get_changed_files.outputs.any_changed == 'true'
shell: bash
run: |
echo "CHANGED_FILES=${{ steps.get_changed_files.outputs.all_changed_files }}" >> $GITHUB_ENV
echo "MASTODON_ACCESS_TOKEN=${{ inputs.MASTODON_ACCESS_TOKEN }}" >> $GITHUB_ENV
echo "BLUESKY_PASSWORD=${{ inputs.BLUESKY_PASSWORD }}" >> $GITHUB_ENV
echo "MATRIX_ACCESS_TOKEN=${{ inputs.MATRIX_ACCESS_TOKEN }}" >> $GITHUB_ENV
echo "SLACK_ACCESS_TOKEN=${{ inputs.SLACK_ACCESS_TOKEN }}" >> $GITHUB_ENV
echo "MASTODON_EU_FR_TOKEN=${{ inputs.MASTODON_EU_FR_TOKEN }}" >> $GITHUB_ENV
echo "GITHUB_TOKEN=${{ inputs.GITHUB_TOKEN }}" >> $GITHUB_ENV
echo "PR_NUMBER=${{ inputs.PR_NUMBER }}" >> $GITHUB_ENV
- name: Run script to create preview
if: steps.get_changed_files.outputs.any_changed == 'true' && inputs.preview == 'true'
shell: bash
run: python -u github_run.py --preview

- name: Run script to publish contents
if: steps.get_changed_files.outputs.any_changed == 'true' && inputs.preview == 'false'
shell: bash
run: python -u github_run.py --json-out processed_files.json

- name: Commit changes
if: steps.get_changed_files.outputs.any_changed == 'true' && inputs.preview == 'false'
uses: stefanzweifel/git-auto-commit-action@v5
with:
file_pattern: "processed_files.json"
branch: "processed_files"

- name: checkout main
uses: actions/checkout@v4
44 changes: 10 additions & 34 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,29 @@ on:
pull_request_target:
branches: [main]
types: [opened, synchronize, reopened]
paths:
- "posts/**"

jobs:
preview:
name: Preview
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Get changed files in posts folder
id: get_changed_files
uses: tj-actions/changed-files@v44
- name: Run composite action
id: composite_action
uses: ./.github/actions/galaxy-social
with:
files: posts/**
json: "true"

- name: get published files cache
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
git fetch origin processed_files:processed_files
git checkout processed_files -- processed_files.json
- name: Set up Python
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install dependencies
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Run script to create preview
if: steps.get_changed_files.outputs.any_changed == 'true'
env:
preview: "true"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
BLUESKY_USERNAME: ${{ secrets.BLUESKY_USERNAME }}
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
MASTODON_EU_FR_TOKEN: ${{ secrets.MASTODON_EU_FR_TOKEN }}
MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }}
SLACK_ACCESS_TOKEN: ${{ secrets.SLACK_ACCESS_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
CHANGED_FILES: ${{ steps.get_changed_files.outputs.all_changed_files }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
run: python -u github_run.py --preview
MASTODON_EU_FR_TOKEN: ${{ secrets.MASTODON_EU_FR_TOKEN }}
50 changes: 9 additions & 41 deletions .github/workflows/publish_content.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ on:
pull_request_target:
branches: [main]
types: [closed]
paths:
- "posts/**"

jobs:
publish:
if: github.event.pull_request.merged == true
name: Publish content
runs-on: ubuntu-latest
permissions:
contents: write
Expand All @@ -16,49 +19,14 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Get changed files in posts folder
id: get_changed_files
uses: tj-actions/changed-files@v44
- name: Run composite action
id: composite_action
uses: ./.github/actions/galaxy-social
with:
files: posts/**
json: "true"

- name: get published files cache
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
git fetch origin processed_files:processed_files
git checkout processed_files -- processed_files.json
- name: Set up Python
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install dependencies
if: steps.get_changed_files.outputs.any_changed == 'true'
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Run script to publish contents
if: steps.get_changed_files.outputs.any_changed == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
BLUESKY_USERNAME: ${{ secrets.BLUESKY_USERNAME }}
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
MASTODON_EU_FR_TOKEN: ${{ secrets.MASTODON_EU_FR_TOKEN }}
MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }}
SLACK_ACCESS_TOKEN: ${{ secrets.SLACK_ACCESS_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
CHANGED_FILES: ${{ steps.get_changed_files.outputs.all_changed_files }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
run: python -u github_run.py --json-out processed_files.json

- name: Commit changes
if: steps.get_changed_files.outputs.any_changed == 'true'
uses: stefanzweifel/git-auto-commit-action@v5
with:
file_pattern: "processed_files.json"
branch: "processed_files"
MASTODON_EU_FR_TOKEN: ${{ secrets.MASTODON_EU_FR_TOKEN }}
4 changes: 2 additions & 2 deletions plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins:
enabled: false
config:
base_url: "https://bsky.social"
username: $BLUESKY_USERNAME
username: ""
password: $BLUESKY_PASSWORD
max_content_length: 300

Expand All @@ -37,7 +37,7 @@ plugins:
enabled: false
config:
access_token: $SLACK_ACCESS_TOKEN
channel_id: $SLACK_CHANNEL_ID
channel_id: ""
max_content_length: 40000

- name: markdown
Expand Down

0 comments on commit 46a704f

Please sign in to comment.