Skip to main content

publish container image

ben.wangzLess than 1 minute

publish container image

prepare

  1. argo workflows is ready
  2. the storage class named local-disks is ready

demo

  1. prepare publish-container-image.yaml
    • dind
      apiVersion: argoproj.io/v1alpha1
      kind: Workflow
      metadata:
        generateName: publish-container-image-
      spec:
        entrypoint: entry
        serviceAccountName: argo-workflow
        volumeClaimTemplates:
        - metadata:
            name: workspace
          spec:
            accessModes:
            - ReadWriteOnce
            storageClassName: local-disks
            resources:
              requests:
                storage: 1Gi
        templates:
        - name: entry
          dag:
            tasks:
            - name: dind
              template: dind
            - name: wait-for-dind
              dependencies:
              - dind
              template: wait-for-dind
              arguments:
                parameters:
                - name: dockerd-host
                  value: "{{tasks.dind.ip}}"
            - name: publish
              dependencies:
              - wait-for-dind
              template: publish
              arguments:
                parameters:
                - name: dockerd-host
                  value: "{{tasks.dind.ip}}"
        - name: dind
          daemon: true
          container:
            image: docker.io/library/docker:25.0.3-dind-alpine3.19
            env:
            - name: DOCKER_TLS_CERTDIR
              value: ""
            command:
            - dockerd-entrypoint.sh
            securityContext:
              privileged: true
            volumeMounts:
            - name: workspace
              mountPath: /workspace
        - name: wait-for-dind
          inputs:
            parameters:
            - name: dockerd-host
          container:
            image: docker.io/library/docker:25.0.3-cli-alpine3.19
            env:
            - name: DOCKER_HOST
              value: "{{inputs.parameters.dockerd-host}}"
            command:
            - sh
            - -c
            args:
            - |
              until docker ps; do sleep 3; done;
        - name: publish
          inputs:
            artifacts:
            - name: source
              path: /workspace/code
              git:
                repo: https://github.com/ben-wangz/blog.git
                revision: main
            parameters:
            - name: dockerd-host
            - name: registry-to-push
              value: docker.io
            - name: image-to-publish
              value: wangz2019/blog-docs:argo-test
            - name: registry
              value: docker.io
          container:
            image: docker.io/library/docker:25.0.3-cli-alpine3.19
            env:
            - name: DOCKER_HOST
              value: "{{inputs.parameters.dockerd-host}}"
            - name: DOCKER_USERNAME
              valueFrom:
                secretKeyRef:
                  name: registry-credentials
                  key: username
            - name: DOCKER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: registry-credentials
                  key: password
            command:
            - sh
            - -c
            args:
            - |
              set -e
              export REGISTRY={{inputs.parameters.registry}}
              export REGISTRY_TO_PUSH={{inputs.parameters.registry-to-push}}
              export IMAGE_TO_PUBLISH=${REGISTRY_TO_PUSH}/{{inputs.parameters.image-to-publish}}
              bash /workspace/code/docs/src/articles/kubernetes/argocd/argo-workflows/publish-container-image/container/build.sh ${IMAGE_TO_PUBLISH}
              docker login -u="${DOCKER_USERNAME}" -p="${DOCKER_PASSWORD}" ${REGISTRY_TO_PUSH} \
              docker push ${IMAGE_TO_PUBLISH}
            volumeMounts:
            - name: workspace
              mountPath: /workspace
      
      
  2. prepare registry-credentials secret
    • #REGISTRY_INSECURE=false
      #REGISTRY=REGISTRY
      #REGISTRY_USERNAME=your-username
      #REGISTRY_PASSWORD=your-password
      kubectl -n business-workflows create secret generic registry-credentials \
        --from-literal="insecure=${REGISTRY_INSECURE:-false}" \
        --from-literal="registry=${REGISTRY:-docker.io}" \
        --from-literal="username=${REGISTRY_USERNAME:-wangz2019}" \
        --from-literal="password=${REGISTRY_PASSWORD}"
      
      
  3. submit with argo workflow client
    • argo -n business-workflows submit publish-container-image.yaml
      
  4. check status
    • argo -n business-workflows list
      
    • # argo -n business-workflows get publish-container-image-2j5z2
      argo -n business-workflows get @lastest
      
    • # argo -n business-workflows logs publish-container-image-2j5z2
      argo -n business-workflows logs @latest