Page History
GitHub is a well-known platform hosting thousands of source-code repositories.
...
An introduction to GitHub actions can be seen here.
Table of Contents |
---|
Main concepts
In a nutshell, workflows are automated processes described as YAML files, stored under .github/workflows. These are usually triggered by events (e.g. code-commit, pull-request) or can also be scheduled.
...
Another way of sharing data, especially between jobs, would be to produce and store artifact(s) in a job and obtain them in another job.
Examples
Basic JUnit example
In this basic example showcasing a dummy calculator, we want to get visibility of the automated test results from some tests implemented in Java, using the JUnit framework.
Info | ||
---|---|---|
| ||
The source code for this example is available in this GitHub repository. |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package com.xpand.java;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class CalcTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void CanAddNumbers()
{
assertThat(Calculator.Add(1, 1), is(2));
assertThat(Calculator.Add(-1, 1), is(0));
}
@Test
public void CanSubtract()
{
assertThat(Calculator.Subtract(1, 1), is(0));
assertThat(Calculator.Subtract(-1, -1), is(0));
assertThat(Calculator.Subtract(100, 5), is(95));
}
@Test
public void CanMultiply()
{
assertThat(Calculator.Multiply(1, 1), is(1));
assertThat(Calculator.Multiply(-1, -1), is(1));
assertThat(Calculator.Multiply(100, 5), is(500));
}
public void CanDivide()
{
assertThat(Calculator.Divide(1, 1), is(1));
assertThat(Calculator.Divide(-1, -1), is(1));
assertThat(Calculator.Divide(100, 5), is(20));
}
@Test
public void CanDoStuff()
{
assertThat(true, is(true));
}
} |
To implement the continuous integration, we'll implement a specific workflow for it and store it .github/workflows/CI.yaml.
We’ll use the actions/checkout action to checkout the code from our repository to the virtual environment. This action is one of the "standard" actions provided by GitHub ; (check full list here. The same for ).
To compile the code, we need to use a JDK; we can use the action actions/setup-java which allows us to choose the specific Java version.
We use Maven to build and run the tests.
Code Block | ||||
---|---|---|---|---|
| ||||
name: example4aCI on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Set up Java uses: actions/setup-java@v1 with: java-version: '1.8+' - name: Build with Maven run: mvn clean compile test --file java-junit-calc/pom.xml - name run: curl -H "Content-Type: multipart/form-data" -u $jira_user:$jira_password -F "file=@target/surefire-reports/TEST-com.xpand.java.CalcTest.xml" "$jira_server_url/rest/raven/1.0/import/execution/junit?projectKey=CALC" |
In order to submit those results to Xray, we'll just need to invoke the REST API (as detailed in Import Execution Results - REST).
However, we do not want to have the Jira credentials hardcoded in the configuration file. Therefore, we'll use some secret variables defined in GitHub project settings.
Some parameters may be hardcoded on the HTTP request used to submit the result; this is up to you to define what makes sense to be explicit on the request or what could be set, for example, using a secret variable in GitHub.
Info | ||
---|---|---|
| ||
The Jira username must exist in the Jira instance and have permission to create Test and Test Execution Issues. |
To see the runs for your workflows (i.e. workflow runs), you may access Actions tab in your repository browser.
Clicking in the last event that triggered the workflow run will show the details.
In Jira, Xray now shows the results of the automated tests in a brand new Test Execution issue.
Tips
- in the workflow definition, configure it to cache Maven dependencies (more info here)
- it's possible to re-run jobs from GitHub UI