Argo Workflows build demo get commitid

基于上一个例子,本例中将使用 commitid 来作为 image的tag

其中涉及了

  1. 如何在task之间传递 parameter
  2. 如何获取 commitid

因为 command 和 entrypoint 都无法使用 管道和重定向 ( args 都以参数形式提供 )
所以这里使用了一个 ubuntu + shell 脚本的方式来 将 commitid 写入缓存文件

/getcommitid.sh

touch /work/$1/commitid.txt
git -C /work/$1 rev-parse HEAD > /work/$1/commitid.txt
ls /work
ls /work/$1
cat /work/$1/commitid.txt

Dockerfile

FROM ubuntu
RUN apt -y update && apt install -y git
COPY getcommitid.sh /
RUN chmod 777 /getcommitid.sh

然后yaml文件内容

这里通过 output parameter 进行传递参数,请注意加粗的部分

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: buildkit-
  namespace: argo
spec:
  ttlStrategy:
    secondsAfterCompletion: 1800 # Time to live after workflow is completed, replaces ttlSecondsAfterFinished
    secondsAfterSuccess: 1800     # Time to live after workflow is successful
    secondsAfterFailure: 1800     # Time to live after workflow fails
  arguments:
    parameters:
      - name: repo
        value: https://USERNAME:PASSWORD@github.com/zl86790/argo_build_demo_react.git
      - name: branch
        value: master
      - name: path
        value: helloworld
      - name: image
        value: libaibai/argobuilddemo
  entrypoint: main
  volumeClaimTemplates:
    - metadata:
        name: work
      spec:
        accessModes: [ "ReadWriteOnce" ]
        # storageClassName: local-storage
        storageClassName: local-path
        resources:
          requests:
            storage: 1Gi
  templates:
    - name: main
      dag:
        tasks:
          - name: clone
            template: clone
            arguments:
              parameters:
                - name: repo
                  value: "{{workflow.parameters.repo}}"
                - name: branch
                  value: "{{workflow.parameters.branch}}"
          - name: getcommitid
            template: getcommitid
            arguments:
              parameters:
                - name: path
                  value: "{{workflow.parameters.path}}"
            depends: "clone"
          - name: mountcommitid
            template: mountcommitid
            arguments:
              parameters:
                - name: path
                  value: "{{workflow.parameters.path}}"
            depends: "getcommitid"
          - name: install
            template: install
            arguments:
              parameters:
                - name: path
                  value: "{{workflow.parameters.path}}"
            depends: "mountcommitid"
          - name: build
            template: build
            arguments:
              parameters:
                - name: path
                  value: "{{workflow.parameters.path}}"
            depends: "install"
          - name: image
            template: image
            arguments:
              parameters:
                - name: path
                  value: "{{workflow.parameters.path}}"
                - name: image
                  value: "{{workflow.parameters.image}}"
                - name: commitid
                  value: "{{tasks.mountcommitid.outputs.parameters.commitid}}"
            depends: "build"
    - name: clone
      inputs:
        parameters:
          - name: repo
          - name: branch
      container:
        volumeMounts:
          - mountPath: /work
            name: work
        image: alpine/git:v2.26.2
        workingDir: /work
        # Do a shallow clone, which is the fastest way to clone, by using the
        # --depth, --branch, and --single-branch options
        args:
          - clone
          - --depth
          - "1"
          - --branch
          - "{{inputs.parameters.branch}}"
          - --single-branch
          - "{{inputs.parameters.repo}}"
          - .
    - name: getcommitid
      inputs:
        parameters:
          - name: path
      container:
        volumeMounts:
          - mountPath: /work
            name: work
        image: libaibai/getcommitid
        workingDir: /work
        command:
          - /bin/bash
        args:
          - -c
          - "/getcommitid.sh {{inputs.parameters.path}}"
    - name: mountcommitid
      inputs:
        parameters:
          - name: path
      outputs:
        parameters:
          - name: commitid
            valueFrom:
              default: "mockup_commitid"   # Default value to use if retrieving valueFrom fails. If not provided workflow will fail instead
              path: /work/{{inputs.parameters.path}}/commitid.txt
      container:
        volumeMounts:
          - mountPath: /work
            name: work
        image: busybox
        workingDir: /work/{{inputs.parameters.path}}
        command:
          - /bin/sh
        args:
          - -c
          - "ls /work"
    - name: install
      inputs:
        parameters:
          - name: path
      container:
        image: node:14.17.3-alpine3.14
        volumeMounts:
          - mountPath: /work
            name: work
        workingDir: /work/{{inputs.parameters.path}}
        env:
          # Because this is not a Gomodule, we must turn modules off.
          - name: ENVTEST
            value: "helloworld"
        command:
          - npm
        args:
          - install
    - name: build
      inputs:
        parameters:
          - name: path
      container:
        image: node:14.17.3-alpine3.14
        volumeMounts:
          - mountPath: /work
            name: work
        workingDir: /work/{{inputs.parameters.path}}
        env:
          # Because this is not a Gomodule, we must turn modules off.
          - name: ENVTEST
            value: "helloworld"
        command:
          - npm
        args:
          - run
          - build
    - name: image
      inputs:
        parameters:
          - name: path
          - name: image
          - name: commitid
      # Mount the configuration so we can push the image.
      # This should create the /.docker/config.json file.
      volumes:
      - name: jenkins-docker-cfg
        projected:
          sources:
          - secret:
              name: regcred
              items:
                - key: .dockerconfigjson
                  path: .docker/config.json
      container:
        # image: debug-v0.19.0
        image: gcr.io/kaniko-project/executor@sha256:f652f28537fa76e8f4f9393de13a064f0206003c451ce2ad6e4359fd5a21acbc
        volumeMounts:
          - name: work
            mountPath: /work
          - name: jenkins-docker-cfg
            mountPath: /root
        workingDir: /work/{{inputs.parameters.path}}
        env:
          - name: envkey
            value: envvalue
        command:
          - /kaniko/executor
        args:
          - -f
          - /work/{{inputs.parameters.path}}/Dockerfile
          - -c
          - /work/{{inputs.parameters.path}}/
          - --destination={{inputs.parameters.image}}:{{inputs.parameters.commitid}}
Send a Message