|
| 1 | +--- |
| 2 | +tags: [how-to, build, image] |
| 3 | +--- |
| 4 | + |
| 5 | +# Using the image outside of Nais |
| 6 | + |
| 7 | +When using the [nais/docker-build-push](https://github.com/nais/docker-build-push) action, the image is pushed to a registry that is meant for use within the Nais platform. |
| 8 | +If you need to use the image outside of Nais, e.g. locally in a development environment, you should push the image to another registry. |
| 9 | + |
| 10 | +## Push to GitHub Container Registry |
| 11 | + |
| 12 | +After the image is built by `nais/docker-build-push`, you can push it to the GitHub Container Registry (GHCR) by adding the following step to your workflow: |
| 13 | + |
| 14 | +- `packages: write` permission is required to push images to the GHCR. |
| 15 | +- Step to retag the image after it has been built. |
| 16 | + |
| 17 | +```yaml hl_lines="13 21-26" |
| 18 | +name: Build and deploy |
| 19 | +on: |
| 20 | + push: |
| 21 | + branches: |
| 22 | + - main |
| 23 | +jobs: |
| 24 | + build_and_deploy: |
| 25 | + name: Build, push and deploy |
| 26 | + runs-on: ubuntu-latest |
| 27 | + permissions: |
| 28 | + contents: read |
| 29 | + id-token: write |
| 30 | + packages: write |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + - name: Build and push image and SBOM to OCI registry |
| 34 | + uses: nais/docker-build-push@v0 |
| 35 | + id: docker-build-push |
| 36 | + with: |
| 37 | + team: <MY-TEAM> # Replace |
| 38 | + - name: Push image to ghcr.io |
| 39 | + run: | |
| 40 | + # Log in to the GitHub Container Registry |
| 41 | + echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 42 | + # Tag the image, e.g. ghcr.io/owner/repo:latest |
| 43 | + docker buildx imagetools create -t ghcr.io/${{ github.repository }}:latest ${{ steps.docker-build-push.outputs.image }} |
| 44 | + - name: Deploy to Nais |
| 45 | + uses: nais/deploy/actions/deploy@v2 |
| 46 | + env: |
| 47 | + CLUSTER: <MY-CLUSTER> # Replace (1) |
| 48 | + RESOURCE: .nais/app.yaml #, topic.yaml, statefulset.yaml, etc. |
| 49 | + VAR: image=${{ steps.docker-build-push.outputs.image }} |
| 50 | + TELEMETRY: ${{ steps.docker-build-push.outputs.telemetry }} |
| 51 | +``` |
| 52 | +
|
| 53 | +## Even more control |
| 54 | +
|
| 55 | +If you need more control of how the image is built, e.g. supporting more platforms etc, you can use the [nais/login action](https://github.com/nais/login) to log in to the registry |
| 56 | +provided by Nais, and build the image using e.g. [docker/build-push-action](https://github.com/docker/build-push-action). |
| 57 | +You can also use the [nais/attest-sign](https://github.com/nais/attest-sign) action to sign the image before pushing it to the registry. |
| 58 | +
|
| 59 | +See the [nais/docker-build-push action file](https://github.com/nais/docker-build-push/blob/main/action.yml) for a complete example. |
0 commit comments