diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..6d38a5b --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,53 @@ +name: CI/CD + +on: + pull_request: + branches: + - main + push: + branches: + - main + +permissions: + contents: read + packages: write + +env: + IMAGE_NAME: ghcr.io/aldriondev/http-server-devops + +jobs: + build: + name: Test, build and publish + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Run tests + run: go test ./... + + - name: Log in to github container registry + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up docker buildx + uses: docker/setup-buildx-action@v3 + + - name: Build and optionally push docker image + uses: docker/build-push-action@v6 + with: + context: . + push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + tags: | + ${{ env.IMAGE_NAME }}:latest + ${{ env.IMAGE_NAME }}:${{ github.sha }} \ No newline at end of file diff --git a/README.md b/README.md index 236495a..100443d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ containerization, CI/CD, Kubernetes deployment, and Terraform infrastructure. - **Language:** Go - **Container:** Docker - **CI/CD:** GitHub Actions -- **Registry:** Docker Hub +- **Registry:** GitHub Container Registry - **Orchestration:** Kubernetes - **IaC:** Terraform @@ -17,7 +17,8 @@ containerization, CI/CD, Kubernetes deployment, and Terraform infrastructure. ```text http-server-devops/ ├── .github/ -│ └── workflows/ # CI/CD pipeline +│ └── workflows/ +│ └── docker-publish.yml ├── k8s/ # Kubernetes manifests ├── terraform/ # Terraform configuration ├── Dockerfile @@ -31,7 +32,7 @@ http-server-devops/ - [x] Task 1: HTTP Server - [x] Task 2: Dockerize -- [ ] Task 3: CI/CD Pipeline +- [x] Task 3: CI/CD Pipeline - [ ] Task 4: Kubernetes Deployment - [ ] Task 5: Terraform Configuration @@ -94,3 +95,45 @@ docker run --rm -p 9090:9090 -e PORT=9090 http-server-devops:local ```bash curl http://localhost:9090/ ``` + +## CI/CD + +This project uses GitHub Actions to run tests, build the Docker image, and publish it to GitHub Container Registry. + +The workflow is defined in: + +```text +.github/workflows/docker-publish.yml +``` + +On pull requests to `main`, the pipeline: + +- runs Go tests +- builds the Docker image without publishing it + +On pushes to `main`, the pipeline: + +- runs Go tests +- builds the Docker image +- publishes the image to GitHub Container Registry + +Published image: + +```text +ghcr.io/aldriondev/http-server-devops +``` + +Published tags: + +- `latest` +- ghcr.io/aldriondev/http-server-devops:latest + +The workflow uses the built-in GITHUB_TOKEN with following permissions: + +```yaml +permissions: + contents: read + packages: write +``` + +No external Docker registry credentials are required.