Continuous Integration with GitHub Actions - 1
GitHub Actions:
GitHub Actions is a feature of GitHub that provides a platform for Continuous Integration (CI) and Continuous Delivery (CD).It allows you to automate your build, test, and deployment pipelines. You can create workflows that run tests whenever code changes are pushed to the repository or deploy merged pull requests to production or other environments.
Steps to Create a GitHub Actions Workflow:
???????????
1. Create a Workflow File:
●???? In your repository, create a directory named .github/workflows if it doesn't already exist.
●???? Inside this directory, create a workflow file, typically named main.yml.
2. Define Workflow Configuration:
●???? In this file, you define all required configurations, such as:
??????????? 1. Branch Specification: The on key specifies the events that trigger the workflow.
You can specify branches or specific events (e.g., push, pull_request).
??????????? For Example:
on:
push:
branches: [ "main" ]
??????????? 2. Job Dependencies: You can define multiple jobs with dependencies (e.g., a build job followed by a deploy job).
??????????????????For eg.
jobs:
build:
runs-on: ubuntu-latest
??????????? 3. Operating System: Specify the OS on which the workflow should run (e.g., ubuntu-latest).
?????????? For eg. jobs:
jobs:
build:
runs-on: ubuntu-latest
??????????? 4. Environment Setup: Set up the required environment, which may include:
??????????????????????? 1.Installing JDK and specifying its version.
领英推荐
??????????????????????? ?For Eg:
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
???????????????????????????????????????????????????????????????????????????????????
??????????????????????? 2.Setting up other dependencies required for your project.
??????????????????????? For Eg:
- name: Build with Maven
run: mvn -B package --file pom.xml
5. Build and Test Commands: Include the necessary commands to build and test your project, such as Maven commands like mvn clean install.
?????For Eg:
- name: Build with Maven
run: mvn clean install
??????????????????????????????????????????????????????????????????????????????????? ?
6. Docker commands: Docker commands to build and push images, if you're using Docker.
??????????? 1.Providing Docker credentials securely through GitHub Secrets.
??????????? For Eg:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
??????????? 2. Specifying the image path for deployment.
??????????? For eg:
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: abcd/demo:0.0.1-SNAPSHOT
???????????????????????????????????
GitHub Secrets: Store sensitive data like Docker credentials in GitHub Secrets to keep them secure.
Example yml for reference main.yml
name: CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
- name: Build with Maven
run: mvn clean install
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: abcd/demo:0.0.1-SNAPSHOT