|
| 1 | +--- |
| 2 | +title: Running Okteto Test in CI |
| 3 | +description: Run your Okteto Test suites automatically in a GitHub Actions pipeline against a Preview Environment. |
| 4 | +sidebar_label: Running Tests in CI |
| 5 | +id: run-tests-in-ci |
| 6 | +--- |
| 7 | + |
| 8 | +Okteto Test runs your Test Containers in a Continuous Integration (CI) pipeline against the same production-like Kubernetes environment your code runs in. This guide shows you how to run the `test` section of your Okteto Manifest on every pull request with GitHub Actions, alongside a Preview Environment, and export the test results as build artifacts. |
| 9 | + |
| 10 | +Running your tests in CI with Okteto reuses the `test` section you already run locally with `okteto test`, so tests behave the same in your inner development loop and in your pipeline. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- An Okteto instance and an [Okteto Manifest](reference/okteto-manifest.mdx) with a [`test` section](reference/okteto-manifest.mdx#test-object-optional) that defines your Test Containers. If you haven't defined one yet, follow the [Getting started with Okteto Test](testing/getting-started-test.mdx) guide. |
| 15 | +- A GitHub repository for your application. |
| 16 | +- An Okteto [Admin Access Token](admin/dashboard.mdx#admin-access-tokens). |
| 17 | + |
| 18 | +This guide uses the [movies rental application](https://github.com/okteto/movies) as the sample application. |
| 19 | + |
| 20 | +## Step 1: Store your Okteto credentials as secrets |
| 21 | + |
| 22 | +The workflow authenticates to your Okteto instance with the [`okteto/context`](https://github.com/marketplace/actions/okteto-context) action, which reads two values from your repository secrets. Storing them as secrets keeps them out of your workflow file. |
| 23 | + |
| 24 | +Create the following secrets in your GitHub repository under **Settings → Secrets and variables → Actions**: |
| 25 | + |
| 26 | +- `OKTETO_TOKEN`: your Okteto [Admin Access Token](admin/dashboard.mdx#admin-access-tokens). |
| 27 | +- `OKTETO_CONTEXT`: the URL of your Okteto instance, for example `https://okteto.example.com`. |
| 28 | + |
| 29 | +For step-by-step instructions on adding secrets, see [Using secrets in GitHub Actions](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). |
| 30 | + |
| 31 | +## Step 2: Add the Test action to your workflow |
| 32 | + |
| 33 | +The [`okteto/test`](https://github.com/marketplace/actions/okteto-test) action runs the Test Containers defined in your Okteto Manifest. Run it after the [`okteto/deploy-preview`](https://github.com/marketplace/actions/deploy-preview-environment) action so your tests execute against a live Preview Environment created for the pull request. |
| 34 | + |
| 35 | +Create a `.github/workflows` folder in the root of your repository and add the following workflow file: |
| 36 | + |
| 37 | +```yaml |
| 38 | +# file: .github/workflows/preview.yaml |
| 39 | +on: |
| 40 | + pull_request: |
| 41 | + branches: |
| 42 | + - main |
| 43 | + |
| 44 | +concurrency: |
| 45 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 46 | + cancel-in-progress: false |
| 47 | + |
| 48 | +jobs: |
| 49 | + test: |
| 50 | + runs-on: ubuntu-latest |
| 51 | + steps: |
| 52 | + - name: Checkout code |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + - name: Context |
| 56 | + uses: okteto/context@latest |
| 57 | + with: |
| 58 | + url: ${{ secrets.OKTETO_CONTEXT }} |
| 59 | + token: ${{ secrets.OKTETO_TOKEN }} |
| 60 | + |
| 61 | + - name: Deploy Preview Environment |
| 62 | + uses: okteto/deploy-preview@latest |
| 63 | + env: |
| 64 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + with: |
| 66 | + name: pr-${{ github.event.number }} |
| 67 | + timeout: 15m |
| 68 | + |
| 69 | + - name: Run tests |
| 70 | + uses: okteto/test@latest |
| 71 | + with: |
| 72 | + namespace: pr-${{ github.event.number }} |
| 73 | + tests: e2e |
| 74 | +``` |
| 75 | +
|
| 76 | +The `namespace` input points the `test` action at the Preview Environment deployed in the previous step, so your Test Containers run in the same Namespace as the application under test. The `tests` input names the Test Containers to run, matching the keys in the `test` section of your Okteto Manifest. Separate multiple names with spaces, or omit the input to run every Test Container. |
| 77 | + |
| 78 | +The `test` action accepts these inputs: |
| 79 | + |
| 80 | +| Input | Required | Default | Description | |
| 81 | +|-------|----------|---------|-------------| |
| 82 | +| `tests` | No | (all) | Test Containers to run, separated by spaces. Runs all Test Containers if omitted. | |
| 83 | +| `namespace` | No | current context | Namespace to run the tests in. Set this to the Preview Environment Namespace. | |
| 84 | +| `name` | No | (none) | Name of the Development Environment. | |
| 85 | +| `file` | No | default path | Path to the Okteto Manifest. | |
| 86 | +| `deploy` | No | `false` | Deploy the Development Environment before testing, even if it already exists. | |
| 87 | +| `no-cache` | No | `false` | Ignore the Test Container `caches` for this run. | |
| 88 | +| `variables` | No | (none) | Variables passed to the tests, separated by commas. | |
| 89 | +| `timeout` | No | `5m` | How long to wait for the tests to finish, for example `10m`. | |
| 90 | +| `log-level` | No | (none) | Log verbosity: `debug`, `info`, `warn`, or `error`. | |
| 91 | + |
| 92 | +:::warning |
| 93 | +Set `cancel-in-progress: false` in the `concurrency` block. Canceling an in-progress deployment can leave the Preview Environment in an inconsistent state and leak resources in your cluster. |
| 94 | +::: |
| 95 | + |
| 96 | +## Step 3: Export test artifacts |
| 97 | + |
| 98 | +Add the [`artifacts`](reference/okteto-manifest.mdx#test-object-optional) field to a Test Container to export files, such as reports and screenshots, after the tests run. The following `e2e` Test Container exports a Playwright report and its results: |
| 99 | + |
| 100 | +```yaml title="okteto.yaml" |
| 101 | +test: |
| 102 | + e2e: |
| 103 | + image: ghcr.io/okteto/playwright:chromium |
| 104 | + context: e2e |
| 105 | + caches: |
| 106 | + - yarn/.cache |
| 107 | + - node_modules |
| 108 | + commands: |
| 109 | + - bash run_tests.sh |
| 110 | + artifacts: |
| 111 | + - test-results |
| 112 | + - playwright-report |
| 113 | +``` |
| 114 | + |
| 115 | +The `okteto/test` action downloads these artifacts to the runner. Use the [`actions/upload-artifact`](https://github.com/actions/upload-artifact) action to attach them to the workflow run so you can download them from the pull request: |
| 116 | + |
| 117 | +```yaml |
| 118 | + - name: Save Playwright report |
| 119 | + uses: actions/upload-artifact@v4 |
| 120 | + if: ${{ !cancelled() }} |
| 121 | + with: |
| 122 | + name: playwright-report |
| 123 | + path: playwright-report/ |
| 124 | + retention-days: 30 |
| 125 | +
|
| 126 | + - name: Save test results |
| 127 | + uses: actions/upload-artifact@v4 |
| 128 | + if: ${{ !cancelled() }} |
| 129 | + with: |
| 130 | + name: test-results |
| 131 | + path: test-results/ |
| 132 | + retention-days: 30 |
| 133 | + include-hidden-files: true |
| 134 | +``` |
| 135 | + |
| 136 | +The `if: ${{ !cancelled() }}` condition uploads the artifacts even when a test fails, so you can inspect the results of a failed run. |
| 137 | + |
| 138 | +## Verification |
| 139 | + |
| 140 | +Open a pull request against the `main` branch. GitHub starts the workflow, which deploys the Preview Environment and then runs your Test Containers against it. Track the run in the **Checks** section of the pull request. |
| 141 | + |
| 142 | +When the tests pass, the workflow succeeds and the `test` step logs output like this: |
| 143 | + |
| 144 | +```bash |
| 145 | + i Using cindy @ okteto.example.com as context |
| 146 | + i Executing test container 'e2e' |
| 147 | + i Running 'bash run_tests.sh' |
| 148 | + 6 passed (9.9s) |
| 149 | + ✓ Command 'bash run_tests.sh' successfully executed |
| 150 | + ✓ Test container 'e2e' passed |
| 151 | +``` |
| 152 | + |
| 153 | +When a test fails, the `test` step exits with a non-zero code, the check fails on the pull request, and the exported artifacts are still available for download from the workflow run. |
| 154 | + |
| 155 | +## Troubleshooting |
| 156 | + |
| 157 | +- **The tests run in the wrong Namespace.** Confirm the `namespace` input of the `okteto/test` action matches the `name` of the `okteto/deploy-preview` step. Both must reference the same Preview Environment. |
| 158 | +- **The workflow can't authenticate.** Check that the `OKTETO_TOKEN` and `OKTETO_CONTEXT` secrets exist and that `OKTETO_TOKEN` is a valid [Admin Access Token](admin/dashboard.mdx#admin-access-tokens). |
| 159 | +- **A Test Container isn't found.** The values in the `tests` input must match the keys under the `test` section of your Okteto Manifest. Run `okteto test` locally to confirm the Test Container names. |
| 160 | +- **The tests time out.** Raise the `timeout` input, for example `timeout: 15m`, for suites that take longer than the 5-minute default. |
| 161 | + |
| 162 | +## Next steps |
| 163 | + |
| 164 | +- Run the same Test Containers locally in your inner loop with [Okteto Test](testing/getting-started-test.mdx). |
| 165 | +- Automatically remove the Preview Environment when the pull request closes by adding the [Preview Environments cleanup workflow](previews/using-github-actions.mdx#step-5-cleanup). |
| 166 | +- Run your pipeline on GitLab instead with [Preview environments using GitLab CI/CD](previews/using-gitlab-cicd.mdx). |
0 commit comments