Skip to content

auto - [zh](云计算工程师) #289

auto - [zh](云计算工程师)

auto - [zh](云计算工程师) #289

name: Articles auto translate
run-name: ${{ github.event.label.name }} - ${{ github.event.issue.title }}
on:
issues:
types:
- labeled
jobs:
ArticlesAutoTranslate:
runs-on: ubuntu-latest
env:
ACTIONS_RUNNER_DEBUG: true
permissions:
issues: write
contents: write
# Check if the label is 'auto'. If not, skip the entire job
if: ${{ github.event.label.name == 'auto' }}
steps:
# Check if the issue body is empty. If so, exit the action
- name: Check if the description of the issue is empty
if: ${{ !github.event.issue.body }}
shell: bash
run: echo "The description of the issue is empty." 1>&2; exit 1;
- name: Extract language code (e.g., [zh])
id: extract-lang
run: |
ISSUE_TITLE="${{ github.event.issue.title }}"
LANG_CODE=$(echo "$ISSUE_TITLE" | sed -E 's/^\[([a-zA-Z]+)\].*/\1/')
echo "::set-output name=lang::$LANG_CODE"
# Initialize git parameters
- uses: actions/checkout@v3
- name: Config git
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git config pull.rebase true
# Crawl new articles
- name: Process main branch
run: |
git checkout main
if [ -d "./articles/_tmp/" ]; then
rm -rf ./articles/_tmp/
fi
- name: Crawl pages and generate Markdown files
id: fetch-webpage-to-markdown
continue-on-error: true
uses: freecodecamp/article-webpage-to-markdown-action@dev
with:
newsLink: '${{ github.event.issue.Body }}'
includeSelector: 'span.author-card-name,section.post-content'
ignoreSelector: '.ad-wrapper'
skipSameArticleCheck: true
skipIssueComment: true
markDownFilePath: './articles/_tmp/'
githubToken: '${{ github.token }}'
- name: Push new article to repo
run: |
cp -f ${{ steps.fetch-webpage-to-markdown.outputs.markdown_file_path }} ./articles/${{ steps.extract-lang.outputs.lang }}/
cp -f ${{ steps.fetch-webpage-to-markdown.outputs.markdown_file_path }} ./articles/_raw/
git add articles/_tmp/*.md
git add articles/_raw/*.md
git add articles/${{ steps.extract-lang.outputs.lang }}/*.md
git commit -m 'Save article markdown.'
git push origin main
# Checkout to a new branch
- name: Git checkout auto-translate
run: |
git fetch
git checkout -b auto-translate origin/auto-translate
git merge --strategy=recursive --strategy-option=theirs --allow-unrelated-histories main
- name: Articles auto translate
uses: freeCodeCamp/articles-auto-translate-action@main
with:
with_issue_title: '${{ github.event.issue.title }}'
with_issue_body: '${{ github.event.issue.Body }}'
with_label_name: '${{ github.event.label.name }}'
with_github_token: '${{ github.token }}'
with_orginal_markdown_file_path: ${{ steps.fetch-webpage-to-markdown.outputs.markdown_file_path }}
with_task_fetch_to_save_path: './articles/_raw/'
with_task_fetch_to_include_selector: '.author-card-name,.post-content'
with_task_fetch_to_ignore_selector: '.ad-wrapper'
with_task_translate_openai_api_key: ${{ secrets.OPENAI_API_KEY }}
with_task_translate_to_save_path: './articles/{lang}/'
# Commit crawled article and auto-translated article to remote
- name: Commit raw article and auto-translated-article
run: |
if [ -d "./articles/_tmp/" ]; then
rm -rf ./articles/_tmp/
fi
git add ./articles/_tmp/*
git add ./articles/_raw/*.md
git add ./articles/${{ steps.extract-lang.outputs.lang }}/*.md
git commit -m 'Save auto-translated article markdown.'
git push origin auto-translate
# Remove _tmp of main
- name: rm _tmp
run: |
git checkout main
if [ -d "./articles/_tmp/" ]; then
rm -rf ./articles/_tmp/
fi
git add ./articles/_tmp/*
git commit -m 'rm _tmp.'
git push origin main