diff --git a/services/actions-handler/handler/controller_builds.go b/services/actions-handler/handler/controller_builds.go index 4fcb7098b5..b05b62dfb4 100644 --- a/services/actions-handler/handler/controller_builds.go +++ b/services/actions-handler/handler/controller_builds.go @@ -39,7 +39,23 @@ func (m *Messenger) handleBuild(ctx context.Context, messageQueue *mq.MessageQue // set up a lagoon client for use in the following process l := lclient.New(m.LagoonAPI.Endpoint, "actions-handler", &token, false) - deployment, err := lagoon.GetDeploymentByName(ctx, message.Meta.Project, message.Meta.Environment, message.Meta.BuildName, false, l) + environment, err := lagoon.GetEnvironmentByNamespace(ctx, message.Namespace, l) + if err != nil || environment == nil { + if err != nil { + m.toLagoonLogs(messageQueue, map[string]interface{}{ + "severity": "error", + "event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"), + "meta": environment, + "message": err.Error(), + }) + if m.EnableDebug { + log.Println(fmt.Sprintf("%sERROR:unable to get environment by namespace - %v", prefix, err)) + } + return err + } + } + environmentID := environment.ID + deployment, err := lagoon.GetDeploymentByName(ctx, message.Meta.Project, environment.Name, message.Meta.BuildName, false, l) if err != nil { m.toLagoonLogs(messageQueue, map[string]interface{}{ "severity": "error", @@ -60,58 +76,6 @@ func (m *Messenger) handleBuild(ctx context.Context, messageQueue *mq.MessageQue } return nil } - var environmentID uint - environment := &schema.Environment{} - // determine the environment id from the message - if message.Meta.ProjectID == nil && message.Meta.EnvironmentID == nil { - project, err := lagoon.GetMinimalProjectByName(ctx, message.Meta.Project, l) - if err != nil { - // send the log to the lagoon-logs exchange to be processed - m.toLagoonLogs(messageQueue, map[string]interface{}{ - "severity": "error", - "event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"), - "meta": project, - "message": err.Error(), - }) - if m.EnableDebug { - log.Println(fmt.Sprintf("%sERROR: unable to get project - %v", prefix, err)) - } - return err - } - environment, err = lagoon.GetEnvironmentByName(ctx, message.Meta.Environment, project.ID, l) - if err != nil { - // send the log to the lagoon-logs exchange to be processed - m.toLagoonLogs(messageQueue, map[string]interface{}{ - "severity": "error", - "event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"), - "meta": environment, - "message": err.Error(), - }) - if m.EnableDebug { - log.Println(fmt.Sprintf("%sERROR: unable to get environment - %v", prefix, err)) - } - return err - } - environmentID = environment.ID - } else { - // pull the id from the message - environmentID = *message.Meta.EnvironmentID - environment, err = lagoon.GetEnvironmentByID(ctx, environmentID, l) - if err != nil { - // send the log to the lagoon-logs exchange to be processed - m.toLagoonLogs(messageQueue, map[string]interface{}{ - "severity": "error", - "event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"), - "meta": environment, - "message": err.Error(), - }) - if m.EnableDebug { - log.Println(fmt.Sprintf("%sERROR: unable to get environment - %v", prefix, err)) - } - return err - } - } - // prepare the deployment patch for later step statusType := schema.StatusTypes(strings.ToUpper(buildStatus)) updateDeploymentPatch := schema.UpdateDeploymentPatchInput{ diff --git a/tests/tasks/api/get-latest-deployment-source.yaml b/tests/tasks/api/get-latest-deployment-source.yaml index 62fd476bc6..cd61dd67a9 100644 --- a/tests/tasks/api/get-latest-deployment-source.yaml +++ b/tests/tasks/api/get-latest-deployment-source.yaml @@ -28,7 +28,7 @@ query: '{{ lookup("template", "./get-latest-deployment-source.gql") }}' register: apiresponse - - name: "{{ testname }} - POST api trigger bulk deployment for environments to {{ graphql_url }}" + - name: "{{ testname }} - POST api check response {{ graphql_url }}" debug: msg: "api response: {{ apiresponse.json }}" @@ -49,6 +49,6 @@ retries: 60 delay: 10 - - name: "{{ testname }} - POST api trigger bulk deployment for environments to {{ graphql_url }}" + - name: "{{ testname }} - POST api check response {{ graphql_url }}" debug: msg: "api response: {{ apiresponse.json }}" diff --git a/tests/tasks/api/get-latest-deployment-status.gql b/tests/tasks/api/get-latest-deployment-status.gql new file mode 100644 index 0000000000..2244afc675 --- /dev/null +++ b/tests/tasks/api/get-latest-deployment-status.gql @@ -0,0 +1,19 @@ +query environmentByKubernetesNamespaceName { + environmentByKubernetesNamespaceName( + kubernetesNamespaceName: "{{ namespace }}" + ){ + deployments(limit: 1){ + id + name + status + bulkId + bulkName + priority + started + created + completed + sourceUser + sourceType + } + } +} \ No newline at end of file diff --git a/tests/tasks/api/get-latest-deployment-status.yaml b/tests/tasks/api/get-latest-deployment-status.yaml new file mode 100644 index 0000000000..fa24bd544c --- /dev/null +++ b/tests/tasks/api/get-latest-deployment-status.yaml @@ -0,0 +1,53 @@ +- name: "{{ testname }} - Verify that status are correct" + block: + - ansible.builtin.include_tasks: admin-token.yaml + - name: "{{ testname }} - POST api check latest deployment {{ graphql_url }}" + uri: + url: "{{ graphql_url }}" + method: POST + headers: + Authorization: "Bearer {{ admin_token }}" + body_format: json + body: + query: '{{ lookup("template", "./get-latest-deployment-status.gql") }}' + register: apiresponse + until: + - apiresponse.json.data.environmentByKubernetesNamespaceName.deployments[0].status is defined + retries: 30 + delay: 10 + + - ansible.builtin.include_tasks: admin-token.yaml + - name: "{{ testname }} - POST api check latest deployment {{ graphql_url }}" + uri: + url: "{{ graphql_url }}" + method: POST + headers: + Authorization: "Bearer {{ admin_token }}" + body_format: json + body: + query: '{{ lookup("template", "./get-latest-deployment-status.gql") }}' + register: apiresponse + + - name: "{{ testname }} - POST api check response {{ graphql_url }}" + debug: + msg: "api response: {{ apiresponse.json }}" + + - ansible.builtin.include_tasks: admin-token.yaml + - name: "{{ testname }} - POST api check latest deployment status are correct {{ graphql_url }}" + uri: + url: "{{ graphql_url }}" + method: POST + headers: + Authorization: "Bearer {{ admin_token }}" + body_format: json + body: + query: '{{ lookup("template", "./get-latest-deployment-status.gql") }}' + register: apiresponse + until: + - apiresponse.json.data.environmentByKubernetesNamespaceName.deployments[0].status == buildStatus + retries: 60 + delay: 10 + + - name: "{{ testname }} - POST api check response {{ graphql_url }}" + debug: + msg: "api response: {{ apiresponse.json }}" diff --git a/tests/tests/github/branch-picky.yaml b/tests/tests/github/branch-picky.yaml index ea865fc327..22bab7971f 100644 --- a/tests/tests/github/branch-picky.yaml +++ b/tests/tests/github/branch-picky.yaml @@ -24,6 +24,15 @@ project: "{{ project }}" url: "{{ check_url }}" +- name: "{{ testname }} - api check deployment status" + hosts: localhost + serial: 1 + vars: + namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}" + buildStatus: "complete" + tasks: + - ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml + - name: "{{ testname }} - webhook github delete push" hosts: localhost serial: 1 diff --git a/tests/tests/github/branch.yaml b/tests/tests/github/branch.yaml index b3b26bc564..0358af44f8 100644 --- a/tests/tests/github/branch.yaml +++ b/tests/tests/github/branch.yaml @@ -88,6 +88,15 @@ project: "{{ project }}" url: "{{ check_url }}" +- name: "{{ testname }} - api check deployment status" + hosts: localhost + serial: 1 + vars: + namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}" + buildStatus: "complete" + tasks: + - ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml + - name: "{{ testname }} - webhook github delete push" hosts: localhost serial: 1 diff --git a/tests/tests/gitlab/branch.yaml b/tests/tests/gitlab/branch.yaml index 9396ffa83c..c81b127879 100644 --- a/tests/tests/gitlab/branch.yaml +++ b/tests/tests/gitlab/branch.yaml @@ -78,6 +78,15 @@ project: "{{ project }}" url: "{{ check_url }}" +- name: "{{ testname }} - api check deployment status" + hosts: localhost + serial: 1 + vars: + namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}" + buildStatus: "complete" + tasks: + - ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml + - name: "{{ testname }} - webhook gitlab delete push" hosts: localhost serial: 1 diff --git a/tests/tests/nginx/nginx.yaml b/tests/tests/nginx/nginx.yaml index fa2fe69552..815028ea18 100644 --- a/tests/tests/nginx/nginx.yaml +++ b/tests/tests/nginx/nginx.yaml @@ -170,6 +170,15 @@ tasks: - ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-source.yaml +- name: "{{ testname }} - api check deployment status" + hosts: localhost + serial: 1 + vars: + namespace: "{{ project | regex_replace('_', '-') }}-{{ branch | regex_replace('/', '-') }}" + buildStatus: "complete" + tasks: + - ansible.builtin.include_tasks: ../../tasks/api/get-latest-deployment-status.yaml + - name: "{{ testname }} - api deleteEnvironment on {{ project }}, which should remove all resources" hosts: localhost serial: 1