What can be done with Github Actions

Emrecan Koç
4 min readOct 25, 2020

--

Github recently released a new feature called Actions. It makes it easy to automate software workflows with CI/CD. And it has a free usage option. I’m really into DevOps. So I give it a try and learn new things from it.

CI/CD tools are getting more and more popular. It makes the software development lifecycle faster and more agile. The open-source community was using different tools to manage their workflows so far. Github brings this new feature to where their code lives. Community is a big plus this kind of feature, and Github already has it.

Running Tests

The first thing I do is automating my unit tests. Tools like Maven and Npm are available, and they can be managed smoothly with Github Actions. It’s really fresh, but the community is already working to extend it. There are lots of examples and custom Actions already shared by the community.

I have created a workflow file like the below to run my unit tests. It is a simple workflow. I’m not a DevOps engineer, but I’ve worked with different automation tools before. Even if you never worked with other tools, you still understand what’s going on here.

name: Java CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml

I used actions/setup-java action for the Java environment. It supports lots of Java tools. You can check it out repository below. Also, it includes some usage examples too.

Deploying packages to github package registry

I have a common project which I want to use from several private projects. I thought it would be good to learn how to deploy and publish things with Github Actions and the Github Package Registry.

I have got this tiny Maven project. Build on Java and depending on a small list of artifacts. I picked Github Package Registry over Maven Central because I’m the only user of this project. Plus, it’s a good example for the Github Package Registry as I said.

I created another workflow for publishing separated from unit tests. It will be triggered when a release tag is added.

Publish Maven project to Github Package Registry

The workflow builds the package, updates the version based on the tag and deploys it to Github Package Registry when a new release tag is added

Another important thing is the settings.xml file. Action has a default settings.xml for deploying Github Package Registry. It requires a Github Token for publishing. More configuration can be found in the official documentation.

Packages section after publishing package

When you successfully deploy your package. It can be accessible over your profile packages section. It could be downloadable from there. But you would like to access it via Maven.

Maven fetches packages from Maven Central by default. But this package stays on Github Package Registry. Therefore, you need to adjust some Maven configuration before using it. The first step is adding server information to your Maven global setting file.

<servers>
<server>
<id>github</id>
<username>GITHUB_USERNAME</username>
<password>GITHUB_TOKEN</password>
</server>
</servers>

Then in your project pom file, you need to define the package URL and artifact details. Something similar like below works fine.

<!-- Repositroy-->
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/YOUR_USERNAME/PACKAGE_NAME</url>
</repository>
</repositories>
<!-- Dependency-->
<dependency>
<groupId>YOUR_PROJECT_GROUP_ID</groupId>
<artifactId>YOUR_GROUP_ID</artifactId>
<version>0.1</version>
</dependency>

Honestly, it is too much effort for other users. Maven Central is a more viable option for more significant projects.

Deploying Github Pages with Github Action

Another thing I’ve done so far is deploying my own portfolio page via Github Pages. Github Pages is a free way to serve a static page over Http. I’ve got my own setup for the portfolio page. I’ve created a Gatsby.js application, which is a Jamstack solution based on React, Redux and GraphQL. The Jamstack architecture means serving pre-built files over a CDN, it’s cheaper and it has better scaling.

For deploying this kind of application to Github Pages, you’ll need to build and then push those static files to your master branch. This process requires lots of things to be done manually. As a software developer, I hate doing things manually. As a Software Developer I prefer wasting my 5 hours to automate something that takes 5 minutes manually :).

This automation can be done with git commands and bash scripts. To achieve such a thing you need to write a script that takes your files and pushes them to your repository. Luckily there is a custom Github Action already exists. JamesIves/github-pages-deploy-action is very popular in the community. It has over 1k stars on Github. Also it has good examples to start with.

The workflow above builds and deploys those static files when I push a change to the development branch. This custom action has lots of configuration check it out too if you interested.

Conclusion

That is all things I have tried so far. I’d like to see where Github Actions go. It has a growing community. Plus, it can be used freely. If you want to learn more about it check their official documentations.

Thanks for reading.

PS: I’m not native English speaker. So I’m sorry for grammer errors :)

--

--

Emrecan Koç

A senior software engineer. Trying to improve writing and social skills.