|
1 | | -name: CI/CD Workflow |
| 1 | +name: Manual Docker Image Build and Push (Node / Node+Java) |
2 | 2 |
|
3 | 3 | on: |
4 | | - push: |
5 | | - branches: |
6 | | - - master |
7 | | - paths: |
8 | | - - 'image-tag' |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + image_tag: |
| 7 | + description: "Debian base tag (e.g. bullseye, bookworm)" |
| 8 | + required: true |
| 9 | + default: "bullseye" |
| 10 | + type: string |
| 11 | + include_java: |
| 12 | + description: "Include Java + Maven in the image?" |
| 13 | + required: false |
| 14 | + default: "false" |
| 15 | + type: choice |
| 16 | + options: |
| 17 | + - "true" |
| 18 | + - "false" |
| 19 | + nvm_version: |
| 20 | + description: "NVM version to install" |
| 21 | + required: false |
| 22 | + default: "0.39.7" |
| 23 | + type: string |
| 24 | + node_version: |
| 25 | + description: "Node.js version to install" |
| 26 | + required: true |
| 27 | + default: "22.1.0" |
| 28 | + type: string |
| 29 | + yarn_version: |
| 30 | + description: "Yarn version to install" |
| 31 | + required: false |
| 32 | + default: "1.22.22" |
| 33 | + type: string |
| 34 | + docker_gid: |
| 35 | + description: "GID of the 'docker' group on the host (used to access docker.sock)" |
| 36 | + required: false |
| 37 | + default: "1001" |
| 38 | + type: string |
| 39 | + java_version: |
| 40 | + description: "Java (OpenJDK) version to install (if Java enabled)" |
| 41 | + required: false |
| 42 | + default: "17" |
| 43 | + type: string |
| 44 | + maven_version: |
| 45 | + description: "Maven version to install (if Java enabled)" |
| 46 | + required: false |
| 47 | + default: "3.9.6" |
| 48 | + type: string |
| 49 | + use_cache: |
| 50 | + description: "Use Docker cache?" |
| 51 | + required: false |
| 52 | + default: "true" |
| 53 | + type: choice |
| 54 | + options: |
| 55 | + - "true" |
| 56 | + - "false" |
| 57 | + update_latest: |
| 58 | + description: "Also tag and push as 'latest'?" |
| 59 | + required: false |
| 60 | + default: "false" |
| 61 | + type: choice |
| 62 | + options: |
| 63 | + - "true" |
| 64 | + - "false" |
9 | 65 |
|
10 | 66 | jobs: |
11 | | - test: |
12 | | - name: Build and push image to dockerhub |
| 67 | + build_and_push: |
| 68 | + name: Build and Push Docker Image |
| 69 | + if: github.actor == 'lgdevlop' |
13 | 70 | runs-on: ubuntu-latest |
14 | | - steps: |
15 | | - - name: Checkout master |
16 | | - uses: actions/checkout@v2 |
17 | | - |
18 | | - - name: Setup Docker Buildx |
19 | | - id: buildx |
20 | | - uses: docker/setup-buildx-action@v1 |
21 | | - |
22 | | - # - name: Cache Docker layers |
23 | | - # uses: actions/cache@v2 |
24 | | - # with: |
25 | | - # path: /tmp/.buildx-cache |
26 | | - # key: ${{ runner.os }}-buildx-${{ github.sha }} |
27 | | - # restore-keys: | |
28 | | - # ${{ runner.os }}-buildx- |
29 | 71 |
|
30 | | - - name: Login to DockerHub |
31 | | - uses: docker/login-action@v1 |
32 | | - with: |
33 | | - username: ${{ secrets.DOCKERHUB_USERNAME }} |
34 | | - password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 72 | + steps: |
| 73 | + - name: 📥 Checkout repository |
| 74 | + uses: actions/checkout@v4 |
35 | 75 |
|
36 | | - - name: Set environment variable |
| 76 | + - name: 🧠 Select Dockerfile template |
| 77 | + id: choose_template |
37 | 78 | run: | |
38 | | - echo "image_tag=$(cat image-tag)" >> $GITHUB_ENV |
| 79 | + if [[ "${{ inputs.include_java }}" == "true" ]]; then |
| 80 | + echo "TEMPLATE=Dockerfile.debian.node-java.template" >> $GITHUB_ENV |
| 81 | + else |
| 82 | + echo "TEMPLATE=Dockerfile.debian.node.template" >> $GITHUB_ENV |
| 83 | + fi |
39 | 84 |
|
40 | | - # - name: Show environment variable |
41 | | - # run: | |
42 | | - # echo "Image tag: ${{ env.image_tag }}" |
| 85 | + - name: 🛠️ Setup Docker Buildx |
| 86 | + uses: docker/setup-buildx-action@v3 |
43 | 87 |
|
44 | | - - name: Set node image on Dockerfile |
45 | | - run: | |
46 | | - sed 's|my-docker-tag|node:'"${{ env.image_tag }}"'|g' Dockerfile > dockerfile.image |
| 88 | + - name: 💾 Restore Docker cache (if enabled) |
| 89 | + if: inputs.use_cache == 'true' |
| 90 | + uses: actions/cache@v4 |
| 91 | + with: |
| 92 | + path: /tmp/.buildx-cache |
| 93 | + key: ${{ runner.os }}-buildx-${{ github.sha }} |
| 94 | + restore-keys: | |
| 95 | + ${{ runner.os }}-buildx- |
47 | 96 |
|
48 | | - # - name: Test image build |
49 | | - # uses: docker/build-push-action@v2 |
50 | | - # with: |
51 | | - # no-cache: true |
52 | | - # context: ./ |
53 | | - # file: ./dockerfile.image |
54 | | - # push: false |
55 | | - # tags: ${{ secrets.DOCKERHUB_USERNAME }}/node-dev/${{ env.image_tag }} |
| 97 | + - name: 🔐 Login to DockerHub |
| 98 | + uses: docker/login-action@v3 |
| 99 | + with: |
| 100 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 101 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
56 | 102 |
|
57 | | - - name: Push image to dockerhub |
58 | | - id: docker_build |
59 | | - uses: docker/build-push-action@v2 |
| 103 | + - name: 🏗️ Build and Push Docker image |
| 104 | + uses: docker/build-push-action@v5 |
60 | 105 | with: |
61 | | - context: ./ |
62 | | - file: ./dockerfile.image |
| 106 | + context: . |
| 107 | + file: ${{ env.TEMPLATE }} |
63 | 108 | push: true |
64 | | - tags: ${{ secrets.DOCKERHUB_USERNAME }}/node-dev:${{ env.image_tag }} |
65 | | - |
66 | | - # deploy: |
67 | | - # name: Push image to dockerhub |
68 | | - # needs: test |
69 | | - # runs-on: ubuntu-latest |
70 | | - # steps: |
71 | | - # - name: Set environment variable |
72 | | - # run: | |
73 | | - # echo "image_tag=$(cat image-tag)" >> $GITHUB_ENV |
74 | | - |
75 | | - # - name: Show Dockerfile |
76 | | - # run: | |
77 | | - # cat dockerfile.image |
78 | | - |
79 | | - # # - name: Build and push |
80 | | - # # id: docker_build |
81 | | - # # uses: docker/build-push-action@v2 |
82 | | - # # with: |
83 | | - # # context: ./ |
84 | | - # # file: ./dockerfile.image |
85 | | - # # push: true |
86 | | - # # tags: lgdevlop/node-dev/${{ env.image_tag }} |
| 109 | + tags: | |
| 110 | + ${{ secrets.DOCKERHUB_USERNAME }}/node-dev:${{ inputs.node_version }}-${{ inputs.image_tag }} |
| 111 | + ${{ inputs.update_latest == 'true' && format('{0}/node-dev:latest', secrets.DOCKERHUB_USERNAME) || '' }} |
| 112 | + build-args: | |
| 113 | + IMAGE_TAG=${{ inputs.image_tag }} |
| 114 | + NVM_VERSION=${{ inputs.nvm_version }} |
| 115 | + NODE_VERSION=${{ inputs.node_version }} |
| 116 | + YARN_VERSION=${{ inputs.yarn_version }} |
| 117 | + JAVA_VERSION=${{ inputs.java_version }} |
| 118 | + MAVEN_VERSION=${{ inputs.maven_version }} |
| 119 | + DOCKER_GID=${{ inputs.docker_gid }} |
| 120 | + cache-from: ${{ inputs.use_cache == 'true' && 'type=gha' || '' }} |
| 121 | + cache-to: ${{ inputs.use_cache == 'true' && 'type=gha,mode=max' || '' }} |
0 commit comments