diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..0c1fe8c --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,47 @@ +name: Docker image + +on: + push: + branches: + - refactor + - master +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Install yq + run: | + sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq + sudo chmod +x /usr/bin/yq + + - name: Run docker tag sh and export var from config.yml + run: | + ./docker_tag.sh + + - name: Export env variabels + uses: cardinalby/export-env-action@v2 + with: + envFile: '.env' + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ env.DOCKER_HUB_USERNAME }}/${{ env.DOCKER_REPO_NAME }}:${{ env.NEW_TAG }} + if: ${{ env.PUSH_TO_DOCKER }} == 'true' diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 6883820..05e2021 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -1,4 +1,4 @@ -name: Linting +name: continuous integration on: push: @@ -6,10 +6,8 @@ on: - '**.go' - go.sum - go.mod - branches-ignore: - - main pull_request: - + jobs: lint: runs-on: ubuntu-latest @@ -17,13 +15,13 @@ jobs: - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: - go-version: '1.22.1' + go-version: '1.22.2' - name: Deps cache id: cache-go-deps - uses: actions/cache@v3 + uses: actions/cache@v4 env: cache-name: go-deps-cache with: @@ -33,7 +31,7 @@ jobs: ${{ runner.os }}-build-${{ env.cache-name }}- - if: ${{ steps.cache-go-deps.outputs.cache-hit != 'true' }} - name: List the state of node modules + name: List the state of go modules continue-on-error: true run: go mod graph @@ -41,6 +39,7 @@ jobs: run: | go mod tidy go mod download + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.57.2 - name: Run golangci-lint - uses: golangci/golangci-lint-action@v3.7.0 \ No newline at end of file + run: golangci-lint run diff --git a/Dockerfile b/Dockerfile index 72a10b9..701baae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN go install github.com/a-h/templ/cmd/templ@latest RUN templ generate RUN go build -o main cmd/api/main.go -FROM alpine:3.20.1 as prod +FROM alpine:3.20.1 WORKDIR /app COPY --from=build /app/main /app/main EXPOSE 5000 diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index a23e850..0000000 --- a/Jenkinsfile +++ /dev/null @@ -1,104 +0,0 @@ -pipeline { - agent any - - environment { - GITHUB_USER = 'ujstor' - GITHUB_REPO = 'portfolio-web' - DOCKER_HUB_USERNAME = 'ujstor' - DOCKER_REPO_NAME = 'portfolio-web-go' - BRANCH = 'master' - VERSION_PART = 'Patch' // Patch, Minor, Major - DOCKER_JENKINS_CERDIDENTALS_ID = 'be9636c4-b828-41af-ad0b-46d4182dfb06' - TAG = '' // Generated automatically - } - - stages { - stage('Checkout Code') { - steps { - script { - git(url: "https://github.com/${GITHUB_USER}/${GITHUB_REPO}/", branch: env.BRANCH_NAME) - } - } - } - - stage('chmod Tag sh') { - steps { - script { - sh "chmod 777 ${WORKSPACE}/docker_tag.sh" - } - } - } - - stage('Generate Docker Image Tag') { - when { - expression { env.BRANCH_NAME == env.BRANCH} - } - steps { - script { - TAG = sh(script: "${WORKSPACE}/docker_tag.sh $DOCKER_HUB_USERNAME $DOCKER_REPO_NAME $VERSION_PART", returnStdout: true).trim() - - if (TAG) { - echo "Docker image tag generated successfully: $TAG" - } else { - error "Failed to generate Docker image tag" - } - - env.TAG = TAG - } - } - } - - stage('Docker Login') { - when { - expression { env.BRANCH_NAME == env.BRANCH } - } - steps { - script { - - withCredentials([usernamePassword(credentialsId: env.DOCKER_JENKINS_CERDIDENTALS_ID, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) { - sh "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD" - } - } - } - } - - stage('Build') { - when { - expression { env.BRANCH_NAME == env.BRANCH } - } - steps { - script { - sh "docker build --no-cache -t ${DOCKER_HUB_USERNAME}/${DOCKER_REPO_NAME}:${TAG} --target prod ." - } - } - } - - stage('Deploy') { - when { - expression { env.BRANCH_NAME == env.BRANCH } - } - steps { - script { - sh "docker push ${DOCKER_HUB_USERNAME}/${DOCKER_REPO_NAME}:${TAG}" - } - } - } - - stage('Environment Cleanup') { - when { - expression { env.BRANCH_NAME == env.BRANCH } - } - steps { - script { - sh "docker rmi ${DOCKER_HUB_USERNAME}/${DOCKER_REPO_NAME}:${TAG}" - } - } - } - } - - post { - success { - echo "Pipeline completed successfully" - } - } -} diff --git a/README.md b/README.md index 923211d..7fc0725 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,6 @@ This static website is built with Go and Templ. - -## Jenkins Pipeline -The pipeline is designed to automate the processes of testing, building, and deploying a web application using Docker. -It creates an image and pushes it to DockerHub. - -![](https://i.imgur.com/llEoE4e.png) - ## Deployment Deployment can be achieved through self-hosting service provided by [Collify](https://coolify.io/docs/installation). diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..2aa6f9e --- /dev/null +++ b/config.yml @@ -0,0 +1,5 @@ +docker: + DOCKER_HUB_USERNAME: ujstor + DOCKER_REPO_NAME: portfolio-web-go + VERSION_PART: Patch # Patch, Minor, major + PUSH_TO_DOCKER: true diff --git a/docker-compose.yml b/docker-compose.yml index 8364adc..7fe9e4e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,7 @@ -version: '3.8' - services: web: build: context: . dockerfile: Dockerfile - target: prod ports: - 5000:5000 diff --git a/docker_tag.sh b/docker_tag.sh index 9fffd7c..4d102bc 100644 --- a/docker_tag.sh +++ b/docker_tag.sh @@ -1,21 +1,19 @@ #!/bin/bash -DOCKER_HUB_USERNAME=$1 -DOCKER_REPO_NAME=$2 -VERSION_PART=$3 +eval $(yq e '.docker | to_entries | .[] | "export \(.key)=\(.value)"' config.yml) DOCKER_IMAGE="$DOCKER_HUB_USERNAME/$DOCKER_REPO_NAME" TAGS=$(curl -s "https://hub.docker.com/v2/repositories/$DOCKER_IMAGE/tags/?page_size=100" | jq -r '.results[].name') if [ -z "$TAGS" ]; then - DEFAULT_TAG="1.0.0" + DEFAULT_TAG="0.0.1" NEW_TAG="$DEFAULT_TAG" else LATEST_TAG=$(echo "$TAGS" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) if [ -z "$LATEST_TAG" ]; then - LATEST_TAG="1.0.0" + LATEST_TAG="0.0.1" fi IFS='.' read -ra PARTS <<< "$LATEST_TAG" @@ -35,4 +33,13 @@ else fi fi -echo $NEW_TAG \ No newline at end of file +create_env_file() { + cat << EOF > .env +DOCKER_HUB_USERNAME=$DOCKER_HUB_USERNAME +DOCKER_REPO_NAME=$DOCKER_REPO_NAME +NEW_TAG=$NEW_TAG +PUSH_TO_DOCKER=$PUSH_TO_DOCKER +EOF +} + +create_env_file