Build using Github & Deploy using Nife

To deploy to Github we essentially need four things.
  1. The application you want to deploy.
  2. A runnable copy of nifectl.
  3. A nife.toml file.
  4. Your Nife API Token.
Building a Docker image automatically is an important step in building a CI/CD pipeline for your Docker workloads.

Steps to Create Docker images with Github Actions
What you need to get started
  • Github account
  • Basic understanding of git or Github.
  • Familiarity with Docker.

Step 1 – Create a Github repository

Before getting started you need a Github repository. Log into Github and start a new project or go to the new repository page. For example, we are creating a new repository called nginx-docker-github-actions. Additionally our repo is public so you can use it as a reference.

Github Deployment

If you are familiar with using git from the command line, you can clone this repository to your computer. If not you can perform your commits through the Github web interface. For the sake of simplicity we will use the web interface in this tutorial.

Step 2 – Commit a Dockerfile to your Github repository

Next we will create a Dockerfile. A Dockerfile is the build manifest or recipe for a Docker container image. Its contents include the base image we can use to run. We can modify the image to suit our needs.

Create a text file called Dockerfile with the following contents:

# Basic nginx dockerfile starting with Ubuntu 20.04
FROM ubuntu:20.04
RUN apt-get -y update
RUN apt-get -y install nginx
Lets walk through the contents of the Dockerfile.
  • The first line is a comment describing what the intention for the Dockerfile is.
  • FROM tells Docker which is the base image.
  • We are using the 20.04 version of Ubuntu from Dockerhub.
  • RUN tells Docker to execute commands inside container image it is building. In this case we are using the apt-get command to update the cache of available packages. Finally we install the NGINX package.

In summary, this Dockerfile takes the base Ubuntu Docker image and installs the NGINX package on top of it. Now put the contents of the Dockerfile into the root of your Github repository. You can do this by using the add file button on your repositories main page.

If you choose “create new file” you will be provided with an editor. Just copy and paste the Dockerfile contents and save the commit. Otherwise you can choose “Upload files” and upload the Dockerfile from your computer. Now that we have the Dockerfile in our repository, we need to setup an action workflow.

Step 3 – Select a Github Action workflowGithub action workflow
  1. Select the Actions tab.
  2. Go to the main page for your repository.
  3. Click on the Actions tab.
  4. In the Actions tab click new workflow.
  5. Use the new workflow button to create a Docker image creation workflow. You will be presented with suggestions for workflows. Because we have a Dockerfile in our repo the Docker workflows will be displayed prominently. We choose the “Docker image” workflow which can be seen in the right of the screenshot below.
  6. Click the “Set up this workflow” button.
  7. Choose the “docker image” workflow by clicking “setup this workflow.”
Github workflow.
Step 4 – Save your new Github action workflow

Now you should see a editor with a new file created. This file contains the information that drives the Github action. Click the “start commit” button to save this file. You will be prompted for commit message. The commit message will go in your repository changelog. Make it informative so you can remember why you made this change later on.

Note the location of the file .github/workflows. This new directory created in our repository stores our actions.

Step 5 – Make a change and trigger a Docker image build

To trigger a build we must make a change to our main branch. Make a simple comment change to the Dockerfile so it looks like this:

# Add a new comment to trigger build.
# basic nginx dockerfile starting with Ubuntu 20.04
FROM ubuntu:20.04
RUN apt-get -y update
RUN apt-get -y install nginx

The easiest way to do this is through the Github web interface. Go to your repository page and click on the docker file. Next use the edit this file button. Make the change and commit to the main branch.

Github will detect the change to your repo and kick off a new build. You should see an indicator that the build is kicked off.

The status indicator uses colors to indicate your build status. The green check mark indicates success, yellow circle indicates in-progress and a red circle indicates failure.

To see your build logs click on the indicator. It takes you to the page for that build. From there you can drill down into the steps and logs for each step. Additionally, you will get emails when the build fails and can easily configure your alerting in Github.

Configuring for Nife
Prerequisite:#
  • Before proceeding further we install nifectl with the command in the github actions.
  • We need nife.toml file and an auth token.
The nife.toml file will be needed to create using nife init so you will likely to do this locally and then add to the repository, for nifectl to find when it is run in deployment.

For the API token use nifectl, using the token previously logged with.

Run nifectl auth token and this will display the token your session. Use this token value into NIFE_ACCESS_TOKEN environment variables inside the deployment.

For github,

  1. You will need to go to the repository settings.
  2. Select CI/CD then expand the variable.
  3. After creating the new variable copy the auth token value into the value field. Then turn on the protected and masked switches so that it is not leaked through the logs.
Ready to Deploy

Now we are ready to commit the github-ci.yml to the repository and install their CI/CD pipeline.

On the github actions web UI, head to CI/CD then jobs and you should see the deployment job running. Click on the running badges to see the progress.

Job’s Done

And that’s pretty much for deploying with CI/CD system. There’s lot more functionality in there allowing you to structure the pipeline as you want and trigger different jobs at different times.