Skip to content

Commit c653232

Browse files
Merge pull request #1263 from Sunbird-Lern/gh-actions
SBCOSS-466: Userorg service - create Github actions to publish docker image to GHCR
2 parents ac7a278 + 28fcbd8 commit c653232

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

.github/BUILD.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# GitHub Action: Build and Deploy
2+
3+
## Overview
4+
This GitHub Action automates the build and deployment process for the UserOrg service. It builds the project, runs tests, packages the application, and pushes a Docker image to GitHub Container Registry (GHCR).
5+
6+
## Usage
7+
The action will automatically build and deploy your service whenever a new tag is pushed.
8+
9+
## Steps
10+
11+
1. **Set up JDK 11**
12+
- Configures the environment with JDK 11 using the `actions/setup-java` action with Temurin distribution.
13+
14+
2. **Checkout code**
15+
- Checks out the repository code with full commit history.
16+
17+
3. **Cache Maven packages**
18+
- Caches Maven dependencies to speed up subsequent builds.
19+
20+
4. **Build and run test cases**
21+
```bash
22+
mvn clean install
23+
```
24+
25+
5. **Package build artifact**
26+
- Packages the application using Play Framework's `dist` goal.
27+
```bash
28+
mvn -f controller/pom.xml play2:dist
29+
```
30+
31+
6. **Upload artifact**
32+
- Uploads the packaged application as a GitHub Actions artifact named `userorg-service-dist`.
33+
34+
7. **Extract image tag details**
35+
- Prepares Docker image name and tags based on the repository name and reference.
36+
37+
8. **Log in to GitHub Container Registry**
38+
- Authenticates to GHCR using the provided GitHub token.
39+
40+
9. **Build and push Docker image**
41+
- Builds the Docker image using the provided Dockerfile and pushes it to GHCR with the appropriate tags.
42+
43+
## Environment Variables
44+
- `REGISTRY`: The GitHub Container Registry URL (ghcr.io)
45+
- `IMAGE_NAME`: Auto-generated based on the repository name (e.g., ghcr.io/username/repo)
46+
- `IMAGE_TAG`: Auto-generated based on the git reference (branch name or tag)
47+
48+
## Permissions
49+
This workflow requires the following permissions:
50+
- `contents: read` - To read the repository contents
51+
- `packages: write` - To push Docker images to GitHub Container Registry
52+
53+
## How to Use the Docker Image
54+
55+
1. **Pull the Docker Image**:
56+
```bash
57+
docker pull ghcr.io/<repository-name>:<tag>
58+
```
59+
Replace `<repository-name>` and `<tag>` with the appropriate values.
60+
61+
2. **Run the Docker Container**:
62+
```bash
63+
docker run -d -p <host-port>:9000 ghcr.io/<repository-name>:<tag>
64+
```
65+
Replace `<host-port>` with your desired port numbers.
66+
67+
3. **Access the Application**:
68+
Once the container is running, you can access the application at `http://localhost:<host-port>`.
69+
70+
## Notes
71+
- The workflow is configured to use Ubuntu latest runner.
72+
- Maven dependencies are cached to improve build performance.
73+
- The Docker image is built using the Dockerfile in the repository root.
74+
- The workflow automatically handles both branch-based and tag-based deployments.

.github/workflows/build.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: Build and Deploy
2+
3+
# Trigger this workflow whenever there is a new tag push
4+
on:
5+
push:
6+
tags:
7+
- '*'
8+
9+
jobs:
10+
ghcr-build-and-deploy:
11+
runs-on: ubuntu-latest
12+
13+
permissions:
14+
contents: read # Read access to repository contents
15+
packages: write # Permission to write to GitHub Packages (GHCR)
16+
17+
# Define container registry to be GitHub Container Registry and store it as an env
18+
env:
19+
REGISTRY: ghcr.io
20+
21+
steps:
22+
# Set up Java Development Kit (JDK) version 11 using Temurin distribution
23+
- name: Set up JDK 11
24+
uses: actions/setup-java@v2
25+
with:
26+
distribution: 'temurin'
27+
java-version: '11'
28+
29+
# Checkout the repository code with full history (fetch-depth: 0)
30+
- name: Checkout code
31+
uses: actions/checkout@v2
32+
with:
33+
fetch-depth: 0
34+
35+
# Cache Maven dependencies to speed up subsequent builds
36+
- name: Cache Maven packages
37+
uses: actions/cache@v3
38+
with:
39+
path: |
40+
~/.m2/repository
41+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
42+
restore-keys: |
43+
${{ runner.os }}-maven-
44+
45+
# Build the project and run test cases
46+
- name: Build and run test cases
47+
run: |
48+
mvn clean install
49+
50+
# Package the build artifact using Play Framework's `dist` goal
51+
- name: Package build artifact (Play dist)
52+
run: mvn -f controller/pom.xml play2:dist
53+
54+
# Upload the build artifact as an artifact to GitHub Actions
55+
- name: Upload artifact
56+
uses: actions/upload-artifact@v4.3.1
57+
with:
58+
name: userorg-service-dist
59+
path: |
60+
controller/target/userorg-service-*-dist.zip
61+
62+
# Extract repository and tag info to form image name and tag
63+
- name: Extract image tag details
64+
id: image_vars
65+
run: |
66+
REPO_LOWER=$(echo "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]')
67+
TAG_LOWER=$(echo "${GITHUB_REF_NAME}" | tr '[:upper:]' '[:lower:]')
68+
IMAGE_NAME=${{ env.REGISTRY }}/${REPO_LOWER}
69+
IMAGE_TAG=${TAG_LOWER}
70+
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV
71+
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
72+
73+
# Log in to GitHub Container Registry using the GitHub Actions token
74+
- name: Log in to GitHub Container Registry (GHCR)
75+
uses: docker/login-action@v2
76+
with:
77+
registry: ${{ env.REGISTRY }}
78+
username: ${{ github.actor }}
79+
password: ${{ secrets.GITHUB_TOKEN }}
80+
81+
# Build and push Docker image to GitHub Container Registry (GHCR)
82+
- name: Build and push Docker image to GHCR
83+
uses: docker/build-push-action@v4
84+
with:
85+
context: .
86+
file: ./Dockerfile
87+
push: true
88+
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}

0 commit comments

Comments
 (0)