As developers, setting up a secure Continuous Integration/Continuous Deployment (CI/CD) pipeline is one of the most beneficial aspects of modern software development. Not only does it automate the software delivery process, but it also encourages a culture of collaboration, early problem detection, and increased release speed.
One of the most popular tools available for creating a CI/CD pipeline is GitHub Actions. This service allows you to automate, customize, and execute your software development workflows right in your GitHub repository.
In this guide, we’ll demonstrate how you can set up a secure CI/CD pipeline for a Go application using GitHub Actions. We’ll delve into the essentials of GitHub Actions, secrets, workflows, and finally, how to build, test, and deploy your Go application.
Understanding GitHub Actions
GitHub Actions provide a powerful, flexible way to automate virtually any aspect of your team’s software workflow. It provides the capability to build, test, and deploy your code right from your GitHub repository.
You can create individual tasks, known as actions, and combine them to create custom workflows in your repository. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.
Building the Go Application with GitHub Actions
Building a Go application with GitHub Actions essentially involves creating a workflow file in your repository. This file serves as the blueprint for your action and defines what happens when certain specified events occur.
To start with, create a new file named main.yml
inside the .github/workflows
directory in your repository. This is where we will define the steps necessary to build our Go application.
The workflow file should look like the following:
name: Go
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: 1.14
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: go build -v .
This workflow does the following:
- It runs whenever there is a push or a pull request to the master branch.
- It sets up a Go environment with the specified version.
- It checks out your repository into the Go module directory.
- It retrieves all dependencies necessary to build the Go application.
- Finally, it builds your Go application.
Using secrets in GitHub Actions
Secrets are encrypted environment variables that you create in your repository settings. They are only exposed to selected actions and are not printed in logs, thereby offering a secure way to store sensitive information like access tokens, passwords, or API keys.
You can use secrets in your workflows by using the secrets
context:
steps:
- name: Hello world action
with:
super_secret: ${{ secrets.SuperSecret }}
env:
super_secret: ${{ secrets.SuperSecret }}
In this example, the secret named SuperSecret
is passed to the Hello world action
and is also set as an environment variable in that step’s environment.
Deploying the Go Application with GitHub Actions
The final step involves deploying your Go application. For this tutorial, we are going to deploy our application to a fictional cloud provider. We’ll assume that this provider requires a username and password, which we will store as secrets in our repository.
Add the following steps to your workflow file:
- name: Deploy
env:
CLOUD_USERNAME: ${{ secrets.CLOUD_USERNAME }}
CLOUD_PASSWORD: ${{ secrets.CLOUD_PASSWORD }}
run: |
echo Deploying to the cloud...
echo Username: $CLOUD_USERNAME
echo Password: $CLOUD_PASSWORD
This workflow step does the following:
- It sets up two environment variables using secrets from your repository.
- It prints out a message and the values of the environment variables.
Once you’ve completed all these steps, you’ll have a secure CI/CD pipeline that automatically builds, tests, and deploys your Go application whenever a change is pushed to the master branch.
With GitHub Actions, not only can you automate your software workflows, but also rest assured that your secrets are secure and only exposed to the actions that need them. By leveraging this power, you can focus on what really matters: writing great code.
Managing and leveraging GitHub Secrets
GitHub Secrets are a crucial component in setting up secure GitHub Actions. They provide a confidential storage location for sensitive data such as authentication keys, access tokens, or passwords. They are encrypted and remain concealed in logs, ensuring the information remains hidden.
When setting up a GitHub action, you have an option to define secrets that the workflow can access. Once defined, you can reference these secrets in your workflow file. For instance, you might require a CLOUD_USERNAME
and CLOUD_PASSWORD
to deploy your Go application to a specific cloud provider.
To add secrets to your GitHub repository, navigate to the repository’s main page, click on ‘Settings’, then ‘Secrets.’ Here, you can add new secrets by providing a name and the corresponding value.
In the YAML workflow file, these secrets can be referenced and assigned to environment variables like so:
env:
CLOUD_USERNAME: ${{ secrets.CLOUD_USERNAME }}
CLOUD_PASSWORD: ${{ secrets.CLOUD_PASSWORD }}
Remember to replace CLOUD_USERNAME
and CLOUD_PASSWORD
with the actual names of your secrets. This way, your secrets remain secure, and your workflow can still access the necessary sensitive information.
Automating builds and deployments with GitHub Actions
GitHub Actions offer a flexible way to automate software development workflows. It allows you to set up a CI/CD pipeline that automatically builds, tests, and deploys your Go application whenever you make changes.
Under the jobs:build:
section of your main.yml
workflow file, you can include various steps to automate the entire process from code checkout to deployment.
For instance, using actions/setup-go@v2
action sets up a Go environment with a specified version. The actions/checkout@v2
action checks out your code into the Go module directory, and using go build -v .
command, you can build your Go application.
Meanwhile, the on:push:branches:
and on:pull_request:branches:
sections allow you to specify when the workflow should be triggered. By setting the value to [master]
, the workflow triggers every time a push or pull request is made to the master branch.
During the deployment step, the secret environment variables CLOUD_USERNAME
and CLOUD_PASSWORD
are used. These are the same variables that were defined in the secrets section earlier.
Setting up a secure CI/CD pipeline for a Go application using GitHub Actions can be quite straightforward. By understanding the key components such as GitHub Actions, workflows, and secrets, and how they work together, you can automate the process of building, testing, and deploying your Go application.
Remember to always keep your sensitive data like passwords, API keys, and tokens secure by storing them as secrets. This way, they are concealed in logs and only exposed to selected actions.
Using GitHub Actions, you can automate your software workflows, ensuring rapid and regular updates to your application, while keeping your code secure. So, start harnessing the power of GitHub Actions today and let it handle the repetitive tasks, while you focus on developing great applications.