From 46a704ff4d09d534c404434f0de4cc2f236cbc21 Mon Sep 17 00:00:00 2001 From: Arash Date: Fri, 14 Jun 2024 12:16:15 +0200 Subject: [PATCH 1/2] Using composite action --- .github/actions/galaxy-social/action.yml | 107 +++++++++++++++++++++++ .github/workflows/preview.yml | 44 +++------- .github/workflows/publish_content.yml | 50 ++--------- plugins.yml | 4 +- 4 files changed, 128 insertions(+), 77 deletions(-) create mode 100644 .github/actions/galaxy-social/action.yml diff --git a/.github/actions/galaxy-social/action.yml b/.github/actions/galaxy-social/action.yml new file mode 100644 index 0000000..69b2425 --- /dev/null +++ b/.github/actions/galaxy-social/action.yml @@ -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 diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 880b8a0..bed5391 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -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 }} diff --git a/.github/workflows/publish_content.yml b/.github/workflows/publish_content.yml index 9af8f3c..9314eb0 100644 --- a/.github/workflows/publish_content.yml +++ b/.github/workflows/publish_content.yml @@ -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 @@ -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 }} diff --git a/plugins.yml b/plugins.yml index 689073d..14826ec 100644 --- a/plugins.yml +++ b/plugins.yml @@ -12,7 +12,7 @@ plugins: enabled: false config: base_url: "https://bsky.social" - username: $BLUESKY_USERNAME + username: "" password: $BLUESKY_PASSWORD max_content_length: 300 @@ -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 From 7c8100422ceeb1abfb9db4bc24c751ce8c89d54d Mon Sep 17 00:00:00 2001 From: Arash Date: Fri, 14 Jun 2024 16:30:41 +0200 Subject: [PATCH 2/2] Set env variables outside composite action --- .github/actions/galaxy-social/action.yml | 42 ++---------------------- .github/workflows/preview.yml | 18 +++++----- .github/workflows/publish_content.yml | 19 ++++++----- 3 files changed, 23 insertions(+), 56 deletions(-) diff --git a/.github/actions/galaxy-social/action.yml b/.github/actions/galaxy-social/action.yml index 69b2425..dbe3025 100644 --- a/.github/actions/galaxy-social/action.yml +++ b/.github/actions/galaxy-social/action.yml @@ -9,32 +9,12 @@ inputs: 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: checkout #This is needed to avoid error on post action + uses: actions/checkout@v4 + - name: Get changed files in posts folder id: get_changed_files uses: tj-actions/changed-files@v44 @@ -73,19 +53,6 @@ runs: 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 @@ -102,6 +69,3 @@ runs: with: file_pattern: "processed_files.json" branch: "processed_files" - - - name: checkout main - uses: actions/checkout@v4 diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index bed5391..0872219 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -18,15 +18,17 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - name: Set environment variables + run: | + echo "PR_NUMBER=${{ github.event.number }}" >> $GITHUB_ENV + echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV + echo "MASTODON_ACCESS_TOKEN=${{ secrets.MASTODON_ACCESS_TOKEN }}" >> $GITHUB_ENV + echo "BLUESKY_PASSWORD=${{ secrets.BLUESKY_PASSWORD }}" >> $GITHUB_ENV + echo "MATRIX_ACCESS_TOKEN=${{ secrets.MATRIX_ACCESS_TOKEN }}" >> $GITHUB_ENV + echo "SLACK_ACCESS_TOKEN=${{ secrets.SLACK_ACCESS_TOKEN }}" >> $GITHUB_ENV + echo "MASTODON_EU_FR_TOKEN=${{ secrets.MASTODON_EU_FR_TOKEN }}" >> $GITHUB_ENV + - name: Run composite action - id: composite_action uses: ./.github/actions/galaxy-social with: preview: "true" - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.number }} - MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }} - BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }} - MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }} - SLACK_ACCESS_TOKEN: ${{ secrets.SLACK_ACCESS_TOKEN }} - MASTODON_EU_FR_TOKEN: ${{ secrets.MASTODON_EU_FR_TOKEN }} diff --git a/.github/workflows/publish_content.yml b/.github/workflows/publish_content.yml index 9314eb0..bc8617f 100644 --- a/.github/workflows/publish_content.yml +++ b/.github/workflows/publish_content.yml @@ -19,14 +19,15 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - name: Set environment variables + run: | + echo "PR_NUMBER=${{ github.event.number }}" >> $GITHUB_ENV + echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV + echo "MASTODON_ACCESS_TOKEN=${{ secrets.MASTODON_ACCESS_TOKEN }}" >> $GITHUB_ENV + echo "BLUESKY_PASSWORD=${{ secrets.BLUESKY_PASSWORD }}" >> $GITHUB_ENV + echo "MATRIX_ACCESS_TOKEN=${{ secrets.MATRIX_ACCESS_TOKEN }}" >> $GITHUB_ENV + echo "SLACK_ACCESS_TOKEN=${{ secrets.SLACK_ACCESS_TOKEN }}" >> $GITHUB_ENV + echo "MASTODON_EU_FR_TOKEN=${{ secrets.MASTODON_EU_FR_TOKEN }}" >> $GITHUB_ENV + - name: Run composite action - id: composite_action uses: ./.github/actions/galaxy-social - with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.number }} - MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }} - BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }} - MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }} - SLACK_ACCESS_TOKEN: ${{ secrets.SLACK_ACCESS_TOKEN }} - MASTODON_EU_FR_TOKEN: ${{ secrets.MASTODON_EU_FR_TOKEN }}