|
| 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. |
0 commit comments