# GitHub Action

> Push new source texts to Lefine from CI, fail a pull request on missing or broken translations, and get the latest translations back as a pull request.

The Lefine Strings action runs the `lefine` CLI in your workflow. In `push` mode it sends new and changed source texts to Lefine. In `check` mode it fails the build when keys or translations are missing, or a text lost a variable. In `pull` mode it downloads the published translations and opens a pull request with them.

Use the action **or** the [GitHub App](https://lefine.ai/en/docs/strings/connect), not both. The App syncs from the dashboard and needs no workflow. The action is for teams who keep everything in CI, or cannot install an App.

## Set it up

1. In Lefine, open **Settings → API keys** and create a key with **Read and write** on Strings.
2. In GitHub, open the repo's **Settings → Secrets and variables → Actions** and add it as `LEFINE_API_KEY`.
3. Add the workflow below as `.github/workflows/lefine.yml`.

```yaml
name: Lefine strings

on:
  push:
    branches: [main]
    paths: ["locales/en.json"]
  schedule:
    - cron: "0 6 * * *" # every day at 06:00 UTC
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  push:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: lefinehq/oss/github-action@main
        with:
          mode: push
          app: acme/web
          api-key: ${{ secrets.LEFINE_API_KEY }}

  pull:
    if: github.event_name != 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: lefinehq/oss/github-action@main
        with:
          mode: pull
          app: acme/web
          output: locales
```

`app` is your workspace and app, `workspace/app`. Leave it out when the repo has the `lefine.ts` that `npx lefine init` wrote.

## Push

`push` reads your source file, `{ "namespace": { "key": "text" } }`, and runs `lefine strings push --yes`. New keys and changed texts go to Lefine. Nothing is published: a person still publishes in Lefine, or you add `lefine strings publish` later.

| Input | Default |
|---|---|
| `source-file` | `<output>/<default locale>.json` from your config |
| `source-locale` | the default locale from your config |
| `working-directory` | `.` (the folder with `lefine.ts` or `i18n.config.ts`) |

Outputs: `created`, `updated`, and `skipped` (`true` when Lefine already had every text).

### On pull requests

You can also run `push` on `pull_request`. Then texts from a branch reach Lefine before the merge, so translators start early. The cost: a pull request that is closed without a merge leaves its keys in Lefine.

A pull request from a fork, or one Dependabot opened, gets no secrets from GitHub. The action sees no key, writes a notice and passes. On your own branches a missing key fails the step and says which secret to add.

## Check

`check` fails the step when your strings have a problem. It asks the CLI and Lefine, and adds no rules of its own:

| Problem | Where the answer comes from | Output |
|---|---|---|
| A name in your code has no key in Lefine, so users see the raw name | `lefine strings missing` | `missing-keys` |
| A language serves fewer texts than the source in the next release | the release preview (`lefine strings status`) | `untranslated` |
| A text lost or added a variable or a plural branch, against its source | the same warnings the **Releases** tab shows | `text-warnings` |

```yaml
on: pull_request

jobs:
  strings:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: lefinehq/oss/github-action@main
        with:
          mode: check
          app: acme/web
          api-key: ${{ secrets.LEFINE_API_KEY }}
          cli-version: 0.2.0 # check needs 0.2.0 or newer
          fail-on: missing-keys,text-warnings
```

- **`fail-on`** picks what fails the step: `missing-keys`, `untranslated`, `text-warnings`, all three by default, or `none` to only report.
- **Each text warning is an annotation** on the run, for example `de common.save: lost {name}`. The full list is in the job summary.
- **Text warnings cover texts waiting to be published**, not texts already live: the same scope as the Releases tab.
- The key needs **Read** only. A pull request from a fork gets no secrets, so the check is skipped there with a notice.
- `strings missing` scans the code in `working-directory`, so check out the code first.
- **`check` needs `@lefine/cli` 0.2.0 or newer.** The default pin, `0.1.0`, has no `strings missing`, and the step says so if you keep it.

## Pull

`pull` runs `lefine strings pull` and commits the changed files to the `lefine/strings` branch. Then it opens a pull request into the branch the workflow runs on, or updates the one that is already open. It reads the public CDN, so it needs no Lefine key.

- **The source file is not touched.** Your `en.json` stays as it is in the repo, so a pull request never takes back a text you pushed but did not publish. Set `include-source: true` to commit it too.
- **The action owns `lefine/strings`** and force-pushes it on every run. Edit texts in Lefine, not on that branch.
- **CI does not run on that pull request** when it is opened with the default `GITHUB_TOKEN`; GitHub does not start workflows from it. Pass a personal access token or a GitHub App token as `github-token`, and the same token to `actions/checkout`, to get checks on it.

| Input | Default |
|---|---|
| `output` | your config's `pull.output`, else `./locales` |
| `branch` | `lefine/strings` |
| `base` | the branch the workflow runs on |
| `pr-title` | `Lefine: latest translations` |
| `source-locale` | `en`, the file the pull request leaves alone |

Outputs: `changed-files`, `pr-url`, and `skipped` (`true` when no translation changed).

## Every input

| Input | What it does |
|---|---|
| `mode` | `push` (default), `check` or `pull` |
| `api-key` | The key; needed for `push` and `check` |
| `cli-version` | The `@lefine/cli` version to run. Pinned to `0.1.0`, so a new CLI release never changes your CI without you |
| `api-url`, `cdn-url` | Another Lefine API or CDN origin |

The action needs Node 20 on the runner. GitHub's Ubuntu and macOS runners have it.

Source: https://lefine.ai/en/docs/strings/github-action/
