STREAMLINE YOUR CI/CD PIPELINE WITH GITHUB ACTIONS
Muhammad Abiodun SULAIMAN (B.Tech, M.Sc.)
|| Data Engineer || Machine Learning Engineer || Cloud DevOps || Data Science || AWS Community Builder (Machine Learning) || Streamlit || Python Automation || Health Information Technology ||
In the current dynamic software development landscape, security and efficiency are critical. Companies and developers work hard to swiftly roll out apps while ensuring they are safe from security flaws. This is where the industry-leading containerization technology Docker comes into play. By wrapping up applications in containers, Docker makes the deployment process simpler. However, if manually done, managing these containers, creating them, checking for vulnerabilities, and pushing them safely can be difficult and time-consuming. For this reason, it's critical to automate these procedures via a Continuous Integration/Continuous Deployment (CI/CD) pipeline.
The Role of CI/CD in Modern Software Development
Software release process automation is the goal of Continuous Integration (CI) and Continuous Deployment (CD). Building software that is reliable, safe, and quickly deployable is the aim.
Continuous Integration:
CI involves merging all developers' working copies to a shared mainline several times daily. The main objectives of CI include:
Continuous Deployment:
CD extends CI by automatically deploying all code changes to a testing and/or production environment after the build stage. This means that, on top of the automated testing, automated release processes further streamline the development lifecycle. Benefits of CD include:
Implementing GitHub Actions for Docker Management:
From the first code change to the last production deployment, GitHub Actions is a CI/CD tool that streamlines the software process. Building processes that automatically create, test, and launch Docker containers is a requirement of using GitHub Actions for Docker administration.
GitHub Actions Workflow: The "Complete Docker Workflow"
An extensive dissection of the "Complete Docker Workflow,” a system for managing Docker deployments with GitHub Actions, is given in this section. There are multiple stages in this workflow, all designed to improve security and expedite procedures across the container management lifecycle.
schedule:
- cron: '21 16 *'
push:
branches:
- branch-name
pull_request:
branches:
- branch-name
2. Environment Configuration
Using environment variables and secrets for configurations like Docker registry credentials secures sensitive information and streamlines the setup process across multiple environments or projects. docker.io is specified as the REGISTRY in this instance.
env:
REGISTRY: ${{ secrets.REGISTRY }}
IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.IMAGE_NAME }}
3. Initial Setup and Configurations
Checkout Repository
Install Cosign
- name: Install Cosign
uses: sigstore/[email protected]
领英推荐
Set up QEMU
- name: Set up QEMU
uses: docker/[email protected]
Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/[email protected]
Log in to Docker Registry
- name: Log in to Docker Registry
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
Extract Docker Metadata
- name: Extract Docker metadata
id: meta
uses: docker/[email protected]
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
4. Building and Pushing Images
The procedure ensures interoperability across various hardware settings by supporting the development of images for many architectures using Docker Buildx and QEMU. Developers' work is made easier by automating the push to registries, freeing them up to concentrate on essential features rather than operational setups. Here, Linux/amd64 is the chosen OS. Please be aware that you can use multiple operating systems.
- name: Build and Push container images
uses: docker/[email protected]
with:
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
5. Security Scanning with Trivy
Preventing potential security risks before they arise in production requires integrating Trivy scans to evaluate image vulnerabilities. This scan is essential for keeping a secure deployment as it looks for vulnerabilities at the OS and library levels. If there are multiple tags for the image, you can duplicate the below step and specify each of the tags.
- name: Scan Docker image with Trivy (specifying a tag)
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:tag
format: 'table'
severity: 'CRITICAL,HIGH'
vuln-type: 'os,library'
6. Digital Signing of Images with Cosign
By confirming the source of the images and signing them using Cosign, GitHub's OIDC integration adds an extra degree of security to ensure that only validated images are released.
- name: Sign the images with GitHub OIDC Token (Non-interactive)
run: |
IFS=',' read -ra ADDR <<< "${{ steps.meta.outputs.tags }}"
for tag in "${ADDR[@]}"; do
echo "Signing $tag"
cosign sign --oidc-issuer=https://token.actions.githubusercontent.com --yes "$tag"
done
env:
COSIGN_EXPERIMENTAL: "true"
Complete CI/CD Pipeline
name: Complete Docker Workflow
on:
schedule:
- cron: '21 16 *'
push:
branches:
- branch-name
tags:
- 'v*.*.*'
pull_request:
branches:
- branch-name
env:
REGISTRY: ${{ secrets.REGISTRY }}
IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.IMAGE_NAME }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write # needed for signing the images with GitHub OIDC Token
steps:
- name: Checkout repository
uses: actions/[email protected]
with:
fetch-depth: 1
- name: Install Cosign
uses: sigstore/[email protected]
- name: Set up QEMU
uses: docker/[email protected]
- name: Set up Docker Buildx
uses: docker/[email protected]
- name: Log in to Docker Registry
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract Docker metadata
id: meta
uses: docker/[email protected]
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and Push container images
uses: docker/[email protected]
with:
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
- name: Scan Docker image with Trivy (tag1)
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:tag1
format: 'table'
severity: 'CRITICAL,HIGH'
vuln-type: 'os,library'
- name: Scan Docker image with Trivy (tag2)
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:tag2
format: 'table'
severity: 'CRITICAL,HIGH'
vuln-type: 'os,library'
- name: Sign the images with GitHub OIDC Token (Non-interactive)
run: |
IFS=',' read -ra ADDR <<< "${{ steps.meta.outputs.tags }}"
for tag in "${ADDR[@]}"; do
echo "Signing $tag"
cosign sign --oidc-issuer=https://token.actions.githubusercontent.com --yes "$tag"
done
env:
COSIGN_EXPERIMENTAL: "true"
Advantages over Traditional Methods
There are several benefits to automating these procedures over more conventional manual ones.
Conclusion
This pipeline comprehensively covers all aspects of Docker image management, from build to security checks to deployment, ensuring high standards of automation and security using GitHub Actions. This setup not only automates the build and deployment process but also incorporates critical security practices like scanning and signing images, pivotal for maintaining the integrity and trustworthiness of software in a CI/CD environment.
However, it should be noted that the pipeline might be more complex than this, depending on the developers' goals. This pipeline has provided a baseline configuration to get you started with automating your deployment while ensuring that the published Docker images are secured.
Founder & CBO at D-Aggregate|10Years Data Expert| @Kaggle Contributor|ML Researcher
5 个月Good work