๐ Hello Nais ยถ
This tutorial will take you through the process of getting a simple application up and running on Nais.
Prerequisites ยถ
- You have a GitHub account connected to your GitHub organization (e.g.
navikt)
Conventions
Throughout this guide, we will use the following conventions:
-
<MY-APP>- The name of your Nais application (e.g.joannas-first) -
<MY-TEAM>- The name of your Nais team (e.g.onboarding) -
<GITHUB-ORG>- Your GitHub organization (e.g.navikt) -
<MY-ENV>- The name of the environment you want to deploy to (e.g.dev-gcp)
NB! Choose names with lowercase letters, numbers and dashes only.
โ๏ธ Setup ยถ
Create your own GitHub repository ยถ
Create your own repo using the nais/hello-nais as a template.
You create a new repository through either the GitHub UI or through the GitHub CLI:
gh repo create <GITHUB-ORG>/<MY-APP> --template nais/hello-nais --private --clonecd <MY-APP>Authorize the repository for deployment ยถ
This is required for the GitHub Actions workflow to be able to deploy your application.
Visit Nais Console. Select your team, and visit the Repositories tab.
Here you can add your repository to the list of authorized repositories. This authorizes the GitHub repository to perform deployments on behalf of your team.
Dockerfile ยถ
This describes the system your application will be running on. It includes the base image, and the commands needed to build your application. This is the payload you are requesting Nais to run. We have created this file for you, as there are no changes needed for this tutorial. Check it out.
Application manifest ยถ
This file describes your application to the Nais platform so that it can run it correctly and provision the resources it needs.
Create a file called app.yaml in a .nais-folder.
mkdir .nais
touch .nais/app.yamlAdd the following content to the file, and insert the appropriate values in the placeholders on the highlighted lines:
.nais/app.yaml
apiVersion: nais.io/v1alpha1
kind: Application
metadata:
name: <MY-APP>
namespace: <MY-TEAM>
spec:
ingresses:
- https://<MY-APP>.<MY-ENV>.nav.cloud.nais.io
image: {{image}}1
port: 8080
ttl: 3h
replicas:
max: 1
min: 1
resources:
requests:
cpu: 50m
memory: 32MiNote the ttl: 3h. It sets the "time to live" for your app to 3 hours, in case you start on this tutorial and forget to clean up after.
GitHub Actions workflow ยถ
GitHub Actions uses the Dockerfile from step 1 and the app.yaml from step 2. to build and deploy your application to Nais.
Create a file called main.yaml in a .github/workflows-folder.
mkdir -p .github/workflows
touch .github/workflows/main.yamlAdd the following content to the file, and insert the appropriate values in the placeholders on the highlighted lines:
.github/workflows/main.yaml
name: Build and deploy
on:
push:
branches:
- main
jobs:
build_and_deploy:
name: Build, push and deploy
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
actions: read
steps:
- uses: actions/checkout@v4
- name: Build and push image and SBOM to OCI registry
uses: nais/docker-build-push@v0
id: docker-build-push
with:
team: <MY-TEAM> # Replace
- name: Deploy to Nais
uses: nais/deploy/actions/deploy@v2
env:
CLUSTER: <MY_ENV> # Replace1
RESOURCE: .nais/app.yaml # This points to the file we created in the previous step
VAR: image=${{ steps.docker-build-push.outputs.image }}2
TELEMETRY: ${{ steps.docker-build-push.outputs.telemetry }}image used in the app.yaml file to the image we just built.Excellent! We're now ready to deploy ๐
๐ข Ship it ยถ
Previously we've made our application and created the required files for deployment. In this part of the tutorial we will deploy our application to Nais.
Commit and push your changes ยถ
Now that we have added the required files, it's time to commit and push them to GitHub.
git add .
git commit -m "FEAT: Add nais app manifest and github workflow"
git push origin mainObserve the GitHub Actions workflow ยถ
When pushed, the GitHub Actions workflow will automatically start. You can observe the workflow by running the following command:
gh run watch- Visit your repository on GitHub.
- Navigate to
Actions. - Select the latest workflow run.
If you get a 403 PERMISSION_DENIED error in the Build and push image step, you might not have added your repo to the Nais team as described above, or it might not have synchronized yet.
Visit your application ยถ
On successful completion, we can view our application at https://<MY-APP>.<MY-ENV>.nav.cloud.nais.io
Congratulations! You have now successfully deployed your first application to Nais! The next and most important step is to clean up after ourselves.
๐งน Clean up ยถ
During this tutorial we have
- created a github repository
- added the required files for deployment
- deployed our application to Nais
Now it's time to clean up after ourselves.
Delete your repository ยถ
When you are finished with this guide you can delete your repository:
gh repo delete <GITHUB-ORG>/<MY-APP>- Visit your repository on GitHub.
- Navigate to
Settings. - At the bottom of the page, click on
Delete this repository - Confirm the deletion
Your next application ยถ
In this guide, we took a few short-cuts to get you up and running quickly.
For instance, we didn't explore all the possibilities of the app.yaml file, or take advantage of the integrations the nais platform provides.
To learn more about the possibilities, explore our documentation for Applications and Nais Jobs.
The GitHub workflow is similarly simplified. Some teams like to do more advanced things, like running tests, or deploying to multiple environments. It is also possible to skip some steps under certain conditions, such as not building a new image if the code hasn't changed. Explore the GitHub Actions documentation for more information about GitHub Actions in general, and our guide on workload image for how you can skip building.