Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

write a github actions pipeline which builds, tests and deploys a very simple golang…

  1. User

    write a github actions pipeline which builds, tests and deploys a very simple golang application to aws ecr where I can then pull it using argocd

  2. Assistant

    Certainly! Below is a GitHub Actions pipeline that builds, tests, and deploys a simple Golang application to AWS Elastic Container Registry (ECR). This pipeline can be triggered on pushes and pull requests to the main branch. After the Docker image is pushed to ECR, you can use Argo CD to pull the image for deployment.

    First, make sure to replace placeholders like YOUR_ECR_REGION, YOUR_ECR_REPOSITORY, and set up the necessary secrets in your GitHub repository.

    # .github/workflows/ci-cd.yml
    
    name: CI/CD Pipeline
    
    on:
      push:
        branches: [ main ]
      pull_request:
        branches: [ main ]
    
    jobs:
      build-test-deploy:
        name: Build, Test, and Deploy
        runs-on: ubuntu-latest
    
        steps:
          # Checkout the repository
          - name: Checkout code
            uses: actions/checkout@v3
    
          # Set up Go environment
          - name: Set up Go
            uses: actions/setup-go@v3
            with:
              go-version: '1.19'
    
          # Cache Go modules
          - name: Cache Go modules
            uses: actions/cache@v3
            with:
              path: |
                ~/.cache/go-build
                ${{ env.GOPATH }}/pkg/mod
              key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
              restore-keys: |
                ${{ runner.os }}-go-
    
          # Build the application
          - name: Build application
            run: go build -v ./...
    
          # Run tests
          - name: Run tests
            run: go test -v ./...
    
          # Log in to Amazon ECR
          - name: Log in to Amazon ECR
            id: login-ecr
            uses: aws-actions/amazon-ecr-login@v1
            with:
              aws-region: YOUR_ECR_REGION
    
          # Build, tag, and push the Docker image to ECR
          - name: Build, tag, and push image to ECR
            env:
              ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
              ECR_REPOSITORY: YOUR_ECR_REPOSITORY
              IMAGE_TAG: ${{ github.sha }}
            run: |
              # Build the Docker image
              docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
              
              # Push the Docker image to ECR
              docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
    
          # Output the image URI for reference
          - name: Image URI
            run: echo "Image pushed to ECR: $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
    
    

    Setup Instructions

    1. AWS Credentials

    To allow GitHub Actions to authenticate with AWS ECR, you need to add your AWS credentials as secrets in your GitHub repository:

    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY

    Steps to add secrets:

    1. In your GitHub repository, go to Settings > Secrets and variables > Actions.
    2. Click on New repository secret.
    3. Add the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY secrets.

    2. Replace Placeholders

    Update the workflow file by replacing the placeholders with your specific values:

    • YOUR_ECR_REGION: The AWS region where your ECR registry is located (e.g., us-east-1).
    • YOUR_ECR_REPOSITORY: The name of your ECR repository (e.g., my-golang-app).

    3. Ensure ECR Repository Exists

    Make sure that the ECR repository exists in AWS. If it doesn't, you can create it using the AWS CLI:

    aws ecr create-repository --repository-name YOUR_ECR_REPOSITORY --region YOUR_ECR_REGION
    

    4. Dockerfile in Repository Root

    Ensure you have a Dockerfile at the root of your repository. This Dockerfile should specify how to build your Golang application into a Docker image. Here is a simple example:

    # Use the official Golang image as the base
    FROM golang:1.19-alpine AS builder
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # Copy go modules manifests
    COPY go.mod go.sum ./
    
    # Download dependencies
    RUN go mod download
    
    # Copy the source code
    COPY . .
    
    # Build the application
    RUN go build -o myapp
    
    # Use a minimal base image for the final image
    FROM alpine:latest
    
    # Set the working directory
    WORKDIR /root/
    
    # Copy the binary from the builder stage
    COPY --from=builder /app/myapp .
    
    # Expose necessary port (if your application uses a specific port)
    EXPOSE 8080
    
    # Command to run the executable
    CMD ["./myapp"]
    

    5. Pulling the Image with Argo CD

    After the image is pushed to ECR, you can configure Argo CD to pull the image using the image URI:

    <ECR_REGISTRY>/<ECR_REPOSITORY>:<IMAGE_TAG>
    

    In the workflow above, <IMAGE_TAG> is set to the GitHub commit SHA (${{ github.sha }}).

    Summary

    This GitHub Actions pipeline automates the process of building, testing, and deploying your Golang application to AWS ECR. By integrating with Argo CD, you can achieve a seamless CI/CD workflow.

    Feel free to customize the pipeline further to suit your project's needs!