Skip to content

Working code: update Makefile for docker image #1

Working code: update Makefile for docker image

Working code: update Makefile for docker image #1

Workflow file for this run

name: Go Project CI
on:
push:
branches:
- main
permissions:
contents: write
env:
TAG: 26.1.1
DOCKER_IMAGE: goproject
jobs:
build:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository
- name: Checkout repository
uses: actions/checkout@v2
# Step 2: Set up Go environment
- name: Set up Go 1.23
uses: actions/setup-go@v3
with:
go-version: '1.23'
# Step 3: Cache Go modules for faster builds
- name: Cache Go modules
uses: actions/cache@v2
with:
path: /tmp/mod-cache
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-mod-
# Step 4: Install Go dependencies
- name: Install dependencies
run: |
make install
# Step 5: Check Go version
- name: Check Go version
run: |
make version
# Step 6: Install golangci-lint
- name: Install golangci-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# Step 7: Run Linting
- name: Run Linting
run: |
make lint
# Step 8: Run Tests
- name: Run Tests
run: |
make test
# Step 9: Build the Go project
- name: Build the Go project
run: |
make build
# Step 10: Docker login to DockerHub
- name: Docker login
run: |
echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
# Step 11: Build and push Docker image
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKERHUB_REPONAME }}/${{ env.DOCKER_IMAGE }}:${{ env.TAG }} .
docker push ${{ secrets.DOCKERHUB_REPONAME }}/${{ env.DOCKER_IMAGE }}:${{ env.TAG }}
env:
TAG: ${{ env.TAG }}
DOCKER_IMAGE: ${{ env.DOCKER_IMAGE }}
DOCKERHUB_REPONAME: ${{ secrets.DOCKERHUB_REPONAME }}
# Step 12: SSH into Azure server and deploy
- name: SSH into Azure server and deploy
run: |
sudo apt-get install -y sshpass
# Connect to Azure server
sshpass -p "${{ secrets.AZURE_SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.AZURE_SERVER_USERNAME }}@${{ secrets.AZURE_SERVER_IP }} << 'EOF'
echo "Connection Successful"
hostname
whoami
# Set environment variables
export TAG=${{ env.TAG }}
export DOCKER_IMAGE=${{ env.DOCKER_IMAGE }}
export DOCKERHUB_REPONAME=${{ secrets.DOCKERHUB_REPONAME }}
# Install Docker if not installed
if ! command -v docker &> /dev/null
then
echo "Installing Docker..."
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
fi
# Pull the latest Docker image
echo "Pulling Docker image: $DOCKERHUB_REPONAME/$DOCKER_IMAGE:$TAG"
docker pull $DOCKERHUB_REPONAME/$DOCKER_IMAGE:$TAG
# Stop and remove existing container if running
if [ $(docker ps -q -f name=$DOCKER_IMAGE) ]; then
echo "Stopping and removing existing container..."
docker stop $DOCKER_IMAGE
docker rm $DOCKER_IMAGE
fi
# Run the container with the latest image
echo "Running the Docker container..."
docker run -d -p 8080:8080 --name $DOCKER_IMAGE $DOCKERHUB_REPONAME/$DOCKER_IMAGE:$TAG
EOF