# Concourse Lab Creation

{{#include ../../banners/hacktricks-training.md}}

## Testing Environment

### Running Concourse

#### With Docker-Compose

Ova docker-compose datoteka pojednostavljuje instalaciju za izvođenje nekih testova sa concourse:
```bash
wget https://raw.githubusercontent.com/starkandwayne/concourse-tutorial/master/docker-compose.yml
docker-compose up -d
```
Možete preuzeti komandnu liniju `fly` za vaš operativni sistem sa veba na `127.0.0.1:8080`

#### Sa Kubernetes-om (Preporučeno)

Možete lako implementirati concourse u **Kubernetes** (na **minikube** na primer) koristeći helm-chart: [**concourse-chart**](https://github.com/concourse/concourse-chart).
```bash
brew install helm
helm repo add concourse https://concourse-charts.storage.googleapis.com/
helm install concourse-release concourse/concourse
# concourse-release will be the prefix name for the concourse elements in k8s
# After the installation you will find the indications to connect to it in the console

# If you need to delete it
helm delete concourse-release
```
Nakon generisanja concourse okruženja, možete generisati tajnu i dati pristup SA koji radi u concourse web-u da pristupi K8s tajnama:
```yaml
echo 'apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: read-secrets
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-secrets-concourse
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: read-secrets
subjects:
- kind: ServiceAccount
name: concourse-release-web
namespace: default

---

apiVersion: v1
kind: Secret
metadata:
name: super
namespace: concourse-release-main
type: Opaque
data:
secret: MWYyZDFlMmU2N2Rm

' | kubectl apply -f -
```
### Kreirajte Pipeline

Pipeline se sastoji od liste [Jobs](https://concourse-ci.org/jobs.html) koja sadrži uređenu listu [Steps](https://concourse-ci.org/steps.html).

### Koraci

Mogu se koristiti različite vrste koraka:

- **the** [**`task` step**](https://concourse-ci.org/task-step.html) **pokreće** [**task**](https://concourse-ci.org/tasks.html)
- [`get` step](https://concourse-ci.org/get-step.html) preuzima [resource](https://concourse-ci.org/resources.html)
- [`put` step](https://concourse-ci.org/put-step.html) ažurira [resource](https://concourse-ci.org/resources.html)
- [`set_pipeline` step](https://concourse-ci.org/set-pipeline-step.html) konfiguriše [pipeline](https://concourse-ci.org/pipelines.html)
- [`load_var` step](https://concourse-ci.org/load-var-step.html) učitava vrednost u [local var](https://concourse-ci.org/vars.html#local-vars)
- [`in_parallel` step](https://concourse-ci.org/in-parallel-step.html) pokreće korake paralelno
- [`do` step](https://concourse-ci.org/do-step.html) pokreće korake sekvencijalno
- [`across` step modifier](https://concourse-ci.org/across-step.html#schema.across) pokreće korak više puta; jednom za svaku kombinaciju vrednosti varijabli
- [`try` step](https://concourse-ci.org/try-step.html) pokušava da pokrene korak i uspeva čak i ako korak ne uspe

Svaki [step](https://concourse-ci.org/steps.html) u [job plan](https://concourse-ci.org/jobs.html#schema.job.plan) se izvršava u **svojoj kontejneru**. Možete pokrenuti bilo šta što želite unutar kontejnera _(tj. pokrenuti moje testove, pokrenuti ovaj bash skript, izgraditi ovu sliku, itd.)_. Dakle, ako imate posao sa pet koraka, Concourse će kreirati pet kontejnera, jedan za svaki korak.

Stoga, moguće je naznačiti tip kontejnera u kojem svaki korak treba da se izvrši.

### Jednostavan Primer Pipeline-a
```yaml
jobs:
- name: simple
plan:
- task: simple-task
privileged: true
config:
# Tells Concourse which type of worker this task should run on
platform: linux
image_resource:
type: registry-image
source:
repository: busybox # images are pulled from docker hub by default
run:
path: sh
args:
- -cx
- |
sleep 1000
echo "$SUPER_SECRET"
params:
SUPER_SECRET: ((super.secret))
```

```bash
fly -t tutorial set-pipeline -p pipe-name -c hello-world.yml
# pipelines are paused when first created
fly -t tutorial unpause-pipeline -p pipe-name
# trigger the job and watch it run to completion
fly -t tutorial trigger-job --job pipe-name/simple --watch
# From another console
fly -t tutorial intercept --job pipe-name/simple
```
Proverite **127.0.0.1:8080** da vidite tok pipeline-a.

### Bash skripta sa izlazom/ulazom pipeline-a

Moguće je **sačuvati rezultate jednog zadatka u datoteku** i označiti da je to izlaz, a zatim označiti ulaz sledećeg zadatka kao izlaz prethodnog zadatka. Ono što concourse radi je da **montira direktorijum prethodnog zadatka u novom zadatku gde možete pristupiti datotekama koje je kreirao prethodni zadatak**.

### Okidači

Ne morate ručno pokretati poslove svaki put kada ih trebate izvršiti, takođe ih možete programirati da se pokreću svaki put:

- Prođe malo vremena: [Time resource](https://github.com/concourse/time-resource/)
- Na nove commit-e na glavnoj grani: [Git resource](https://github.com/concourse/git-resource)
- Novi PR-ovi: [Github-PR resource](https://github.com/telia-oss/github-pr-resource)
- Preuzmite ili pošaljite najnoviju sliku vaše aplikacije: [Registry-image resource](https://github.com/concourse/registry-image-resource/)

Pogledajte primer YAML pipeline-a koji se pokreće na nove commit-e na masteru u [https://concourse-ci.org/tutorial-resources.html](https://concourse-ci.org/tutorial-resources.html)

{{#include ../../banners/hacktricks-training.md}}
